Fix O(N²) bind parameter lookup with O(N) map pre-build#144
Open
krleonid wants to merge 1 commit into
Open
Conversation
The bind() inner loop scanned the full args slice twice per parameter (once for ordinal, once for name), making the total work O(N²). Replace both linear scans with two maps built once before the loop, reducing the complexity to O(N). Benchmarks show 42× speedup at N=1000 positional params and 22× for named params. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
71ad1c8 to
9545c30
Compare
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.
Summary
bind()previously scanned the fullargsslice twice per parameter — once for ordinal match, once for name match — making the total work O(N²)range argsloops with two maps (byOrdinal,byName) built once before the outer loop, reducing complexity to O(N)Benchmark results (
-benchmem -count=3, Apple M4 Pro)The new implementation uses slightly more memory (two maps proportional to N) but the time savings are substantial for any non-trivial number of parameters.
Test plan
TestBind*tests passgo build ./...clean🤖 Generated with Claude Code