perf(iterator): lazy-allocate Item.slice#2292
Open
shaunpatterson wants to merge 1 commit into
Open
Conversation
Replaces the lazy-init wrapper (a y.Slice field with nil-check + lazy
allocation) with the more direct approach: the field is gone entirely
and the two former Resize call sites use make([]byte, N) instead.
The motivating requirement was 'iterators that never call Value/ValueCopy
should not pay for a buffer wrapper they will never touch' (dgraph's
posting list rollup). With the field deleted, that requirement falls
out for free: the wrapper never exists at all.
Also drops the dead *y.Slice parameter from vlog.Read (it was named _
inside the function — the only caller passed item.slice solely to satisfy
the signature). Removes a misleading hint that the slice is 'used by
vlog.Read'.
iterator.go:
- Item struct: drop slice *y.Slice field
- yieldItemValue: drop lazy-init; use make([]byte, N) inline; call
vlog.Read without the dead param
- prefetchValue: use make([]byte, N) instead of item.slice.Resize
- newItem: drop the now-unneeded slice initialization
value.go:
- vlog.Read signature: drop the unused *y.Slice parameter
iterator_lazy_slice_test.go:
- Regression test (unchanged): exercises Value/ValueCopy on items
produced by the new path to ensure no nil-deref.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
537d9ac to
15d21de
Compare
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