Skip to content

fix: avoid BeaconState commit() clone() in beacon-node - #8728

Merged
twoeths merged 7 commits into
unstablefrom
te/avoid_beacon_state_clone
Jan 23, 2026
Merged

fix: avoid BeaconState commit() clone() in beacon-node#8728
twoeths merged 7 commits into
unstablefrom
te/avoid_beacon_state_clone

Conversation

@twoeths

@twoeths twoeths commented Jan 7, 2026

Copy link
Copy Markdown
Member

Motivation

  • we never mutate state inside beacon-node so we should not do the clone() there, same to commit()
  • it's not a problem for ts BeaconStateView, even not a big performance issue for the native BeaconStateView, it's just that we don't have to because it's a principle to not to mutate any BeaconStates in beacon-node
  • this helps us not having to implement clone() and commit() in the BeaconStateView interface

Description

  • remove clone() in state caches and regen.getState() api
  • remove commit()
  • remove unused functions
  • do state.clone() of rewards api to inside its implementation
  • simplify computeBlockRewards()

Closes #8725

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @twoeths, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant refactoring to the beacon-node by eliminating redundant state cloning and committing operations. The changes are motivated by the architectural principle that BeaconState objects should be treated as immutable within the beacon-node to prevent unintended side effects. By removing these operations, the codebase becomes cleaner, more efficient, and aligns better with the intended design, while also simplifying related interfaces and improving type consistency.

Highlights

  • Immutability Enforcement: The primary goal of this pull request is to reinforce the principle that BeaconState objects should remain immutable within the beacon-node context, preventing accidental mutations.
  • Removal of Redundant Operations: Unnecessary clone() and commit() calls on BeaconState objects have been removed from various parts of the beacon-node, including state caches and the regen.getState() API.
  • Simplified Interfaces: The BeaconStateView interface has been simplified by removing the requirement to implement clone() and commit() methods, reflecting the immutable nature of states in this module.
  • Type Safety and Clarity: Return types for state retrieval functions like getStateBySlot and getStateByStateRoot have been updated to explicitly reflect CachedBeaconStateAllForks, improving type safety and code clarity.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request is a well-executed refactoring that aims to enforce the immutability of BeaconState within the beacon-node by removing clone() and commit() calls. The responsibility for cloning the state is correctly moved into the functions within the state-transition package that perform mutations, which is a great design improvement for encapsulation and API safety. The changes are applied consistently across the codebase, including updates to state cache implementations and their consumers. The removal of unused code and the fix of a latent type issue are also good cleanups. Overall, the changes improve code quality and maintainability without introducing any apparent issues.

@twoeths

twoeths commented Jan 8, 2026

Copy link
Copy Markdown
Member Author

tested this for almost 1 day, the only change is state clone count decreased clearly

Screenshot 2026-01-08 at 13 56 30

on mainnet super node, the attestation subnet surged but not affect memory much

Screenshot 2026-01-08 at 13 57 15

on other nodes it's just the same. So I guess it does not affect much, it's more about stability not about performance anyway

@twoeths
twoeths marked this pull request as ready for review January 8, 2026 06:59
@twoeths
twoeths requested a review from a team as a code owner January 8, 2026 06:59
@github-actions

github-actions Bot commented Jan 13, 2026

Copy link
Copy Markdown
Contributor

⚠️ Performance Alert ⚠️

Possible performance regression was detected for some benchmarks.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold.

Benchmark suite Current: f1f90c0 Previous: 3932675 Ratio
200 bytes - uncompress - snappy 20.283 us/op 1.1595 us/op 17.49
Full benchmark results
Benchmark suite Current: f1f90c0 Previous: 3932675 Ratio
getPubkeys - index2pubkey - req 1000 vs - 250000 vc 1.1283 ms/op 1.1555 ms/op 0.98
getPubkeys - validatorsArr - req 1000 vs - 250000 vc 36.951 us/op 35.208 us/op 1.05
BLS verify - blst 1.4322 ms/op 1.0021 ms/op 1.43
BLS verifyMultipleSignatures 3 - blst 2.2358 ms/op 1.2403 ms/op 1.80
BLS verifyMultipleSignatures 8 - blst 2.4906 ms/op 2.4976 ms/op 1.00
BLS verifyMultipleSignatures 32 - blst 7.4023 ms/op 6.1104 ms/op 1.21
BLS verifyMultipleSignatures 64 - blst 10.673 ms/op 11.224 ms/op 0.95
BLS verifyMultipleSignatures 128 - blst 17.022 ms/op 17.336 ms/op 0.98
BLS deserializing 10000 signatures 673.80 ms/op 682.16 ms/op 0.99
BLS deserializing 100000 signatures 6.9165 s/op 6.8837 s/op 1.00
BLS verifyMultipleSignatures - same message - 3 - blst 946.81 us/op 1.1910 ms/op 0.79
BLS verifyMultipleSignatures - same message - 8 - blst 1.2145 ms/op 1.6046 ms/op 0.76
BLS verifyMultipleSignatures - same message - 32 - blst 1.7474 ms/op 1.6240 ms/op 1.08
BLS verifyMultipleSignatures - same message - 64 - blst 2.6662 ms/op 2.5621 ms/op 1.04
BLS verifyMultipleSignatures - same message - 128 - blst 4.4776 ms/op 4.1723 ms/op 1.07
BLS aggregatePubkeys 32 - blst 19.744 us/op 21.863 us/op 0.90
BLS aggregatePubkeys 128 - blst 70.828 us/op 67.433 us/op 1.05
getSlashingsAndExits - default max 70.999 us/op 72.766 us/op 0.98
getSlashingsAndExits - 2k 338.46 us/op 312.86 us/op 1.08
isKnown best case - 1 super set check 217.00 ns/op 198.00 ns/op 1.10
isKnown normal case - 2 super set checks 208.00 ns/op 193.00 ns/op 1.08
isKnown worse case - 16 super set checks 207.00 ns/op 185.00 ns/op 1.12
InMemoryCheckpointStateCache - add get delete 2.2370 us/op 2.3780 us/op 0.94
validate api signedAggregateAndProof - struct 1.4750 ms/op 1.5397 ms/op 0.96
validate gossip signedAggregateAndProof - struct 1.7403 ms/op 1.7114 ms/op 1.02
batch validate gossip attestation - vc 640000 - chunk 32 127.03 us/op 115.43 us/op 1.10
batch validate gossip attestation - vc 640000 - chunk 64 108.15 us/op 101.61 us/op 1.06
batch validate gossip attestation - vc 640000 - chunk 128 95.028 us/op 115.85 us/op 0.82
batch validate gossip attestation - vc 640000 - chunk 256 91.776 us/op 90.741 us/op 1.01
bytes32 toHexString 448.00 ns/op 363.00 ns/op 1.23
bytes32 Buffer.toString(hex) 246.00 ns/op 236.00 ns/op 1.04
bytes32 Buffer.toString(hex) from Uint8Array 334.00 ns/op 324.00 ns/op 1.03
bytes32 Buffer.toString(hex) + 0x 288.00 ns/op 231.00 ns/op 1.25
Object access 1 prop 0.12200 ns/op 0.14400 ns/op 0.85
Map access 1 prop 0.11500 ns/op 0.11500 ns/op 1.00
Object get x1000 5.3470 ns/op 5.2060 ns/op 1.03
Map get x1000 0.37500 ns/op 0.40100 ns/op 0.94
Object set x1000 28.939 ns/op 28.694 ns/op 1.01
Map set x1000 20.395 ns/op 19.561 ns/op 1.04
Return object 10000 times 0.23190 ns/op 0.22750 ns/op 1.02
Throw Error 10000 times 4.1987 us/op 4.0092 us/op 1.05
toHex 146.01 ns/op 154.33 ns/op 0.95
Buffer.from 125.14 ns/op 122.36 ns/op 1.02
shared Buffer 79.692 ns/op 77.600 ns/op 1.03
fastMsgIdFn sha256 / 200 bytes 1.8510 us/op 1.8090 us/op 1.02
fastMsgIdFn h32 xxhash / 200 bytes 197.00 ns/op 210.00 ns/op 0.94
fastMsgIdFn h64 xxhash / 200 bytes 265.00 ns/op 258.00 ns/op 1.03
fastMsgIdFn sha256 / 1000 bytes 5.9730 us/op 5.8960 us/op 1.01
fastMsgIdFn h32 xxhash / 1000 bytes 343.00 ns/op 288.00 ns/op 1.19
fastMsgIdFn h64 xxhash / 1000 bytes 455.00 ns/op 309.00 ns/op 1.47
fastMsgIdFn sha256 / 10000 bytes 52.983 us/op 50.587 us/op 1.05
fastMsgIdFn h32 xxhash / 10000 bytes 1.4200 us/op 1.6040 us/op 0.89
fastMsgIdFn h64 xxhash / 10000 bytes 946.00 ns/op 884.00 ns/op 1.07
100 bytes - compress - snappyjs 1.2613 us/op 1.2301 us/op 1.03
100 bytes - compress - snappy 1.1666 us/op 1.1236 us/op 1.04
100 bytes - compress - snappy-wasm 707.57 ns/op 757.56 ns/op 0.93
100 bytes - compress - snappy-wasm - prealloc 1.8630 us/op 1.2730 us/op 1.46
200 bytes - compress - snappyjs 1.8190 us/op 1.4327 us/op 1.27
200 bytes - compress - snappy 1.3753 us/op 1.3350 us/op 1.03
200 bytes - compress - snappy-wasm 1.0597 us/op 1.3638 us/op 0.78
200 bytes - compress - snappy-wasm - prealloc 1.8497 us/op 1.5149 us/op 1.22
300 bytes - compress - snappyjs 2.5785 us/op 2.2285 us/op 1.16
300 bytes - compress - snappy 1.3635 us/op 2.7233 us/op 0.50
300 bytes - compress - snappy-wasm 880.51 ns/op 772.97 ns/op 1.14
300 bytes - compress - snappy-wasm - prealloc 1.5854 us/op 1.9126 us/op 0.83
400 bytes - compress - snappyjs 2.1770 us/op 1.9955 us/op 1.09
400 bytes - compress - snappy 1.5278 us/op 1.4148 us/op 1.08
400 bytes - compress - snappy-wasm 1.1961 us/op 832.43 ns/op 1.44
400 bytes - compress - snappy-wasm - prealloc 1.5769 us/op 1.0762 us/op 1.47
500 bytes - compress - snappyjs 2.3891 us/op 2.7663 us/op 0.86
500 bytes - compress - snappy 1.4924 us/op 1.3576 us/op 1.10
500 bytes - compress - snappy-wasm 1.2291 us/op 1.1535 us/op 1.07
500 bytes - compress - snappy-wasm - prealloc 1.5376 us/op 1.1679 us/op 1.32
1000 bytes - compress - snappyjs 4.7414 us/op 4.6148 us/op 1.03
1000 bytes - compress - snappy 1.8560 us/op 1.5787 us/op 1.18
1000 bytes - compress - snappy-wasm 1.7613 us/op 1.9577 us/op 0.90
1000 bytes - compress - snappy-wasm - prealloc 1.9826 us/op 1.9873 us/op 1.00
10000 bytes - compress - snappyjs 33.794 us/op 26.897 us/op 1.26
10000 bytes - compress - snappy 23.856 us/op 34.579 us/op 0.69
10000 bytes - compress - snappy-wasm 21.864 us/op 31.705 us/op 0.69
10000 bytes - compress - snappy-wasm - prealloc 36.904 us/op 28.214 us/op 1.31
100 bytes - uncompress - snappyjs 774.65 ns/op 764.63 ns/op 1.01
100 bytes - uncompress - snappy 1.2155 us/op 1.0810 us/op 1.12
100 bytes - uncompress - snappy-wasm 646.40 ns/op 935.16 ns/op 0.69
100 bytes - uncompress - snappy-wasm - prealloc 853.40 ns/op 1.1398 us/op 0.75
200 bytes - uncompress - snappyjs 1.0711 us/op 1.2325 us/op 0.87
200 bytes - uncompress - snappy 20.283 us/op 1.1595 us/op 17.49
200 bytes - uncompress - snappy-wasm 979.13 ns/op 711.39 ns/op 1.38
200 bytes - uncompress - snappy-wasm - prealloc 1.3117 us/op 1.6565 us/op 0.79
300 bytes - uncompress - snappyjs 1.7206 us/op 1.4453 us/op 1.19
300 bytes - uncompress - snappy 3.0294 us/op 2.6633 us/op 1.14
300 bytes - uncompress - snappy-wasm 832.30 ns/op 1.1614 us/op 0.72
300 bytes - uncompress - snappy-wasm - prealloc 1.0052 us/op 1.5149 us/op 0.66
400 bytes - uncompress - snappyjs 1.1583 us/op 1.5339 us/op 0.76
400 bytes - uncompress - snappy 1.3443 us/op 1.4614 us/op 0.92
400 bytes - uncompress - snappy-wasm 938.68 ns/op 835.90 ns/op 1.12
400 bytes - uncompress - snappy-wasm - prealloc 1.0748 us/op 1.3849 us/op 0.78
500 bytes - uncompress - snappyjs 2.0456 us/op 2.6247 us/op 0.78
500 bytes - uncompress - snappy 1.5205 us/op 1.7607 us/op 0.86
500 bytes - uncompress - snappy-wasm 1.3012 us/op 842.62 ns/op 1.54
500 bytes - uncompress - snappy-wasm - prealloc 1.1294 us/op 1.3347 us/op 0.85
1000 bytes - uncompress - snappyjs 2.0726 us/op 2.6317 us/op 0.79
1000 bytes - uncompress - snappy 1.9989 us/op 1.5517 us/op 1.29
1000 bytes - uncompress - snappy-wasm 1.1418 us/op 1.3143 us/op 0.87
1000 bytes - uncompress - snappy-wasm - prealloc 1.3038 us/op 1.3680 us/op 0.95
10000 bytes - uncompress - snappyjs 21.147 us/op 21.624 us/op 0.98
10000 bytes - uncompress - snappy 43.067 us/op 23.060 us/op 1.87
10000 bytes - uncompress - snappy-wasm 17.771 us/op 31.355 us/op 0.57
10000 bytes - uncompress - snappy-wasm - prealloc 19.716 us/op 28.445 us/op 0.69
send data - 1000 256B messages 17.581 ms/op 16.290 ms/op 1.08
send data - 1000 512B messages 19.684 ms/op 18.578 ms/op 1.06
send data - 1000 1024B messages 26.340 ms/op 24.298 ms/op 1.08
send data - 1000 1200B messages 29.032 ms/op 29.455 ms/op 0.99
send data - 1000 2048B messages 25.691 ms/op 23.418 ms/op 1.10
send data - 1000 4096B messages 50.100 ms/op 58.794 ms/op 0.85
send data - 1000 16384B messages 122.41 ms/op 115.70 ms/op 1.06
send data - 1000 65536B messages 267.72 ms/op 301.27 ms/op 0.89
enrSubnets - fastDeserialize 64 bits 2.1680 us/op 1.1210 us/op 1.93
enrSubnets - ssz BitVector 64 bits 342.00 ns/op 319.00 ns/op 1.07
enrSubnets - fastDeserialize 4 bits 147.00 ns/op 161.00 ns/op 0.91
enrSubnets - ssz BitVector 4 bits 354.00 ns/op 346.00 ns/op 1.02
prioritizePeers score -10:0 att 32-0.1 sync 2-0 250.00 us/op 257.01 us/op 0.97
prioritizePeers score 0:0 att 32-0.25 sync 2-0.25 290.11 us/op 283.34 us/op 1.02
prioritizePeers score 0:0 att 32-0.5 sync 2-0.5 406.21 us/op 392.94 us/op 1.03
prioritizePeers score 0:0 att 64-0.75 sync 4-0.75 758.80 us/op 726.48 us/op 1.04
prioritizePeers score 0:0 att 64-1 sync 4-1 885.35 us/op 864.40 us/op 1.02
array of 16000 items push then shift 1.8592 us/op 1.5769 us/op 1.18
LinkedList of 16000 items push then shift 8.0500 ns/op 7.1950 ns/op 1.12
array of 16000 items push then pop 84.410 ns/op 75.524 ns/op 1.12
LinkedList of 16000 items push then pop 7.6040 ns/op 7.0090 ns/op 1.08
array of 24000 items push then shift 2.4628 us/op 2.3292 us/op 1.06
LinkedList of 24000 items push then shift 7.6120 ns/op 7.3420 ns/op 1.04
array of 24000 items push then pop 111.80 ns/op 106.20 ns/op 1.05
LinkedList of 24000 items push then pop 7.4490 ns/op 7.0390 ns/op 1.06
intersect bitArray bitLen 8 5.7800 ns/op 5.5850 ns/op 1.03
intersect array and set length 8 34.913 ns/op 32.652 ns/op 1.07
intersect bitArray bitLen 128 28.989 ns/op 27.999 ns/op 1.04
intersect array and set length 128 560.42 ns/op 541.27 ns/op 1.04
bitArray.getTrueBitIndexes() bitLen 128 1.1460 us/op 1.1800 us/op 0.97
bitArray.getTrueBitIndexes() bitLen 248 1.7780 us/op 1.8780 us/op 0.95
bitArray.getTrueBitIndexes() bitLen 512 3.7220 us/op 3.6540 us/op 1.02
Full columns - reconstruct all 6 blobs 383.32 us/op 273.13 us/op 1.40
Full columns - reconstruct half of the blobs out of 6 128.27 us/op 140.47 us/op 0.91
Full columns - reconstruct single blob out of 6 33.702 us/op 32.202 us/op 1.05
Half columns - reconstruct all 6 blobs 281.23 ms/op 258.65 ms/op 1.09
Half columns - reconstruct half of the blobs out of 6 149.87 ms/op 131.45 ms/op 1.14
Half columns - reconstruct single blob out of 6 54.317 ms/op 48.701 ms/op 1.12
Full columns - reconstruct all 10 blobs 432.19 us/op 390.78 us/op 1.11
Full columns - reconstruct half of the blobs out of 10 183.69 us/op 157.93 us/op 1.16
Full columns - reconstruct single blob out of 10 32.724 us/op 30.513 us/op 1.07
Half columns - reconstruct all 10 blobs 461.38 ms/op 433.84 ms/op 1.06
Half columns - reconstruct half of the blobs out of 10 235.73 ms/op 222.29 ms/op 1.06
Half columns - reconstruct single blob out of 10 52.765 ms/op 48.189 ms/op 1.09
Full columns - reconstruct all 20 blobs 906.58 us/op 772.14 us/op 1.17
Full columns - reconstruct half of the blobs out of 20 336.43 us/op 315.09 us/op 1.07
Full columns - reconstruct single blob out of 20 34.379 us/op 31.405 us/op 1.09
Half columns - reconstruct all 20 blobs 917.46 ms/op 859.20 ms/op 1.07
Half columns - reconstruct half of the blobs out of 20 453.53 ms/op 433.03 ms/op 1.05
Half columns - reconstruct single blob out of 20 49.927 ms/op 47.880 ms/op 1.04
Set add up to 64 items then delete first 2.1473 us/op 1.9697 us/op 1.09
OrderedSet add up to 64 items then delete first 3.1875 us/op 2.8740 us/op 1.11
Set add up to 64 items then delete last 2.3896 us/op 2.1716 us/op 1.10
OrderedSet add up to 64 items then delete last 3.6530 us/op 3.1096 us/op 1.17
Set add up to 64 items then delete middle 2.4632 us/op 2.1756 us/op 1.13
OrderedSet add up to 64 items then delete middle 5.2201 us/op 4.7061 us/op 1.11
Set add up to 128 items then delete first 4.8974 us/op 4.6307 us/op 1.06
OrderedSet add up to 128 items then delete first 7.3456 us/op 7.0925 us/op 1.04
Set add up to 128 items then delete last 4.8888 us/op 4.4261 us/op 1.10
OrderedSet add up to 128 items then delete last 7.5379 us/op 6.4433 us/op 1.17
Set add up to 128 items then delete middle 4.7525 us/op 4.4421 us/op 1.07
OrderedSet add up to 128 items then delete middle 14.119 us/op 13.010 us/op 1.09
Set add up to 256 items then delete first 10.374 us/op 9.9543 us/op 1.04
OrderedSet add up to 256 items then delete first 15.166 us/op 14.830 us/op 1.02
Set add up to 256 items then delete last 9.9156 us/op 9.3177 us/op 1.06
OrderedSet add up to 256 items then delete last 15.198 us/op 13.776 us/op 1.10
Set add up to 256 items then delete middle 9.7757 us/op 9.2519 us/op 1.06
OrderedSet add up to 256 items then delete middle 42.405 us/op 40.143 us/op 1.06
pass gossip attestations to forkchoice per slot 2.8092 ms/op 2.4561 ms/op 1.14
forkChoice updateHead vc 100000 bc 64 eq 0 518.90 us/op 484.70 us/op 1.07
forkChoice updateHead vc 600000 bc 64 eq 0 3.1886 ms/op 2.8920 ms/op 1.10
forkChoice updateHead vc 1000000 bc 64 eq 0 5.1875 ms/op 4.8234 ms/op 1.08
forkChoice updateHead vc 600000 bc 320 eq 0 3.1180 ms/op 2.9115 ms/op 1.07
forkChoice updateHead vc 600000 bc 1200 eq 0 3.1867 ms/op 2.9414 ms/op 1.08
forkChoice updateHead vc 600000 bc 7200 eq 0 3.6994 ms/op 3.1794 ms/op 1.16
forkChoice updateHead vc 600000 bc 64 eq 1000 3.5049 ms/op 3.3137 ms/op 1.06
forkChoice updateHead vc 600000 bc 64 eq 10000 3.5948 ms/op 3.4471 ms/op 1.04
forkChoice updateHead vc 600000 bc 64 eq 300000 9.7399 ms/op 9.0563 ms/op 1.08
computeDeltas 1400000 validators 0% inactive 14.720 ms/op 13.628 ms/op 1.08
computeDeltas 1400000 validators 10% inactive 13.839 ms/op 14.029 ms/op 0.99
computeDeltas 1400000 validators 20% inactive 12.881 ms/op 12.160 ms/op 1.06
computeDeltas 1400000 validators 50% inactive 10.084 ms/op 9.3157 ms/op 1.08
computeDeltas 2100000 validators 0% inactive 22.065 ms/op 20.502 ms/op 1.08
computeDeltas 2100000 validators 10% inactive 20.630 ms/op 19.197 ms/op 1.07
computeDeltas 2100000 validators 20% inactive 19.200 ms/op 18.044 ms/op 1.06
computeDeltas 2100000 validators 50% inactive 15.034 ms/op 14.137 ms/op 1.06
altair processAttestation - 250000 vs - 7PWei normalcase 2.1569 ms/op 1.8790 ms/op 1.15
altair processAttestation - 250000 vs - 7PWei worstcase 3.1064 ms/op 2.7465 ms/op 1.13
altair processAttestation - setStatus - 1/6 committees join 121.89 us/op 117.42 us/op 1.04
altair processAttestation - setStatus - 1/3 committees join 246.52 us/op 229.70 us/op 1.07
altair processAttestation - setStatus - 1/2 committees join 336.71 us/op 325.01 us/op 1.04
altair processAttestation - setStatus - 2/3 committees join 414.32 us/op 414.30 us/op 1.00
altair processAttestation - setStatus - 4/5 committees join 571.45 us/op 575.87 us/op 0.99
altair processAttestation - setStatus - 100% committees join 670.86 us/op 680.85 us/op 0.99
altair processBlock - 250000 vs - 7PWei normalcase 3.8735 ms/op 3.7499 ms/op 1.03
altair processBlock - 250000 vs - 7PWei normalcase hashState 20.469 ms/op 21.445 ms/op 0.95
altair processBlock - 250000 vs - 7PWei worstcase 28.976 ms/op 26.222 ms/op 1.11
altair processBlock - 250000 vs - 7PWei worstcase hashState 62.696 ms/op 67.071 ms/op 0.93
phase0 processBlock - 250000 vs - 7PWei normalcase 1.4808 ms/op 1.5753 ms/op 0.94
phase0 processBlock - 250000 vs - 7PWei worstcase 23.582 ms/op 22.026 ms/op 1.07
altair processEth1Data - 250000 vs - 7PWei normalcase 386.51 us/op 366.35 us/op 1.06
getExpectedWithdrawals 250000 eb:1,eth1:1,we:0,wn:0,smpl:16 9.0250 us/op 7.8290 us/op 1.15
getExpectedWithdrawals 250000 eb:0.95,eth1:0.1,we:0.05,wn:0,smpl:220 37.041 us/op 56.641 us/op 0.65
getExpectedWithdrawals 250000 eb:0.95,eth1:0.3,we:0.05,wn:0,smpl:43 16.284 us/op 9.8620 us/op 1.65
getExpectedWithdrawals 250000 eb:0.95,eth1:0.7,we:0.05,wn:0,smpl:19 9.5880 us/op 10.248 us/op 0.94
getExpectedWithdrawals 250000 eb:0.1,eth1:0.1,we:0,wn:0,smpl:1021 251.53 us/op 245.42 us/op 1.02
getExpectedWithdrawals 250000 eb:0.03,eth1:0.03,we:0,wn:0,smpl:11778 2.1632 ms/op 1.7145 ms/op 1.26
getExpectedWithdrawals 250000 eb:0.01,eth1:0.01,we:0,wn:0,smpl:16384 3.2912 ms/op 2.3081 ms/op 1.43
getExpectedWithdrawals 250000 eb:0,eth1:0,we:0,wn:0,smpl:16384 2.5247 ms/op 2.3494 ms/op 1.07
getExpectedWithdrawals 250000 eb:0,eth1:0,we:0,wn:0,nocache,smpl:16384 4.9793 ms/op 3.9429 ms/op 1.26
getExpectedWithdrawals 250000 eb:0,eth1:1,we:0,wn:0,smpl:16384 2.9234 ms/op 2.3584 ms/op 1.24
getExpectedWithdrawals 250000 eb:0,eth1:1,we:0,wn:0,nocache,smpl:16384 4.9288 ms/op 3.9418 ms/op 1.25
Tree 40 250000 create 373.02 ms/op 373.98 ms/op 1.00
Tree 40 250000 get(125000) 128.76 ns/op 125.56 ns/op 1.03
Tree 40 250000 set(125000) 1.3624 us/op 1.2282 us/op 1.11
Tree 40 250000 toArray() 14.458 ms/op 15.417 ms/op 0.94
Tree 40 250000 iterate all - toArray() + loop 13.859 ms/op 15.131 ms/op 0.92
Tree 40 250000 iterate all - get(i) 47.919 ms/op 45.655 ms/op 1.05
Array 250000 create 2.5762 ms/op 2.4962 ms/op 1.03
Array 250000 clone - spread 836.44 us/op 809.91 us/op 1.03
Array 250000 get(125000) 0.36400 ns/op 0.36100 ns/op 1.01
Array 250000 set(125000) 0.36800 ns/op 0.35600 ns/op 1.03
Array 250000 iterate all - loop 62.380 us/op 58.644 us/op 1.06
phase0 afterProcessEpoch - 250000 vs - 7PWei 42.181 ms/op 41.332 ms/op 1.02
Array.fill - length 1000000 2.9450 ms/op 2.8404 ms/op 1.04
Array push - length 1000000 11.055 ms/op 10.313 ms/op 1.07
Array.get 0.22088 ns/op 0.21772 ns/op 1.01
Uint8Array.get 0.22138 ns/op 0.21929 ns/op 1.01
phase0 beforeProcessEpoch - 250000 vs - 7PWei 14.342 ms/op 13.698 ms/op 1.05
altair processEpoch - mainnet_e81889 262.37 ms/op 226.69 ms/op 1.16
mainnet_e81889 - altair beforeProcessEpoch 17.089 ms/op 15.732 ms/op 1.09
mainnet_e81889 - altair processJustificationAndFinalization 5.5650 us/op 5.4270 us/op 1.03
mainnet_e81889 - altair processInactivityUpdates 3.8284 ms/op 3.6389 ms/op 1.05
mainnet_e81889 - altair processRewardsAndPenalties 27.933 ms/op 19.913 ms/op 1.40
mainnet_e81889 - altair processRegistryUpdates 596.00 ns/op 632.00 ns/op 0.94
mainnet_e81889 - altair processSlashings 159.00 ns/op 164.00 ns/op 0.97
mainnet_e81889 - altair processEth1DataReset 221.00 ns/op 158.00 ns/op 1.40
mainnet_e81889 - altair processEffectiveBalanceUpdates 1.7678 ms/op 1.8079 ms/op 0.98
mainnet_e81889 - altair processSlashingsReset 1.0040 us/op 810.00 ns/op 1.24
mainnet_e81889 - altair processRandaoMixesReset 1.0980 us/op 1.0120 us/op 1.08
mainnet_e81889 - altair processHistoricalRootsUpdate 161.00 ns/op 158.00 ns/op 1.02
mainnet_e81889 - altair processParticipationFlagUpdates 639.00 ns/op 507.00 ns/op 1.26
mainnet_e81889 - altair processSyncCommitteeUpdates 128.00 ns/op 130.00 ns/op 0.98
mainnet_e81889 - altair afterProcessEpoch 42.776 ms/op 41.817 ms/op 1.02
capella processEpoch - mainnet_e217614 838.40 ms/op 800.14 ms/op 1.05
mainnet_e217614 - capella beforeProcessEpoch 71.285 ms/op 55.620 ms/op 1.28
mainnet_e217614 - capella processJustificationAndFinalization 6.4900 us/op 5.2450 us/op 1.24
mainnet_e217614 - capella processInactivityUpdates 15.427 ms/op 15.147 ms/op 1.02
mainnet_e217614 - capella processRewardsAndPenalties 109.15 ms/op 99.233 ms/op 1.10
mainnet_e217614 - capella processRegistryUpdates 5.8860 us/op 5.8310 us/op 1.01
mainnet_e217614 - capella processSlashings 173.00 ns/op 172.00 ns/op 1.01
mainnet_e217614 - capella processEth1DataReset 247.00 ns/op 160.00 ns/op 1.54
mainnet_e217614 - capella processEffectiveBalanceUpdates 15.634 ms/op 9.5744 ms/op 1.63
mainnet_e217614 - capella processSlashingsReset 909.00 ns/op 764.00 ns/op 1.19
mainnet_e217614 - capella processRandaoMixesReset 1.1080 us/op 1.3720 us/op 0.81
mainnet_e217614 - capella processHistoricalRootsUpdate 188.00 ns/op 238.00 ns/op 0.79
mainnet_e217614 - capella processParticipationFlagUpdates 681.00 ns/op 688.00 ns/op 0.99
mainnet_e217614 - capella afterProcessEpoch 116.60 ms/op 113.67 ms/op 1.03
phase0 processEpoch - mainnet_e58758 362.37 ms/op 255.06 ms/op 1.42
mainnet_e58758 - phase0 beforeProcessEpoch 59.725 ms/op 53.744 ms/op 1.11
mainnet_e58758 - phase0 processJustificationAndFinalization 5.8820 us/op 5.8770 us/op 1.00
mainnet_e58758 - phase0 processRewardsAndPenalties 20.177 ms/op 17.222 ms/op 1.17
mainnet_e58758 - phase0 processRegistryUpdates 2.8540 us/op 2.7330 us/op 1.04
mainnet_e58758 - phase0 processSlashings 212.00 ns/op 194.00 ns/op 1.09
mainnet_e58758 - phase0 processEth1DataReset 197.00 ns/op 219.00 ns/op 0.90
mainnet_e58758 - phase0 processEffectiveBalanceUpdates 1.4524 ms/op 1.0867 ms/op 1.34
mainnet_e58758 - phase0 processSlashingsReset 1.0360 us/op 877.00 ns/op 1.18
mainnet_e58758 - phase0 processRandaoMixesReset 1.3580 us/op 1.1050 us/op 1.23
mainnet_e58758 - phase0 processHistoricalRootsUpdate 205.00 ns/op 220.00 ns/op 0.93
mainnet_e58758 - phase0 processParticipationRecordUpdates 863.00 ns/op 897.00 ns/op 0.96
mainnet_e58758 - phase0 afterProcessEpoch 36.696 ms/op 35.812 ms/op 1.02
phase0 processEffectiveBalanceUpdates - 250000 normalcase 2.2129 ms/op 2.5886 ms/op 0.85
phase0 processEffectiveBalanceUpdates - 250000 worstcase 0.5 1.8128 ms/op 1.6162 ms/op 1.12
altair processInactivityUpdates - 250000 normalcase 15.655 ms/op 13.438 ms/op 1.16
altair processInactivityUpdates - 250000 worstcase 16.402 ms/op 16.183 ms/op 1.01
phase0 processRegistryUpdates - 250000 normalcase 4.8640 us/op 5.9520 us/op 0.82
phase0 processRegistryUpdates - 250000 badcase_full_deposits 274.42 us/op 329.40 us/op 0.83
phase0 processRegistryUpdates - 250000 worstcase 0.5 80.419 ms/op 64.873 ms/op 1.24
altair processRewardsAndPenalties - 250000 normalcase 17.880 ms/op 16.894 ms/op 1.06
altair processRewardsAndPenalties - 250000 worstcase 17.475 ms/op 16.014 ms/op 1.09
phase0 getAttestationDeltas - 250000 normalcase 6.9258 ms/op 5.5204 ms/op 1.25
phase0 getAttestationDeltas - 250000 worstcase 7.0379 ms/op 5.9099 ms/op 1.19
phase0 processSlashings - 250000 worstcase 110.63 us/op 105.15 us/op 1.05
altair processSyncCommitteeUpdates - 250000 11.528 ms/op 10.951 ms/op 1.05
BeaconState.hashTreeRoot - No change 205.00 ns/op 259.00 ns/op 0.79
BeaconState.hashTreeRoot - 1 full validator 99.002 us/op 81.431 us/op 1.22
BeaconState.hashTreeRoot - 32 full validator 1.1223 ms/op 1.1442 ms/op 0.98
BeaconState.hashTreeRoot - 512 full validator 9.4961 ms/op 8.2971 ms/op 1.14
BeaconState.hashTreeRoot - 1 validator.effectiveBalance 116.50 us/op 107.75 us/op 1.08
BeaconState.hashTreeRoot - 32 validator.effectiveBalance 2.2899 ms/op 1.5399 ms/op 1.49
BeaconState.hashTreeRoot - 512 validator.effectiveBalance 25.253 ms/op 18.230 ms/op 1.39
BeaconState.hashTreeRoot - 1 balances 84.521 us/op 69.706 us/op 1.21
BeaconState.hashTreeRoot - 32 balances 1.0004 ms/op 835.78 us/op 1.20
BeaconState.hashTreeRoot - 512 balances 6.5833 ms/op 6.1257 ms/op 1.07
BeaconState.hashTreeRoot - 250000 balances 158.75 ms/op 149.54 ms/op 1.06
aggregationBits - 2048 els - zipIndexesInBitList 34.049 us/op 20.607 us/op 1.65
regular array get 100000 times 51.284 us/op 24.400 us/op 2.10
wrappedArray get 100000 times 27.217 us/op 24.476 us/op 1.11
arrayWithProxy get 100000 times 15.889 ms/op 16.744 ms/op 0.95
ssz.Root.equals 24.961 ns/op 23.975 ns/op 1.04
byteArrayEquals 24.068 ns/op 23.286 ns/op 1.03
Buffer.compare 10.269 ns/op 10.019 ns/op 1.02
processSlot - 1 slots 11.548 us/op 9.9250 us/op 1.16
processSlot - 32 slots 2.5505 ms/op 2.5192 ms/op 1.01
getEffectiveBalanceIncrementsZeroInactive - 250000 vs - 7PWei 5.2245 ms/op 4.1825 ms/op 1.25
getCommitteeAssignments - req 1 vs - 250000 vc 1.9962 ms/op 1.9065 ms/op 1.05
getCommitteeAssignments - req 100 vs - 250000 vc 3.8658 ms/op 3.7068 ms/op 1.04
getCommitteeAssignments - req 1000 vs - 250000 vc 4.1482 ms/op 3.9556 ms/op 1.05
findModifiedValidators - 10000 modified validators 767.17 ms/op 617.87 ms/op 1.24
findModifiedValidators - 1000 modified validators 421.55 ms/op 599.46 ms/op 0.70
findModifiedValidators - 100 modified validators 411.34 ms/op 502.90 ms/op 0.82
findModifiedValidators - 10 modified validators 205.92 ms/op 372.36 ms/op 0.55
findModifiedValidators - 1 modified validators 140.25 ms/op 330.17 ms/op 0.42
findModifiedValidators - no difference 201.18 ms/op 360.73 ms/op 0.56
migrate state 1500000 validators, 3400 modified, 2000 new 1.0869 s/op 1.4814 s/op 0.73
RootCache.getBlockRootAtSlot - 250000 vs - 7PWei 4.4400 ns/op 7.3000 ns/op 0.61
state getBlockRootAtSlot - 250000 vs - 7PWei 624.77 ns/op 1.1800 us/op 0.53
computeProposerIndex 100000 validators 1.5765 ms/op 1.7455 ms/op 0.90
getNextSyncCommitteeIndices 1000 validators 120.87 ms/op 173.30 ms/op 0.70
getNextSyncCommitteeIndices 10000 validators 120.45 ms/op 177.22 ms/op 0.68
getNextSyncCommitteeIndices 100000 validators 122.97 ms/op 197.07 ms/op 0.62
computeProposers - vc 250000 661.94 us/op 649.60 us/op 1.02
computeEpochShuffling - vc 250000 41.307 ms/op 44.000 ms/op 0.94
getNextSyncCommittee - vc 250000 10.729 ms/op 10.899 ms/op 0.98
nodejs block root to RootHex using toHex 154.55 ns/op 151.15 ns/op 1.02
nodejs block root to RootHex using toRootHex 82.297 ns/op 108.69 ns/op 0.76
nodejs fromHex(blob) 206.43 us/op 241.32 us/op 0.86
nodejs fromHexInto(blob) 710.38 us/op 811.12 us/op 0.88
nodejs block root to RootHex using the deprecated toHexString 524.84 ns/op 555.89 ns/op 0.94
browser block root to RootHex using toHex 303.50 ns/op 359.65 ns/op 0.84
browser block root to RootHex using toRootHex 155.09 ns/op 164.36 ns/op 0.94
browser fromHex(blob) 1.1984 ms/op 1.0854 ms/op 1.10
browser fromHexInto(blob) 732.36 us/op 720.01 us/op 1.02
browser block root to RootHex using the deprecated toHexString 384.63 ns/op 539.11 ns/op 0.71

by benchmarkbot/action

@nflaig nflaig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this seems to limit our ability to serve historical data, need to be careful that we don't break those use cases

Comment on lines +26 to +28
if (res.state instanceof Uint8Array) {
// this should not happen because stateId is a string, check `getStateResponseWithRegen` implementation
throw new Error(`State for stateId ${stateId} is not available for proof generation.`);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this seems wrong, stateId is a string but it can be a slot in which case we might query a historical state which will be of type Uint8Array

const blockSlot = parseInt(String(stateId), 10);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

reverted the change in f7d9c5c

Comment thread packages/beacon-node/src/api/impl/validator/index.ts Outdated
Comment thread packages/beacon-node/src/chain/chain.ts Outdated
const data = await this.db.stateArchive.getByRoot(fromHex(stateRoot));
return data && {state: data, executionOptimistic: false, finalized: true};
// we can do a `this.db.stateArchive.getByRoot()` here, but
// returning 1 per 100s of states that are persisted in the archive state is not useful enough

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we had users run with --chain.archiveStateEpochFrequency 1 to have faster state access, not sure this is still the case but we need to keep that in mind

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I changed it to query state archive db to return Uint8Array instead
the goal is not to return the BeaconStateAllForks, I want to use CachedBeaconStateAllForks only to make it easier to migrate to BeaconStateView later

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

// if block is trusted don't verify proposer or op signature
verifyProposer: !useBlsBatchVerify && !validSignatures && !validProposerSignature,
verifySignatures: !useBlsBatchVerify && !validSignatures,
dontTransferCache: false,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

how is this change related to the other changes in the PR?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

dontTransferCache is used silently inside this call, so I make it explicit. There is nothing changed here.

see

let postState = state.clone(options.dontTransferCache);

*/
getStateSync(stateRoot: RootHex): CachedBeaconStateAllForks | null {
return this.blockStateCache.get(stateRoot, {dontTransferCache: true});
return this.blockStateCache.get(stateRoot);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the comment above explicitly notes that we don't transfer cache, either comment or code needs to be adapted

@nflaig

nflaig commented Jan 23, 2026

Copy link
Copy Markdown
Member

there is still a clone here

is it required because we pass state into createCachedBeaconState?

@twoeths

twoeths commented Jan 23, 2026

Copy link
Copy Markdown
Member Author

is it required because we pass state into createCachedBeaconState?

good catch, removed it

@nflaig nflaig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@twoeths
twoeths merged commit b2437a6 into unstable Jan 23, 2026
39 of 44 checks passed
@twoeths
twoeths deleted the te/avoid_beacon_state_clone branch January 23, 2026 12:48
@codecov

codecov Bot commented Jan 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 52.04%. Comparing base (b79f41e) to head (253f589).
⚠️ Report is 24 commits behind head on unstable.

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #8728      +/-   ##
============================================
+ Coverage     52.02%   52.04%   +0.02%     
============================================
  Files           848      848              
  Lines         64650    64524     -126     
  Branches       4767     4759       -8     
============================================
- Hits          33632    33582      -50     
+ Misses        30949    30873      -76     
  Partials         69       69              
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wemeetagain

Copy link
Copy Markdown
Member

🎉 This PR is included in v1.40.0 🎉

nflaig pushed a commit that referenced this pull request Feb 23, 2026
**Motivation**

- see #8650

**Description**

- implement `IBeaconStateView` interface
- implement the `ts` `BeaconStateView`

blocked by #8728 to build successfully

---------

Co-authored-by: Tuyen Nguyen <twoeths@users.noreply.github.com>
Co-authored-by: Cayman <caymannava@gmail.com>
lodekeeper pushed a commit to lodekeeper/lodestar that referenced this pull request Mar 13, 2026
**Motivation**

- see ChainSafe#8650

**Description**

- implement `IBeaconStateView` interface
- implement the `ts` `BeaconStateView`

blocked by ChainSafe#8728 to build successfully

---------

Co-authored-by: Tuyen Nguyen <twoeths@users.noreply.github.com>
Co-authored-by: Cayman <caymannava@gmail.com>
@twoeths twoeths mentioned this pull request Apr 16, 2026
5 tasks
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.

Avoid calling BeaconState.clone() in beacon-node

3 participants