Repository navigation
perf(builtin): avoid quadratic string scans in uniq - #1007
Merged
Merged
Conversation
antonmedv
added a commit
that referenced
this pull request
Nov 25, 2026
Extend the string index from #1007 to integer and float values, keyed the same way runtime.Equal compares them. uniq(1..999999) drops from hours to ~200ms.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
uniqcompared each element against every value already kept, and re-boxedv.Index(i).Interface()on every comparison. For lists with many distinct strings this is quadratic work plus repeated allocations.This PR:
map[string]struct{}index (seeded from the values kept so far) and uses it for string elementsruntime.Equalscan for all non-string values and for small resultsOutput and first-occurrence order are unchanged.
runtime.Equalonly treats astringas equal to anotherstring(named string types fall through toreflect.DeepEqual, which also requires identical types), so a string-keyed index cannot change which elements are dropped.Benchmark
expr.Runwith a pre-compiled program, Apple M4 Pro, Go 1.27, median of 7:join(uniq(labels), ",")join(uniq(labels), ",")join(uniq(labels), ",")join(uniq(map(orders, .CustomerEmail)), ",")join(uniq(map(orders, .CustomerEmail)), ",")Lists with very few distinct values never reach the index threshold and stay within ~2% of before.
Tests
Added tests covering the index path (more than 16 distinct strings, mixed string / non-string values, ordering).
go test ./...passes.