Skip to content

Fix O(N²) bind parameter lookup with O(N) map pre-build#144

Open
krleonid wants to merge 1 commit into
duckdb:mainfrom
krleonid:fix-bind-on2-lookup
Open

Fix O(N²) bind parameter lookup with O(N) map pre-build#144
krleonid wants to merge 1 commit into
duckdb:mainfrom
krleonid:fix-bind-on2-lookup

Conversation

@krleonid
Copy link
Copy Markdown

@krleonid krleonid commented Jun 2, 2026

Summary

  • bind() previously scanned the full args slice twice per parameter — once for ordinal match, once for name match — making the total work O(N²)
  • Replace both inner range args loops with two maps (byOrdinal, byName) built once before the outer loop, reducing complexity to O(N)

Benchmark results (-benchmem -count=3, Apple M4 Pro)

N Old positional (ns/op) New positional (ns/op) Speedup
10 210 440 0.5× (map overhead dominates)
50 3,780 1,610 2.3×
100 14,600 3,100 4.7×
200 59,000 5,600 10.5×
1000 1,595,000 38,000 42×
N Old named (ns/op) New named (ns/op) Speedup
100 27,900 7,700 3.6×
200 75,400 15,000 5.0×
1000 2,058,000 91,000 22×

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

  • All existing TestBind* tests pass
  • go build ./... clean
  • Benchmarks confirm O(N) scaling

🤖 Generated with Claude Code

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>
@krleonid krleonid force-pushed the fix-bind-on2-lookup branch from 71ad1c8 to 9545c30 Compare June 2, 2026 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant