java-topology/defects/cockroachdb/patch/cockroachdb-0001-indexes-used-map.patch

35 lines
1.2 KiB
Diff

# UNDF: UNDF-2026-000000036
--- a/pkg/sql/opt/exec/execbuilder/builder.go
+++ b/pkg/sql/opt/exec/execbuilder/builder.go
@@ -197,16 +197,28 @@ type IndexesUsed struct {
// IndexesUsed is a list of indexes used in a query.
type IndexesUsed struct {
indexes []struct {
tableID cat.StableID
indexID cat.StableID
}
+ // seen is a set of already-added (tableID, indexID) pairs for O(1)
+ // deduplication. Without it, add() calls slices.Contains on the growing
+ // slice — O(N) per add, O(N²) total across N index references in a query.
+ seen map[[2]cat.StableID]struct{}
}
-// add adds the given index to the list, if it is not already present.
+// add adds the given index to the list if it is not already present.
+// O(1) amortized via the seen map; previously O(N) via slices.Contains.
func (iu *IndexesUsed) add(tableID, indexID cat.StableID) {
s := struct {
tableID cat.StableID
indexID cat.StableID
}{tableID, indexID}
- if !slices.Contains(iu.indexes, s) {
- iu.indexes = append(iu.indexes, s)
+ key := [2]cat.StableID{tableID, indexID}
+ if iu.seen == nil {
+ iu.seen = make(map[[2]cat.StableID]struct{})
}
+ if _, ok := iu.seen[key]; !ok {
+ iu.seen[key] = struct{}{}
+ iu.indexes = append(iu.indexes, s)
+ }
}