Eliminate CGO overhead in row scanning hot path#1
Merged
Conversation
Replace per-cell CGO calls in getNull and getBytes with pure Go pointer arithmetic, reducing ~500K CGO boundary crossings per 1000-row × 250-column query to zero. Bypass verifyAndRewriteColIdx in rows.Next by calling getFn directly. Measured improvement (single query, 1000 rows × 250 cols): - Scan time: 16.4ms → 8.1ms (2× faster) - Under 90% CPU pressure: max scan 1.2s → 0.9s Changes: - vector_getters.go: getNull uses inline bit manipulation instead of mapping.ValidityMaskValueIsValid CGO call - vector_getters.go: getBytes reads duckdb_string_t layout directly instead of mapping.StringTData (2 CGO calls + C.GoBytes) - rows.go: Next() calls getFn directly, skipping GetValue/verifyAndRewriteColIdx Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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
getNullandgetByteswith pure Go pointer arithmetic, reducing ~500K CGO boundary crossings per 1000-row × 250-column query to zeroverifyAndRewriteColIdxinrows.Nextby callinggetFndirectly (no bounds/projection check needed on the read path)Measured improvement
Single query, 1000 rows × 250 columns, no concurrency:
Under 90% CPU pressure (10 concurrent workers):
Changes
vector_getters.go:getNull— inline bit manipulation instead ofmapping.ValidityMaskValueIsValidCGO callvector_getters.go:getBytes— readduckdb_string_tlayout directly instead ofmapping.StringTData(2 CGO calls +C.GoBytescopy)rows.go:Next()— callgetFndirectly, skippingGetValue/verifyAndRewriteColIdxTest plan
go test ./...passes (full test suite)concurrent_select_stormbenchmark under CPU pressureduckdb-fetch-benchfrom mdb-engine to compare per-row Next/Scan times🤖 Generated with Claude Code