perf(iterator): pre-seed waste pool with embedded Item slots#2294
Open
shaunpatterson wants to merge 1 commit into
Open
perf(iterator): pre-seed waste pool with embedded Item slots#2294shaunpatterson wants to merge 1 commit into
shaunpatterson wants to merge 1 commit into
Conversation
Iterator.prefetch() with PrefetchValues=false uses prefetchSize=2, so the first two newItem() calls on a fresh iterator always heap-allocate before the per-iterator `waste` recycling kicks in. For workloads like dgraph posting-list rollup (NewKeyIterator, AllVersions=true, PrefetchValues=false) that re-create iterators per posting list, this is 2 allocs/op of pure churn. Embed [2]Item directly in the Iterator struct and push them onto the waste pool at construction. The first two newItem() pops now return these embedded slots; only iterators that demand more items (PrefetchValues=true with PrefetchSize>2) fall back to allocating. Iterator is already only used through *Iterator and never copied by value, so the sync.WaitGroup inside Item is safe to embed. txn is assigned to each prefilled item to mirror what newItem does for heap-allocated items. BenchmarkRollupKeyIterator (Apple M4 Max): before: 829.5 ns/op 770 B/op 13 allocs/op after: ~800 ns/op 754 B/op 11 allocs/op 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
Iterator.prefetch()withPrefetchValues=falseusesprefetchSize=2, so the first twonewItem()calls on a fresh iterator always heap-allocate before the per-iteratorwasterecycling kicks in. For workloads like dgraph posting-list rollup (NewKeyIterator,AllVersions=true,PrefetchValues=false) that re-create iterators per posting list, this is 2 allocs/op of pure churn.Embed
[2]Itemdirectly in theIteratorstruct and push them onto the waste pool at construction. The first twonewItem()pops now return these embedded slots; only iterators that demand more items (PrefetchValues=truewithPrefetchSize>2) fall back to allocating.Safety
Iteratoris already only used through*Iteratorand never copied by value, so thesync.WaitGroupinsideItemis safe to embed.txnis assigned to each prefilled item to mirror whatnewItemdoes for heap-allocated items.Benchmark — BenchmarkRollupKeyIterator (Apple M4 Max)
Test plan
go test ./...passes🤖 Generated with Claude Code