perf(skl): embed Iterator by value in UniIterator#2288
Open
shaunpatterson wants to merge 1 commit into
Open
Conversation
499a5af to
2881ce3
Compare
Eliminates the UniIterator wrapper struct entirely. UniIterator existed
solely to wrap Iterator (which already implements every method of
y.Iterator) and dispatch Next/Seek/Rewind to Prev/SeekForPrev/SeekToLast
when a 'reversed' flag was set.
The reversed flag moves onto Iterator directly:
type Iterator struct {
list *Skiplist
n *node
reversed bool
}
Iterator.Next/Seek/Rewind dispatch to their backward counterparts when
reversed is true, so the same *Iterator satisfies y.Iterator in both
directions. NewUniIterator returns *Iterator (the function name is kept
for compatibility — it now does the IncrRef + struct construction
inlined, eliminating the UniIterator wrapper allocation).
Net: one less heap allocation per NewUniIterator call. Eliminates the
embed-by-value vs named-field footgun from the original PR (it goes
away — there is no longer a struct-of-Iterator; the Iterator IS the
unidirectional iterator when needed).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2881ce3 to
91cf0fd
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
Halves the per-memtable iterator construction allocation count by embedding the inner
IteratorinsideUniIteratorinstead of holding a*Iterator. PreviouslyNewUniIteratorallocated twice: once for the innerIterator(viaNewIterator) and once for theUniIteratorwrapper. Embedding by value collapses these into a single heap allocation.Extracts the shared
(*Iterator).inithelper so future setup added inNewIteratorapplies uniformly toUniIterator's embedded construction path.Motivation
In dgraph's posting list rollup, ~6
UniIterators are constructed per cache miss (1 mutable + 5 immutable memtables). This scales to roughly 6 fewer allocs per rollup miss.Benchmark — BenchmarkRollupKeyIterator
Test plan
go test ./skl/...passesgo test ./...passes🤖 Generated with Claude Code