perf(db): pre-size memtable slice in getMemTables#2289
Open
shaunpatterson wants to merge 1 commit into
Open
Conversation
The slice was previously grown via append-from-nil, triggering a growslice on every call (and additional growslices for each cap doubling boundary crossed). The final size is known up front: 1 mutable (unless ReadOnly) + len(db.imm) immutables. Pre-sizing collapses all growslice events into a single make(). Profiled in BenchmarkRollupKeyIterator: line 730's append-from-nil allocation (5.18M objects / ~4M iterations ≈ 1.3 allocs/op from growslice) is eliminated; in its place is a single make() allocation matching the final size. In real dgraph workloads with 6 memtables (1 mutable + 5 immutable), the original code grew the slice through 4 cap boundaries (0→1→2→4→8), so this saves 4 allocs per iterator construction. Each rollup cache miss constructs one iterator. Behavior is preserved: the only observable difference is that an empty result returns a non-nil zero-length slice rather than nil. All three callers (db.get, txn.NewIterator, stream_writer) use len() or range, neither of which distinguishes the two. 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
The memtable slice returned by
getMemTableswas previously grown via append-from-nil, triggering agrowsliceon every call (and additional growslices for each cap doubling boundary crossed). The final size is known up front: 1 mutable (unlessReadOnly) +len(db.imm)immutables. Pre-sizing collapses allgrowsliceevents into a singlemake().Motivation
Profiled in
BenchmarkRollupKeyIterator: the original append-from-nil allocation (~1.3 allocs/op from growslice) is eliminated; in its place is a singlemake()matching the final size.In real dgraph workloads with 6 memtables (1 mutable + 5 immutable), the original code grew the slice through 4 cap boundaries (0→1→2→4→8), so this saves 4 allocs per iterator construction. Each rollup cache miss constructs one iterator.
Behavior
Preserved. The only observable difference is that an empty result returns a non-nil zero-length slice rather than
nil. All three callers (db.get,txn.NewIterator,stream_writer) uselen()orrange, neither of which distinguishes the two.Test plan
go test ./...passes🤖 Generated with Claude Code