perf(iterator): lazy-allocate Item.slice#2292
Open
shaunpatterson wants to merge 1 commit into
Open
Conversation
newItem eagerly allocated a y.Slice wrapper per Item even though both users of item.slice (yieldItemValue and prefetchValue) already nil-check and lazy-init it before any access. Iterators that never read values (KeyOnly, or AllVersions=true with PrefetchValues=false — e.g. dgraph's posting list rollup walking ~8 versions per cache miss) were paying a 24-byte allocation per Item creation for a struct they never touched. Drop the eager new(y.Slice) and rely on the existing lazy-init. Add a regression test covering all three value-reading paths (PrefetchValues=true, synchronous Item.Value, ValueCopy) to ensure the lazy initialization stays correct. BenchmarkRollupKeyIterator (Apple M4 Max, 5×3s): Before: ~910 ns/op 873 B/op 18 allocs/op After: ~855 ns/op 825 B/op 16 allocs/op ~6% faster, 48 B/op saved, 2 fewer allocations per iterator setup. Co-Authored-By: Claude Opus 4.7 <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
newItemeagerly allocated ay.Slicewrapper perItemeven though both users ofitem.slice(yieldItemValueandprefetchValue) already nil-check and lazy-init it before any access. Iterators that never read values (KeyOnly, orAllVersions=truewithPrefetchValues=false— e.g. dgraph's posting list rollup walking ~8 versions per cache miss) were paying a 24-byte allocation perItemcreation for a struct they never touched.Drop the eager
new(y.Slice)and rely on the existing lazy-init. Add a regression test covering all three value-reading paths (PrefetchValues=true, synchronousItem.Value,ValueCopy) to ensure the lazy initialization stays correct.Benchmark — BenchmarkRollupKeyIterator (Apple M4 Max, 5×3s)
~6% faster, 48 B/op saved, 2 fewer allocations per iterator setup.
Test plan
go test ./...passesTestRegressionLazyItemSliceValueReadcovers all three value-read paths🤖 Generated with Claude Code