From 5bbceaa14622a28c9f53ca098e4d14996ea493d2 Mon Sep 17 00:00:00 2001 From: Matthew Nibecker Date: Tue, 24 Nov 2026 12:04:47 -0800 Subject: [PATCH 1/2] rip Dynamics that share tags without a reverse tag map When every input to vector.Apply is a Dynamic with the same tags or a constant (Const, Missing, Null, None, or an Error of one), take each piece directly instead of picking through the reverse tag map. Results that keep the input's tags also reuse its cached tag maps. --- vector/apply.go | 77 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/vector/apply.go b/vector/apply.go index 15876f980..7bdb3f649 100644 --- a/vector/apply.go +++ b/vector/apply.go @@ -96,7 +96,70 @@ func Apply(opt ApplyOpt, eval func(...Any) Any, vecs ...Any) Any { results[i] = Apply(opt, eval, ripped...) } // stitch removes nils and replaces Dynamics with their values. - return stitch(d.Tags, results) + out := stitch(d.Tags, results) + if od, ok := out.(*Dynamic); ok && sameTags(od.Tags, d.Tags) { + // The tag maps depend only on the tags, so share any that d + // has already computed. + od.forwardTagMap.Store(d.forwardTagMap.Load()) + od.reverseTagMap.Store(d.reverseTagMap.Load()) + } + return out +} + +// sameTags returns true if a and b are the same slice. +func sameTags(a, b []uint32) bool { + return len(a) == len(b) && (len(a) == 0 || &a[0] == &b[0]) +} + +// zippable returns true if vec can be ripped along d's tags without d's +// reverse tag map, either because vec is a Dynamic whose slots line up with +// d's or because picking from vec depends only on the number of slots picked. +func zippable(vec Any, d *Dynamic) bool { + switch vec := vec.(type) { + case *Dynamic: + return vec == d || sameTags(vec.Tags, d.Tags) && len(vec.Values) == len(d.Values) + case *Error: + return zippable(vec.Vals, d) + case *Named: + return zippable(vec.Any, d) + case *Option: + if _, ok := vec.Any.(*Dynamic); ok { + return false + } + return zippable(vec.Any, d) + case *Const, *Missing, *Null, *None, *TypeOnly: + return true + default: + return false + } +} + +// zip returns the part of vec that lines up with d.Values[i]. vec must be +// zippable with d. +func zip(vec Any, d *Dynamic, i int) Any { + n := d.Values[i].Len() + switch vec := vec.(type) { + case *Dynamic: + return vec.Values[i] + case *Const: + return NewConst(vec.Any, n) + case *Missing: + return NewMissing(n) + case *Null: + return NewNull(n) + case *None: + return NewNone(n) + case *TypeOnly: + return NewTypeOnly(vec.typ, n) + case *Error: + return &Error{vec.Typ, zip(vec.Vals, d, i)} + case *Named: + return &Named{vec.Typ, zip(vec.Any, d, i)} + case *Option: + return &Option{Typ: vec.Typ, Any: zip(vec.Any, d, i)} + default: + panic(vec) + } } func homogenous(s []uint32) bool { @@ -115,12 +178,16 @@ func findDynamic(vecs []Any) (*Dynamic, bool) { func rip(vecs []Any, d *Dynamic) iter.Seq2[int, []Any] { return func(yield func(int, []Any) bool) { - if len(vecs) == 1 { + if !slices.ContainsFunc(vecs, func(vec Any) bool { return !zippable(vec, d) }) { for i, v := range d.Values { if v == nil || v.Len() == 0 { continue } - if !yield(i, []Any{v}) { + ripped := make([]Any, len(vecs)) + for j, vec := range vecs { + ripped[j] = zip(vec, d, i) + } + if !yield(i, ripped) { return } } @@ -132,8 +199,8 @@ func rip(vecs []Any, d *Dynamic) iter.Seq2[int, []Any] { } ripped := make([]Any, len(vecs)) for j, vec := range vecs { - if vec == d { - ripped[j] = d.Values[i] + if IsDynamic(vec) && zippable(vec, d) { + ripped[j] = zip(vec, d, i) } else { ripped[j] = Pick(vec, rev) } From 3f688e970051759b1dabc5fcd9fb057c5242c88c Mon Sep 17 00:00:00 2001 From: Matthew Nibecker Date: Tue, 24 Nov 2026 13:08:16 -0800 Subject: [PATCH 2/2] slices.Equal --- vector/apply.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/vector/apply.go b/vector/apply.go index 7bdb3f649..7e294f7ff 100644 --- a/vector/apply.go +++ b/vector/apply.go @@ -97,7 +97,7 @@ func Apply(opt ApplyOpt, eval func(...Any) Any, vecs ...Any) Any { } // stitch removes nils and replaces Dynamics with their values. out := stitch(d.Tags, results) - if od, ok := out.(*Dynamic); ok && sameTags(od.Tags, d.Tags) { + if od, ok := out.(*Dynamic); ok && len(od.Values) == len(d.Values) && sameTags(od.Tags, d.Tags) { // The tag maps depend only on the tags, so share any that d // has already computed. od.forwardTagMap.Store(d.forwardTagMap.Load()) @@ -106,9 +106,9 @@ func Apply(opt ApplyOpt, eval func(...Any) Any, vecs ...Any) Any { return out } -// sameTags returns true if a and b are the same slice. +// sameTags returns true if a and b hold the same tags. func sameTags(a, b []uint32) bool { - return len(a) == len(b) && (len(a) == 0 || &a[0] == &b[0]) + return len(a) == len(b) && (len(a) == 0 || &a[0] == &b[0] || slices.Equal(a, b)) } // zippable returns true if vec can be ripped along d's tags without d's @@ -117,7 +117,7 @@ func sameTags(a, b []uint32) bool { func zippable(vec Any, d *Dynamic) bool { switch vec := vec.(type) { case *Dynamic: - return vec == d || sameTags(vec.Tags, d.Tags) && len(vec.Values) == len(d.Values) + return vec == d || len(vec.Values) == len(d.Values) && sameTags(vec.Tags, d.Tags) case *Error: return zippable(vec.Vals, d) case *Named: @@ -178,7 +178,11 @@ func findDynamic(vecs []Any) (*Dynamic, bool) { func rip(vecs []Any, d *Dynamic) iter.Seq2[int, []Any] { return func(yield func(int, []Any) bool) { - if !slices.ContainsFunc(vecs, func(vec Any) bool { return !zippable(vec, d) }) { + zips := make([]bool, len(vecs)) + for j, vec := range vecs { + zips[j] = zippable(vec, d) + } + if !slices.Contains(zips, false) { for i, v := range d.Values { if v == nil || v.Len() == 0 { continue @@ -199,7 +203,7 @@ func rip(vecs []Any, d *Dynamic) iter.Seq2[int, []Any] { } ripped := make([]Any, len(vecs)) for j, vec := range vecs { - if IsDynamic(vec) && zippable(vec, d) { + if IsDynamic(vec) && zips[j] { ripped[j] = zip(vec, d, i) } else { ripped[j] = Pick(vec, rev)