Eliminate CGO overhead in row scanning hot path#141
Merged
taniabogatsch merged 2 commits intoMay 28, 2026
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>
taniabogatsch
approved these changes
May 28, 2026
Member
taniabogatsch
left a comment
There was a problem hiding this comment.
LGTM - just had two nits/notes on comments.
- Use "entry" instead of "word" in getNull comments to match DuckDB convention - Add note about INLINE_LENGTH not being exposed in the C API 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):
Why this matters
Each CGO call pins a goroutine to an OS thread and creates a preemption point. Under CPU contention (70-90%), the OS scheduler delays resumption at each crossing. With 500K crossings per query, even rare preemption events accumulate into multi-second scan stalls in production (observed 1-3s spikes on 32-core pods at 70%+ CPU).
After this change, the scan loop is pure Go — no thread pinning, no preemption points between cells. The goroutine runs uninterrupted through all 250K cells per query.
Changes
vector_getters.go:getNull— inline bit manipulation instead ofmapping.ValidityMaskValueIsValidCGO call. The validity mask is auint64bitfield already accessible viavec.maskPtr; we just read and bit-test it directly.vector_getters.go:getBytes— readduckdb_string_tlayout directly (uint32 length at offset 0, inlined data at offset 4 for short strings, pointer at offset 8 for long strings) instead ofmapping.StringTDatawhich made 2 CGO calls (C.duckdb_string_t_length+C.duckdb_string_t_data) plusC.GoBytes.rows.go:Next()— callgetFndirectly on the column vector, skippingGetValue/verifyAndRewriteColIdxbounds checking (indices are always valid in the rows path).Safety
getBytesusesstrings.Cloneto copy string data to the Go heap before the chunk can be freed (same guarantee as before).duckdb_string_tis 16-byte aligned in DuckDB column storage. Reads at offset 0 (uint32) and offset 8 (pointer) are naturally aligned.duckdb_go_bindings_is_validimplementation exactly (mask[index/64] & (1 << index%64)).Test plan
go test ./...passes (full test suite including TestTypes, TestBlob, TestBit, TestList, TestMap, TestEnumNullValues, TestEmbeddedNulls, etc.)🤖 Generated with Claude Code