Repository navigation
fix(estimated-costs)!: size sum items as the apply does in average-case estimates (GROVE_V4) - #1019
Conversation
…se 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). average_case_merk_insert_element sized it by its serialized size instead, which encodes the sum as a varint, and average_case_merk_replace_element did the same for an ItemWithSumItem. The estimate then came in below the applied bytes by up to 8 per item: a SumItem(-3) inserted with its sum tree was estimated at 280 added bytes and applied at 288. From GROVE_V4 both size a sum-bearing item by the specialized cost, through the new operations.average_case.average_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. BREAKING CHANGE: GroveDBOperationsAverageCaseVersions gains the public field average_case_sum_item_cost_size. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to No identified issue prevents merging after normal checks. Pre-merge checks |
|
|
Waiting for bot review — coderabbitai ✓ · thepastaclaw not yet. Wait for the missing reviews, or a writer can post |
|
🕓 Review not started yet because the new head is waiting for the 30-minute push debounce.
Commit dd5fb6c. Normal review starts when eligible; priority review starts as soon as a slot is available. |
Codecov Report❌ Patch coverage is ❌ Your patch check has failed because the patch coverage (88.04%) 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 #1019 +/- ##
===========================================
- Coverage 93.22% 93.21% -0.01%
===========================================
Files 340 346 +6
Lines 107777 108102 +325
===========================================
+ Hits 100475 100771 +296
- Misses 7302 7331 +29
🚀 New features to boost your workflow:
|
|
Reviewed |
… estimates (GROVE_V4) (#1020) 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]>
Basic explanation
What this does: GroveDB can estimate what a batch of writes will cost without running it. A sum item is always stored as if its number took 9 bytes, whatever its value. The estimate instead counted the number at its compact size (1 byte for a small value), so it missed up to 8 bytes for each sum item it wrote. From
GROVE_V4, the estimate counts a sum item the way it is stored. Older grove versions estimate exactly as before.Value: Dash Platform uses this estimate as the fee bound a write is checked against before it is applied, so the estimate must be at least the real cost. Platform writes sum items for identity balances, token balances, summable count counters and summable index-only entries. Each one a batch inserted was estimated up to 8 bytes (216,000 credits of storage fee) below its real cost, and so was each replaced
ItemWithSumItem. Replacing aSumItem, such as a balance update, was already estimated correctly.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, 0 for a value that needs all 9) for each insertedSumItemorItemWithSumItem, and by the same amount in replaced bytes for each replacedItemWithSumItem. Applied costs, stored data and proofs do not change. Code that buildsGroveDBOperationsAverageCaseVersionsby 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 average-case estimators sized the same elements differently:average_case_merk_insert_elementsized every element that is not a tree byserialized_size, which encodes the sum as a varint.SumItem(-3)serializes to 3 bytes and is applied at 11.average_case_merk_replace_elementalready sized aSumItembySUM_ITEM_COST_SIZE, but anItemWithSumItemfell through toserialized_size.This was noted as out of scope in #1018. One batch on an empty db creating a sum tree with one child,
GroveVersion::latest(),added_bytes:What was done?
GroveDBOperationsAverageCaseVersions::average_case_sum_item_cost_size:0inGROVE_V1..GROVE_V3,1inGROVE_V4. Any other value is aVersionError.average_case_merk_insert_elementandaverage_case_merk_replace_elementmove intomod.rs/v0.rs/v1.rsdispatch modules undergrovedb/src/estimated_costs/average_case_costs/. Each dispatcher keeps the function's existing version check and then matches on the new slot.v0is the previous body unchanged.v1: an element that is not a tree is sized byspecialized_value_defined_costwhen it has one (a sum-bearing item, wrapped or not), else byserialized_sizeas before. This is the same choice the apply makes ininsert_into_batch_operations.v1: anItemWithSumItemis sized byspecialized_value_defined_cost. Every other arm is unchanged, including theSumItemarm, which already matched the apply.ReferenceWithSumItemand the backward-reference sum variants are unaffected: the apply sizes them by their serialized length, and they have no specialized cost.CHANGELOG.mdentry and aGROVE_V4module doc entry.Not in this PR:
worst_case_merk_insert_elementsizes sum items byserialized_size;worst_case_merk_replace_elementdoes so forItemWithSumItem). It is a separate change with its own slot.average_case_merk_insert_elementis sized without its wrapper byte) is in the same function but is a tree-sizing question; this PR leaves the tree arm as it was.How Has This Been Tested?
New tests in
grovedb/src/batch/estimated_costs/average_case_costs.rs, next to #1018's, each over six children:SumItem,SumItemwith flags,ItemWithSumItem,ItemWithSumItemwith flags (under a sum tree), andNonCountedSumItemandItemWithSumItem(under a count-sum tree):test_batch_sum_bearing_item_created_with_its_tree_average_case_added_bytes_match_actual: one batch creating the tree with the child; the latest estimate'sadded_bytesequal the applied ones. With theGROVE_V4slot at 0 it fails (280 vs 288 for the first child).test_batch_sum_bearing_item_replace_average_case_replaced_bytes_track_actual: a batch replacing the child with itself. A replace estimate over-counts its propagation, so its total is above the applied one; the test checks the element's own share: replacing the sum-bearing child instead of a plain item must raise the estimate'sreplaced_bytesby as much as it raises the applied ones, and the estimate must cover the applied cost. With the slot at 0 it fails forItemWithSumItem(+1 vs +9).test_batch_sum_bearing_item_average_case_pinned_before_v4:GROVE_V3insert and replace estimates for all six children, pinned to the figures measured before this change.test_average_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 to the estimate tests (sum_bearing_item,average_case,estimated_costs,aggregate_tree_created): 166 passed.v0bodies were checked to be byte-identical to the removed functions after their version check.Breaking Changes
GroveDBOperationsAverageCaseVersionsgains a public field,average_case_sum_item_cost_size, so code that builds the struct by hand must set it. Estimates underGROVE_V1..GROVE_V3do not change;GROVE_V4average-case estimates rise as described above.Checklist:
For repository code-owners and collaborators only
🤖 Generated with Claude Code
PR Hygiene ·
dd5fb6c/skip-botsproceeds without the ones not yet reported/self-reviewedWhen every merge requirement is met, the
PR Hygienecheck passes. Reviewer limits do not block merging; other required GitHub checks and protections still apply.Summary by CodeRabbit