Repository navigation
perf: extract value_length off the wire in size accounting - #207
Merged
Merged
Conversation
existingValueLength (the Put-overwrite delta) and calculateTotalSize (the cleaner's startup + hourly total-size scan) both consume only ValueMessage.value_length, yet each fully proto.Unmarshal'd the row — allocating and copying the inline Data payload (up to the 64 KiB inline threshold) that neither caller reads. Add a shared protowire extractor, valueMessageValueLength, that scans and validates the whole message but reads only field 7, skipping Data without materializing it. It preserves the callers' existing semantics: an empty message yields (0, true), a repeated field 7 takes the last value (proto scalar last-wins), and a structurally malformed record yields ok=false (the same "return 0 / skip row" fallback proto.Unmarshal errors took). For a 64 KiB inline row this turns the per-row decode from 6331 ns / 2 allocs / 65,680 B into 13 ns / 0 allocs / 0 B, cutting allocation churn on the metadata scan for inline-heavy caches. Raw-file/segment rows (the large-object workload) carry no Data, so behavior there is unchanged. A differential test cross-checks the extractor against proto.Unmarshal across the ValueMessage shapes Put writes, plus duplicate-field and malformed inputs. make test-storage passes. Surfaced by Perfloop cases case_2j2veg0hs5 and case_0pe7dc8ta7. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01YCoN4Him5ZY4hZovVp37ra Signed-off-by: Ovais Tariq <[email protected]>
Greptile SummaryThis PR optimizes logical-size accounting by extracting
Confidence Score: 5/5The PR appears safe to merge, with no concrete changed-code failure identified. The new extractor matches the stored Important Files Changed
Reviews (1): Last reviewed commit: "perf: extract value_length off the wire ..." | Re-trigger Greptile |
|
🎉 This PR is included in version 1.10.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
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.
Problem
Two size-accounting paths consume only
ValueMessage.value_length, but each fullyproto.Unmarshal'd the metadata row — which allocates and copies the inlineDatapayload (up to the 64 KiB inline threshold) that neither caller reads:existingValueLength(storage/storage.go) — the Put-overwrite delta added in fix: subtract the replaced value's size when Put overwrites a key (#203) #205, one decode per overwrite of an inline key.calculateTotalSize(storage/cleaner.go) — the cleaner's startup scan and the hourly reconciliation scan (also added in fix: subtract the replaced value's size when Put overwrites a key (#203) #205), one decode per metadata row, every pass.On an inline-heavy cache this is a discarded payload copy + message allocation for every inline row scanned.
Fix
Add a shared
protowireextractor,valueMessageValueLength, that scans and validates the whole message but reads only field 7, skippingData(field 2) without materializing it. It preserves the callers' existing semantics exactly:(0, true);ok=false, which maps onto the same "return 0 / skip the row" fallback the callers already took on aproto.Unmarshalerror.Wire it into both sites.
Numbers
BenchmarkValueLength_Inline64KiB(64 KiB inline row):Raw-file and segment rows (the large-object workload) carry no
Data, so their behavior is unchanged — the win is specifically the allocation churn on inline-heavy metadata scans.Test
storage/value_wire_test.godifferential-tests the extractor againstproto.Unmarshalacross theValueMessageshapesPutwrites (inline/raw-file/segment/empty/all-fields), plus duplicate-field last-wins and malformed/truncated inputs. Fullmake test-storagepasses (incl. #205'sTestPut_Overwrite_TotalSizeStaysExactand the cleaner tests).Provenance
Surfaced by Perfloop frontier cases
case_2j2veg0hs5andcase_0pe7dc8ta7(patternavoidable_copy), confirmed against source before implementing.🤖 Generated with Claude Code
Note
Low Risk
Localized performance change with tests asserting parity with
proto.Unmarshal; size accounting semantics are preserved on success and error paths.Overview
Size accounting no longer fully unmarshals each metadata
ValueMessagewhen it only needsvalue_length. A sharedvalueMessageValueLengthhelper scans protobuf wire format withprotowire, reads field 7, and skips field 2 (Data) without copying—avoiding up to 64 KiB allocations per inline row.That helper replaces
proto.UnmarshalinexistingValueLength(Put overwrite deltas) andcalculateTotalSize(cleaner startup and hourly reconciliation scans). Malformed records still yieldok=false, mapping to the same return-0 / skip-row behavior as before.storage/value_wire_test.godifferential-tests the extractor against full decode (inline/raw/segment shapes, duplicate field last-wins, malformed input) plus a 64 KiB inline benchmark.Reviewed by Cursor Bugbot for commit e821e26. Bugbot is set up for automated code reviews on this repo. Configure here.