Skip to content

chore(trie): Use Vec<Option<...>> in HashedPostStateCursors#19487

Merged
mediocregopher merged 16 commits intomainfrom
mediocregopher/18848-0x00101010-rework-cursors
Nov 7, 2025
Merged

chore(trie): Use Vec<Option<...>> in HashedPostStateCursors#19487
mediocregopher merged 16 commits intomainfrom
mediocregopher/18848-0x00101010-rework-cursors

Conversation

@mediocregopher
Copy link
Member

@mediocregopher mediocregopher commented Nov 4, 2025

Fixes #18848

This continues the work done #19233. I've done a bit of cleanup and added more comprehensive proptests.

This PR modifies the in-memory representation of HashedPostStateSorted so that it only uses a single Vec for both updates and deletions, rather than a Vec for updates and HashSet for deletions. This allows the type to more closely map to how changesets are stored in the database, and will make it simpler to serialize/deserialize this data in general (e.g. for #19430).

Changing the in-memory representation of HashedPostStateSorted requires changing how the in-memory overlay for the database works. This was done in a manner essentially identical how InMemoryTrieCursor is implemented now, with a small complication that a trait is required to handle the two different value types of the hashed state tables (Account and U256).

@github-project-automation github-project-automation bot moved this to Backlog in Reth Tracker Nov 4, 2025
@github-actions github-actions bot added A-trie Related to Merkle Patricia Trie implementation C-enhancement New feature or request labels Nov 4, 2025
@mediocregopher mediocregopher changed the title chore(trie): Use Vec<Option<...>> in HashedPostStateCursors chore(trie): Use Vec<Option<...>> in HashedPostStateCursors Nov 4, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 4, 2025

CodSpeed Performance Report

Merging #19487 will improve performances by 12.05%

Comparing mediocregopher/18848-0x00101010-rework-cursors (9c68646) with main (5006d5f)

Summary

⚡ 2 improvements
✅ 75 untouched

Benchmarks breakdown

Benchmark BASE HEAD Change
hash builder[init size 10000 | update size 100 | num updates 10] 88.3 ms 79.9 ms +10.41%
hash builder[init size 10000 | update size 100 | num updates 5] 45.6 ms 40.7 ms +12.05%

@mediocregopher
Copy link
Member Author

Confirmed no impact on perf:
image

@mediocregopher mediocregopher marked this pull request as ready for review November 4, 2025 16:39
}
updated_accounts.sort_unstable_by_key(|(address, _)| *address);
let accounts = HashedAccountsSorted { accounts: updated_accounts, destroyed_accounts };
let mut accounts: Vec<_> = self.accounts.drain().collect();
Copy link
Member

Choose a reason for hiding this comment

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

nice simplification - instead of filtering accounts into two separate collections, then sort the valid ones we just drain and sort

Copy link
Member

@yongkangc yongkangc left a comment

Choose a reason for hiding this comment

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

lgtm overall, left some questions and nits

pub accounts: HashedAccountsSorted,
/// Map of hashed addresses to hashed storage.
/// Sorted collection of account updates. `None` indicates a destroyed account.
pub accounts: Vec<(B256, Option<Account>)>,
Copy link
Member

Choose a reason for hiding this comment

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

would a type alias be nicer / more intuitive here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Imo it's ok as-is... but open to it if others thing it would be easier to read. Which part were you thinking would be better as an alias?

assert!(!cursor.is_storage_empty().unwrap());
}

// all zero values, but not wiped
Copy link
Member

Choose a reason for hiding this comment

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

does this test catch implementations that would incorrectly treat zero values as empty storage?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is testing that given a single zero value in the overlay (which indicates the slot was deleted), is_storage_empty still returns false, since the db has other non-zero slots.

@github-project-automation github-project-automation bot moved this from Backlog to In Progress in Reth Tracker Nov 5, 2025
@mediocregopher mediocregopher added this pull request to the merge queue Nov 7, 2025
@mediocregopher mediocregopher removed this pull request from the merge queue due to a manual request Nov 7, 2025
@mediocregopher mediocregopher added this pull request to the merge queue Nov 7, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 7, 2025
@mediocregopher mediocregopher added this pull request to the merge queue Nov 7, 2025
Merged via the queue into main with commit 7faddba Nov 7, 2025
43 checks passed
@mediocregopher mediocregopher deleted the mediocregopher/18848-0x00101010-rework-cursors branch November 7, 2025 14:20
@github-project-automation github-project-automation bot moved this from In Progress to Done in Reth Tracker Nov 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-trie Related to Merkle Patricia Trie implementation C-enhancement New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Use Vec<(B256, Option<...>)> in HashedPostStateCursors

3 participants