Skip to content

Commit e44a9fc

Browse files
authored
locodedb: allocate less, don't leave references to csv data (#60)
2 parents 9063b19 + c20dea5 commit e44a9fc

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

pkg/locodedb/calls.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"cmp"
55
"errors"
66
"slices"
7-
"strings"
87
)
98

109
// ErrNotFound is returned when the record is not found in the location database.
@@ -52,10 +51,10 @@ func Get(locodeStr string) (Record, error) {
5251
}
5352

5453
code := locodeStr[CountryCodeLen:]
55-
n, _ := slices.BinarySearchFunc(cd.locodes, code, func(csv locodesCSV, s string) int {
56-
return cmp.Compare(csv.code, s)
54+
n, ok := slices.BinarySearchFunc(cd.locodes, code, func(csv locodesCSV, s string) int {
55+
return cmp.Compare(codeFromCSV(&csv), s)
5756
})
58-
if n == len(cd.locodes) || strings.Compare(cd.locodes[n].code, code) != 0 {
57+
if !ok {
5958
return Record{}, ErrNotFound
6059
}
6160

@@ -69,14 +68,18 @@ func Get(locodeStr string) (Record, error) {
6968
}, nil
7069
}
7170

71+
func codeFromCSV(c *locodesCSV) string {
72+
return locodeStrings[c.offset : c.offset+LocationCodeLen]
73+
}
74+
7275
func locFromCSV(c *locodesCSV) string {
73-
return locodeStrings[c.offset : c.offset+uint32(c.locationLen)]
76+
return locodeStrings[c.offset+LocationCodeLen : c.offset+LocationCodeLen+uint32(c.locationLen)]
7477
}
7578

7679
func divCodeFromCSV(c *locodesCSV) string {
77-
return locodeStrings[c.offset+uint32(c.locationLen) : c.offset+uint32(c.locationLen)+uint32(c.subDivCodeLen)]
80+
return locodeStrings[c.offset+LocationCodeLen+uint32(c.locationLen) : c.offset+LocationCodeLen+uint32(c.locationLen)+uint32(c.subDivCodeLen)]
7881
}
7982

8083
func divNameFromCSV(c *locodesCSV) string {
81-
return locodeStrings[c.offset+uint32(c.locationLen)+uint32(c.subDivCodeLen) : c.offset+uint32(c.locationLen)+uint32(c.subDivCodeLen)+uint32(c.subDivNameLen)]
84+
return locodeStrings[c.offset+LocationCodeLen+uint32(c.locationLen)+uint32(c.subDivCodeLen) : c.offset+LocationCodeLen+uint32(c.locationLen)+uint32(c.subDivCodeLen)+uint32(c.subDivNameLen)]
8285
}

pkg/locodedb/utils.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ type countryData struct {
4545
type locodesCSV struct {
4646
point Point
4747
offset uint32
48-
code string
4948
locationLen uint8
5049
subDivCodeLen uint8
5150
subDivNameLen uint8
@@ -106,6 +105,7 @@ func unpackLocodesData(data []byte, mc map[countryCode]countryData) (string, err
106105
subDivNameLen = uint8(len(record[4]))
107106
)
108107

108+
b.WriteString(record[0][CountryCodeLen:])
109109
b.WriteString(record[1])
110110
b.WriteString(record[3])
111111
b.WriteString(record[4])
@@ -132,7 +132,6 @@ func unpackLocodesData(data []byte, mc map[countryCode]countryData) (string, err
132132
}
133133
rec.locodes = append(rec.locodes, locodesCSV{
134134
point: Point{Latitude: float32(lat), Longitude: float32(lng)},
135-
code: record[0][CountryCodeLen:],
136135
offset: recOffset,
137136
locationLen: locationLen,
138137
subDivCodeLen: subDivCodeLen,

0 commit comments

Comments
 (0)