Repository navigation
fix(estimated-costs)!: size sum items as the apply does in worst-case estimates (GROVE_V4) - #1020
Conversation
… estimates (GROVE_V4) The apply writes a SumItem or ItemWithSumItem (wrapped or not) with its specialized cost, which charges the sum value at its fixed 9-byte width (SUM_ITEM_COST_SIZE) and counts a wrapper's byte. worst_case_merk_insert_element sized it by its serialized size instead, which encodes the sum as a varint; worst_case_merk_replace_element did the same for an ItemWithSumItem and left out a wrapped SumItem's wrapper byte. The worst case then came in below the applied bytes by up to 8 per item and was not an upper bound: a SumItem(-3) inserted into a normal tree was estimated at 142 added bytes and applied at 150. From GROVE_V4 both size a sum-bearing item by the specialized cost, through the new operations.worst_case.worst_case_sum_item_cost_size slot (0 in V1..V3, 1 in V4). The two estimators move into mod.rs/v0.rs/v1.rs dispatch modules; v0 is the previous body unchanged, so older grove versions estimate exactly as before. This is the worst-case twin of #1019. BREAKING CHANGE: GroveDBOperationsWorstCaseVersions gains the public field worst_case_sum_item_cost_size. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configuration
📒 Files selected for processing (14)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
🕓 Review not started yet because the new head is waiting for the 30-minute push debounce.
Commit f7c524b. Normal review starts when eligible; priority review starts as soon as a slot is available. |
Codecov Report❌ Patch coverage is ❌ Your patch status has failed because the patch coverage (86.96%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## develop #1020 +/- ##
===========================================
- Coverage 93.21% 93.20% -0.02%
===========================================
Files 346 352 +6
Lines 108102 108428 +326
===========================================
+ Hits 100771 101062 +291
- Misses 7331 7366 +35
🚀 New features to boost your workflow:
|
…tinuation-dd8070 develop's #1021 landed this branch's two GROVE_V4 fixes (same-batch reference prediction and the Unchanged re-measure) with later review fixes of its own, so those now exist once, in develop's form. What this branch adds over develop is the settlement mode: - the prediction keeps develop's runs_value_update and post-apply check; a settling batch uses version 1 on every grove version - CHANGELOG: the fixes stay under develop's entries in 6.1.0, and the settlement entries move to Unreleased - same_batch_reference_prediction_tests is develop's file with the settling modes added, its helpers shared with settle_test_support - the option-off estimate pins are retaken from develop at 0ea327d: 18 of the 66 latest-version rows moved with #1018..#1020 and none of the 66 grove v3 rows did - a settled tree's lookup comment says why it is still needed after #1018 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Basic explanation
What this does: GroveDB can also estimate the worst-case cost of a batch of writes, which is meant to be an upper bound. A sum item is always stored as if its number took 9 bytes, whatever its value. The worst-case estimate counted the number at its compact size (1 byte for a small value), so it came in up to 8 bytes below the real cost for each sum item. From
GROVE_V4, the worst case counts a sum item the way it is stored. Older grove versions estimate exactly as before. #1019 made the same correction to the average-case estimate.Value: A worst-case estimate below the real cost is not a worst case. Code that relies on it as an upper bound, such as a fee bound checked before a write is applied, could under-reserve by up to 8 bytes (216,000 credits of storage fee on Dash Platform) per sum item.
Risks: Low. Only
GROVE_V4estimates change, and they only go up: by 9 bytes less the sum value's varint width (8 for a small value) for each insertedSumItemorItemWithSumItem, by the same amount for each replacedItemWithSumItem, and by 1 byte for each replaced wrappedSumItem. Applied costs, stored data and proofs do not change. Code that buildsGroveDBOperationsWorstCaseVersionsby hand must set one new field.Issue being fixed or feature implemented
The apply writes a
SumItemorItemWithSumItem(wrapped or not) withOp::PutWithSpecializedCost, sized byElement::specialized_value_defined_cost:SUM_ITEM_COST_SIZE(11: the sum at its fixed 9-byte width, the type byte, the flags option byte), plus the flags, plus the item bytes and their length for anItemWithSumItem, plus one byte for a wrapper. The worst-case estimators sized the same elements differently:worst_case_merk_insert_elementsized every element that is not one of its four sized tree types byserialized_size, which encodes the sum as a varint.SumItem(-3)serializes to 3 bytes and is applied at 11.worst_case_merk_replace_elementsized aSumItembySUM_ITEM_COST_SIZEplus flags but without a wrapper's byte, and anItemWithSumItembyserialized_size.One batch inserting the item into the root (a normal tree) of an empty db,
GroveVersion::latest(),added_bytes:Replacing the only child of a count-sum tree with itself, bytes written (added plus replaced) above replacing a plain item of the same wrapping:
What was done?
GroveDBOperationsWorstCaseVersions::worst_case_sum_item_cost_size:0inGROVE_V1..GROVE_V3,1inGROVE_V4. Any other value is aVersionError.worst_case_merk_insert_elementandworst_case_merk_replace_elementmove intomod.rs/v0.rs/v1.rsdispatch modules undergrovedb/src/estimated_costs/worst_case_costs/, the same layout fix(estimated-costs)!: size sum items as the apply does in average-case estimates (GROVE_V4) #1019 used for the average-case pair. Each dispatcher keeps the function's existing version check and then matches on the new slot.v0is the previous body unchanged (checked byte for byte).v1: an element outside the sized tree arm is sized byspecialized_value_defined_costwhen it has one, else byserialized_sizeas before.v1: aSumItemand anItemWithSumItemare sized byspecialized_value_defined_cost. The arms keep their shapes: aSumItemis still a same-size replace, and anItemWithSumItemis still charged as a replace that may grow.CHANGELOG.mdentry and aGROVE_V4module doc entry.Not in this PR, both seen while writing the tests:
TreeType::NormalTreeas the parent tree type for every op (WorstCaseTreeCacheKnownPaths::execute_ops_on_path), so a child of a sum or count tree is not charged its node's aggregate bytes. A plain item inserted under a count-sum tree is estimated 32 bytes below its appliedadded_bytes. The tests for wrapped items, which only go under count trees, compare deltas against a plain item for that reason.SumItemreplace arm assumes the replaced element had the same size. Replacing a plainItem(b"")with aSumItemis estimated at 0 added bytes, while the apply adds 8.How Has This Been Tested?
New tests in
grovedb/src/batch/estimated_costs/worst_case_costs.rs, overSumItem,SumItemwith flags,ItemWithSumItem,ItemWithSumItemwith flags, andNonCountedSumItemandItemWithSumItem:test_batch_sum_bearing_item_insert_worst_case_covers_actual: the four unwrapped items inserted into a normal tree; the latest worst case'sadded_bytesequal the applied ones and the whole worst case covers the applied cost (worse_or_eq_than). With theGROVE_V4slot at 0 it fails (142 vs 150 for the first item).test_batch_sum_bearing_item_worst_case_sizing_tracks_actual: all six items under a count-sum tree, inserted and replaced; writing the sum-bearing item rather than a plain one of the same wrapping raises the worst case by what it raises the apply. With the slot at 0 it fails (-2 vs +6 for the first item).test_batch_sum_bearing_item_worst_case_pinned_before_v4:GROVE_V3figures for every scenario above, pinned to the values measured before this change.test_worst_case_sum_item_cost_size_unknown_version_is_refused: slot value 9 is aVersionErrorfrom both the insert and the replace dispatcher.Runs:
cargo test -p grovedb --libfiltered tosum_bearing_item,worst_case_sum_item_cost_sizeandworst_case: 108 passed.cargo test -p grovedb-version: 56 passed.cargo clippy -p grovedb -- -D warningsover every feature exceptgrovedbg(its build script downloads a release artifact, which timed out locally) andcargo fmt --all -- --checkare clean.Breaking Changes
GroveDBOperationsWorstCaseVersionsgains a public field,worst_case_sum_item_cost_size, so code that builds the struct by hand must set it. Estimates underGROVE_V1..GROVE_V3do not change;GROVE_V4worst-case estimates rise as described above.Checklist:
For repository code-owners and collaborators only
🤖 Generated with Claude Code