perf(table): reuse RowScanner across Iter rows - #57
Merged
Conversation
Extract the Iter scan loop into iterRows so it can be driven directly, and add a DB-independent benchmark using an in-memory fake pgx.Rows. This measures the per-row scan cost of the current implementation, which creates a fresh scany RowScanner (recomputing the column-to-field mapping) on every row via pgxscan.API.ScanRow. Serves as the baseline for the scanner-reuse optimization in the next commit. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Iter created a fresh scany RowScanner per row via pgxscan.API.ScanRow,
recomputing the column-to-field mapping on every iteration. Create one
RowScanner before the loop and reuse it so the reflection work happens
once per query instead of once per row.
Benchmarked via BenchmarkTableIter (in-memory fake pgx.Rows), count=8:
rows=1000 time 394.8µs -> 177.5µs (-55%)
B/op 406.6Ki -> 63.2Ki (-84%)
allocs 6002 -> 1008 (-83%, ~6/row -> ~1/row)
All deltas p=0.000. GetAll/List/pagination already reuse a single
RowScanner internally, so this only affects the Iter streaming path.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
klaidliadon
approved these changes
Jul 31, 2026
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.
What
Table.Itercreated a fresh scanyRowScanneron every row viapgxscan.API.ScanRow, recomputing the column-to-field mapping each iteration. This creates oneRowScannerbefore the loop and reuses it, so the reflection work happens once per query instead of once per row.Motivated by this write-up on
pgx.CollectRows— the same per-row reflection anti-pattern it describes was present inIter.GetAll/List/pagination already reuse a singleRowScannerinternally, so this only affects theIterstreaming path.Commits
test(table)— extracts theIterscan loop intoiterRowsand addsBenchmarkTableIter, driven by an in-memory fakepgx.Rows(no DB needed, runs in CI). Baseline.perf(table)— the one-line-idea change: reuse theRowScanner.Result
BenchmarkTableItervia benchstat, count=8, all deltasp=0.000:Testing
go test ./tests/...(Postgres-backed) passes, includingTestIterandTestTable.go build ./...andgo vet ./clean.🤖 Generated with Claude Code