Skip to content

Commit

Permalink
Avoid creating empty vectors in vector.Apply (#5540)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nwt authored Dec 18, 2024
1 parent ef76d4d commit ed7019a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
18 changes: 12 additions & 6 deletions vector/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ func Apply(ripUnions bool, eval func(...Any) Any, vecs ...Any) Any {
}
var results []Any
for _, ripped := range rip(vecs, d) {
results = append(results, Apply(ripUnions, eval, ripped...))
var result Any
if len(ripped) > 0 {
result = Apply(ripUnions, eval, ripped...)
}
results = append(results, result)
}
return stitch(d.Tags, results)
}
Expand All @@ -36,11 +40,13 @@ func rip(vecs []Any, d *Dynamic) [][]Any {
var ripped [][]Any
for j, rev := range d.TagMap.Reverse {
var newVecs []Any
for _, vec := range vecs {
if vec == d {
newVecs = append(newVecs, d.Values[j])
} else {
newVecs = append(newVecs, NewView(vec, rev))
if len(rev) > 0 {
for _, vec := range vecs {
if vec == d {
newVecs = append(newVecs, d.Values[j])
} else {
newVecs = append(newVecs, NewView(vec, rev))
}
}
}
ripped = append(ripped, newVecs)
Expand Down
6 changes: 5 additions & 1 deletion vector/tagmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ type TagMap struct {
func NewTagMap(tags []uint32, vals []Any) *TagMap {
lens := make([]uint32, 0, len(vals))
for _, v := range vals {
lens = append(lens, uint32(v.Len()))
var length uint32
if v != nil {
length = v.Len()
}
lens = append(lens, length)
}
return NewTagMapFromLens(tags, lens)
}
Expand Down

0 comments on commit ed7019a

Please sign in to comment.