Skip to content

perf(storage): prototype compact PieceV2 storage - #292

Draft
Kubuxu wants to merge 16 commits into
mainfrom
feat/optimized-add-pieces-2
Draft

perf(storage): prototype compact PieceV2 storage#292
Kubuxu wants to merge 16 commits into
mainfrom
feat/optimized-add-pieces-2

Conversation

@Kubuxu

@Kubuxu Kubuxu commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Summary

Draft implementation of the compact two-slot PieceV2 storage prototype, related to #286.

New compact-state datasets store pieces in a contiguous per-dataset array:

struct PieceV2 {
    bytes32 root;
    uint256 metadata;
}

metadata packs padding, tree height, leaf count, and the Fenwick partial sum into one storage slot.

Changes

  • Hardened canonical PieceCIDv2 validation and decoded (padding, height, root) in one pass.
  • Added compact PieceV2 storage while retaining the existing declarations for physical layout safety.
  • Moved piece addition, proof verification, lookup, getters, pagination, CID search, scheduling, removal, and cleanup to compact storage.
  • Preserved piece IDs, public ABI behavior, events, and listener callback arguments for compact-state datasets.
  • Added coverage for packing bounds, Fenwick semantics, proofs, pagination, removal, cleanup, and raw storage reclamation.
  • Measured steady-state addPieces storage activity for 1, 4, 16, and 32-piece batches.

Storage measurements

Each scenario creates a fresh dataset, adds one seed piece before recording, then records the measured addPieces call.

Batch EVM-slot reads EVM-slot writes KAMT touched KAMT modified Newly occupied slots
1 10 → 12 7 → 4 11 → 7 6 → 3 5 → 2
4 21 → 25 28 → 13 24 → 7 19 → 3 20 → 8
16 69 → 85 112 → 49 72 → 8 67 → 4 80 → 32
32 133 → 165 224 → 97 137 → 9 132 → 5 160 → 64

Values are legacy → compact.

Findings

  • Storage footprint: consistently 60% lower5N → 2N newly occupied slots.
  • EVM-slot writes: reduced from 7N to 3N + 1; reduction grows from 42.9% at one piece to 56.7% at 32 pieces.
  • KAMT locality: compact contiguous records substantially reduce object activity as batches grow:
    • 4 pieces: 24 → 7 touched, 19 → 3 modified.
    • 16 pieces: 72 → 8 touched, 67 → 4 modified.
    • 32 pieces: 137 → 9 touched, 132 → 5 modified.
  • EVM-slot reads: increase by 19.0–24.1%. These are repeated reads of already-accessed values, which are cached; they do not correspond to a comparable increase in KAMT object activity.
  • Scaling: the compact layout modifies two shared objects plus roughly one compact-data object per 16 pieces, subject to alignment. The legacy representation modifies approximately four piece-data objects per piece.

At 32 pieces, the compact layout occupies 64 slots instead of 160, touches 9 KAMT objects instead of 137, and modifies 5 instead of 132.

Compatibility note

This is a forward-only prototype. Backwards compatibility for datasets created before the compact representation was not considered.

Keep this PR as draft; it is not ready to merge or deploy as an upgrade.

Verification

make test
209 passed, 0 failed

@FilOzzy FilOzzy added this to FOC Jul 28, 2026
@github-project-automation github-project-automation Bot moved this to 📌 Triage in FOC Jul 28, 2026
@Kubuxu Kubuxu self-assigned this Jul 28, 2026
Kubuxu added 12 commits July 29, 2026 15:01
Record storage reads, writes, KAMT object locality, and persistent slot growth for additions to an existing data set. This establishes the baseline for the compact piece representation work.

Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
Document the compact PieceV2 design and split the prototype into sequential, agent-ready implementation digests with explicit invariants, acceptance criteria, verification, and handoff requirements.

Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
Reject ambiguous and malformed CID encodings before they can be stored or used for proof verification.

Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
Append the two-slot compact piece mapping and centralize bounded metadata packing before production paths adopt the new storage.

Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
Store newly added pieces in compact slots with their final Fenwick sums, while batching the dataset leaf-count update.\n\nThis removes legacy addition writes ahead of migrating readers and removers.

Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
Serve piece getters, pagination, CID search, and deletion scheduling from compact metadata while preserving canonical CID output and mapping-style getter defaults.

Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
Adapt behavioral and raw-storage tests to compact piece records, including Fenwick sums and cleanup reclamation.

Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
Run generation after layout cleanup so make cannot evaluate generated targets before their files are removed.

Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
Measure isolated 1, 4, 16, and 32-piece additions and lock the observed storage activity. Document the compact baseline, slot reductions, and KAMT alignment behavior.

Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
Keep the consolidated measurement comparison and align the four-piece KAMT baseline with the revised measurement method.

Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
@Kubuxu
Kubuxu force-pushed the feat/optimized-add-pieces-2 branch from 063dea8 to 1145d1f Compare July 29, 2026 13:13
Add deterministic storage population for reproducing the deployed verifier's large state tree during Filecoin gas benchmarks.

Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
@rjan90 rjan90 moved this from 📌 Triage to ⌨️ In Progress in FOC Jul 29, 2026
Kubuxu added 2 commits July 30, 2026 15:08
Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
…-add-pieces-2

Signed-off-by: Jakub Sztandera <oss@kubuxu.com>

# Conflicts:
#	src/PDPVerifier.sol
#	src/PDPVerifierLayout.json
#	src/PDPVerifierLayout.sol
Keep compact-storage coverage while preserving the established sum-tree names in tests.

Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
@Kubuxu

Kubuxu commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator Author

Benchmark results:

Optimised gas reduction versus baseline:

Ballast slots Batch 1 2 4 8 16 32
0 9.9% 9.1% 27.5% 33.1% 36.2% 38.1%
1M 40.4% 57.4% 72.8% 82.3% 87.0% 89.5%
5M 40.3% 60.7% 73.6% 83.5% 88.3% 91.1%

Flat gas used

Ballast slots Version Batch 1 2 4 8 16 32
0 Baseline 17,217,928 22,999,322 42,969,779 64,817,453 112,656,392 205,053,901
0 Optimized 15,504,996 20,909,427 31,155,721 43,346,504 71,885,266 126,894,938
1M Baseline 135,895,549 200,896,826 341,493,403 592,579,677 989,314,920 1,817,386,096
1M Optimized 80,992,902 85,593,374 92,775,205 105,070,523 128,651,224 190,157,808
5M Baseline 157,548,595 249,842,564 389,163,815 711,535,675 1,251,005,462 2,259,544,725
5M Optimized 94,115,959 98,222,892 102,830,002 117,059,589 146,507,177 200,297,037

The optimisation largely removes ballast sensitivity: at 5M slots, batch 32 falls from 2.260B to 200.3M gas (91.1%, or 11.3× lower). Batch 64 is excluded because both versions revert at the Lotus event-size limit.

Benchmarks were performed in foc-devnet, ballasting down the PDPVerifier tree with the added dev-only balast(start, n) method creating sputtering of occupied slots.

@@ -0,0 +1,359 @@
## Recommendation

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I plan to remove this doc. It was generated as part of LLM task guidance.

@Kubuxu

Kubuxu commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator Author

@rvagg @wjmelements I would appreciate an early review. I don't expect the logic to change much, but it will get way messier when I make this backwards compatible.

@rvagg

rvagg commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Batch 64 is excluded because both versions revert at the Lotus event-size limit.

But I think we can fix that here can't we because (a) we're emitting those events with uncapped arrays of piece CIDs and (b) the piece CID we're emitting are the larger legacy format, not the compact 64-byte format; so we could go quite high, at least for the PDPVerifier component (FWSS will impose its own limits), right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: ⌨️ In Progress
Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants