Skip to content

Replace MutableMap with CAccountMap NIF for fast State.clone#37

Merged
dominicletz merged 1 commit into
masterfrom
caccount-map-nif-phase1
Jul 2, 2026
Merged

Replace MutableMap with CAccountMap NIF for fast State.clone#37
dominicletz merged 1 commit into
masterfrom
caccount-map-nif-phase1

Conversation

@dominicletz

Copy link
Copy Markdown
Member

Summary

  • Add a native account map (CAccountMap) in merkletree_nif.so with COW semantics matching CMerkleTree, holding nonce, balance, storage trie ref, and code per address.
  • Switch Chain.State from MutableMap to CAccountMap so State.clone/1 is a single NIF call instead of iterating ~14k accounts through ETS + Account.clone.
  • Remove the mutable_map dependency; compact DB states still use plain Map at serialization boundaries.

Performance

Benchmark at ~14,609 accounts (same scale as the profiled Shell.call_tx path):

  • Before: ~5.4s in State.clone (ETS fold + per-account CMerkleTree.clone + MutableMap.put)
  • After: ~2ms (CAccountMap.clone/1)

Test plan

  • mix test test/caccount_map_test.exs
  • mix test test/chain_state_merkle_test.exs (COW round-trip / difference)
  • mix test test/cmerkletree_test.exs
  • make -C c_src nif / make -C c_src nif-asan
  • Re-profile Shell.call_tx on production-scale state in staging

Made with Cursor

Store accounts in a native C++ map with COW semantics so State.clone avoids
per-account ETS iteration and Account struct allocation (~5s to ~2ms at 14k accounts).

Co-authored-by: Cursor <cursoragent@cursor.com>
@gemini-code-assist

Copy link
Copy Markdown

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@dominicletz dominicletz merged commit 10d634b into master Jul 2, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant