Skip to content

Commit ed7019a

Browse files
authored
Avoid creating empty vectors in vector.Apply (#5540)
vector.Apply can create a large number of empty (zero-length) vectors through calls to vector.rip, generating substantial garbage. Fix that by changing rip to return nil instead of a slice of empty vectors, changing Apply to skip the recursive call when it sees a nil from rip and substitute a nil vector in place of the skipped call's result, and changing NewTagMap to tolerate nil vectors so NewDynamic will as well.
1 parent ef76d4d commit ed7019a

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

vector/apply.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ func Apply(ripUnions bool, eval func(...Any) Any, vecs ...Any) Any {
1818
}
1919
var results []Any
2020
for _, ripped := range rip(vecs, d) {
21-
results = append(results, Apply(ripUnions, eval, ripped...))
21+
var result Any
22+
if len(ripped) > 0 {
23+
result = Apply(ripUnions, eval, ripped...)
24+
}
25+
results = append(results, result)
2226
}
2327
return stitch(d.Tags, results)
2428
}
@@ -36,11 +40,13 @@ func rip(vecs []Any, d *Dynamic) [][]Any {
3640
var ripped [][]Any
3741
for j, rev := range d.TagMap.Reverse {
3842
var newVecs []Any
39-
for _, vec := range vecs {
40-
if vec == d {
41-
newVecs = append(newVecs, d.Values[j])
42-
} else {
43-
newVecs = append(newVecs, NewView(vec, rev))
43+
if len(rev) > 0 {
44+
for _, vec := range vecs {
45+
if vec == d {
46+
newVecs = append(newVecs, d.Values[j])
47+
} else {
48+
newVecs = append(newVecs, NewView(vec, rev))
49+
}
4450
}
4551
}
4652
ripped = append(ripped, newVecs)

vector/tagmap.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ type TagMap struct {
1313
func NewTagMap(tags []uint32, vals []Any) *TagMap {
1414
lens := make([]uint32, 0, len(vals))
1515
for _, v := range vals {
16-
lens = append(lens, uint32(v.Len()))
16+
var length uint32
17+
if v != nil {
18+
length = v.Len()
19+
}
20+
lens = append(lens, length)
1721
}
1822
return NewTagMapFromLens(tags, lens)
1923
}

0 commit comments

Comments
 (0)