Skip to content

Commit

Permalink
Reduce allocation in data2map by avoiding strings.{Split,SplitN}
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Sep 3, 2020
1 parent 9c691c4 commit 2e65b01
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,23 @@ type ltsvMap map[string]string

func data2map(data []byte) (ltsvMap, error) {
d := string(data)
fields := strings.Split(d, "\t")
l := ltsvMap{}
for _, v := range fields {
kv := strings.SplitN(strings.TrimSpace(v), ":", 2)
if len(kv) != 2 {
var i int
var v string
for i < len(d) {
j := strings.IndexByte(d[i:], '\t')
if j >= 0 {
v = d[i : i+j]
i += j + 1
} else {
v = d[i:]
i = len(d)
}
k := strings.IndexByte(v, ':')
if k < 0 {
return nil, fmt.Errorf("not a ltsv: %s", d)
}
l[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
l[strings.TrimSpace(v[:k])] = strings.TrimSpace(v[k+1:])
}
return l, nil
}
Expand Down

0 comments on commit 2e65b01

Please sign in to comment.