perf(table): fuse 3 allocate calls in Builder.addHelper into 1#2280
Open
shaunpatterson wants to merge 1 commit into
Open
perf(table): fuse 3 allocate calls in Builder.addHelper into 1#2280shaunpatterson wants to merge 1 commit into
shaunpatterson wants to merge 1 commit into
Conversation
addHelper is the write hot-path: called once per entry during table build. The original code did three separate writes — append(h.Encode()), append(diffKey), and allocate+Encode for the value — which costs: - 3 capacity checks against b.curBlock.data - 1 heap-escaping local [4]byte from h.Encode() per entry - 1 intermediate copy of the header bytes Fuse them into a single allocate(4 + dlen + vsz) and write the header, diffKey, and value directly into the slice. The header is written via the same unsafe pointer cast pattern already used elsewhere in the package (matches header.Encode's byte layout). Benchmark impact (M4 Max, median of 3 runs): - BenchmarkBuilder/zstd_compression/level_1: -13.1% - BenchmarkBuilder/no_compression: -9.4% - BenchmarkBuilder/encryption: -7.6%
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
Replaces the three separate
b.allocate()calls inBuilder.addHelper(header, diff-key, value) with a single allocation sized for all three pieces, then writes each piece directly into the slot. The fused write uses*(*header)(unsafe.Pointer(&dst[0])) = hfor the header, eliminating the heap-escapingh.Encode()local and two intermediate capacity checks per entry.Each
addHelpercall previously paid:allocate(one per piece)headerslice produced byh.Encode()After the fuse, that's one capacity check, no heap escape.
Measurement
BenchmarkBuilder/no_compression: -12.5%BenchmarkBuilder/zstd_level_1: -13.1%BenchmarkBuilder/encryption: -7.6%ns/op, median of 3 runsTest plan
go test -short -race ./table/— all builder tests pass (TestTableIndex,TestBuilder*,TestTableIterator, ...)go vet ./...addHelperis on the hot path of every table build, so existing tests give 100% line coverage of the modified function.🤖 Generated with Claude Code