perf(table): fuse 3 allocate calls in Builder.addHelper into 1#2280
Open
shaunpatterson wants to merge 2 commits into
Open
perf(table): fuse 3 allocate calls in Builder.addHelper into 1#2280shaunpatterson wants to merge 2 commits into
shaunpatterson wants to merge 2 commits 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%
The fused-allocate optimization wrote the block entry header via `*(*header)(unsafe.Pointer(&dst[0])) = h`, where dst is a sub-slice of the block buffer at an arbitrary (frequently unaligned) offset. Unlike header.Encode(), whose make([]byte, headerSize) backing array is allocator-aligned, this typed store targets an address with no alignment guarantee. That violates the unsafe alignment rules and can fault (SIGBUS) or trap into slow emulation on strict-alignment architectures (32-bit arm, mips, ppc), even though it happens to work on amd64/arm64. Write the two uint16 header fields with binary.NativeEndian.PutUint16 instead. This reproduces the exact native-endian byte layout that header.Encode/Decode rely on (so the on-disk format is byte-identical and round-trips through the unsafe Decode unchanged), is safe at any offset, and keeps the single-allocate win (no heap-escaping h.Encode() buffer, one capacity check). Also replaces the hardcoded 4 with the existing headerSize constant. Co-Authored-By: Claude Opus 4.8 (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
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