Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep states in cache as WeakRef #3420

Closed
wants to merge 6 commits into from

Conversation

dapplion
Copy link
Contributor

@dapplion dapplion commented Nov 8, 2021

Motivation

Lodestar has suffered serious memory issues when the state cache grows unbounded due to some bug (for example #3171)

The problem is that the state caches are unbounded memory wise. Current state cache design only limits by state count. In normal conditions that's good enough thanks to high structural sharing. However if the states are too different it will cause an out of memory (OOM) exception.

Description

Keep all states but the head as WeakRefs. NodeJS will garbage collect (GC) the oldest WeakRefs when the app's memory is considered too high.

Script packages/lodestar/test/perf/weakrefOrder.ts demonstrates the behavior of WeakRef (tested in NodeJS v14 and v16).

  • WeakRef are GC'ed strictly by creation order
  • For a WeakRef to become GC-able the macro queue must tick at least once
  • After calling WeakRef.deref(), for that WeakRef to become GC-able the macro queue must tick at least once

@codecov
Copy link

codecov bot commented Nov 8, 2021

Codecov Report

Merging #3420 (4f96320) into master (08dbb21) will increase coverage by 0.00%.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #3420   +/-   ##
=======================================
  Coverage   38.21%   38.21%           
=======================================
  Files         303      303           
  Lines        7890     7921   +31     
  Branches     1207     1110   -97     
=======================================
+ Hits         3015     3027   +12     
- Misses       4734     4764   +30     
+ Partials      141      130   -11     

@codeclimate
Copy link

codeclimate bot commented Nov 8, 2021

Code Climate has analyzed commit 4f96320 and detected 0 issues on this pull request.

View more on Code Climate.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 8, 2021

Performance Report

✔️ no performance regression detected

Full benchmark results
Benchmark suite Current: d8f525f Previous: 08dbb21 Ratio
BeaconState.hashTreeRoot - No change 770.00 ns/op 608.00 ns/op 1.27
BeaconState.hashTreeRoot - 1 full validator 101.33 us/op 86.193 us/op 1.18
BeaconState.hashTreeRoot - 32 full validator 1.4395 ms/op 1.3475 ms/op 1.07
BeaconState.hashTreeRoot - 512 full validator 18.307 ms/op 16.267 ms/op 1.13
BeaconState.hashTreeRoot - 1 validator.effectiveBalance 101.73 us/op 85.028 us/op 1.20
BeaconState.hashTreeRoot - 32 validator.effectiveBalance 1.4999 ms/op 1.3171 ms/op 1.14
BeaconState.hashTreeRoot - 512 validator.effectiveBalance 22.015 ms/op 17.193 ms/op 1.28
BeaconState.hashTreeRoot - 1 balances 73.303 us/op 59.728 us/op 1.23
BeaconState.hashTreeRoot - 32 balances 628.71 us/op 514.37 us/op 1.22
BeaconState.hashTreeRoot - 512 balances 5.7596 ms/op 4.6966 ms/op 1.23
BeaconState.hashTreeRoot - 250000 balances 111.44 ms/op 88.311 ms/op 1.26
processSlot - 1 slots 55.288 us/op 43.324 us/op 1.28
processSlot - 32 slots 2.7913 ms/op 2.3168 ms/op 1.20
getCommitteeAssignments - req 1 vs - 250000 vc 5.3750 ms/op 4.3956 ms/op 1.22
getCommitteeAssignments - req 100 vs - 250000 vc 7.3643 ms/op 6.1973 ms/op 1.19
getCommitteeAssignments - req 1000 vs - 250000 vc 8.1653 ms/op 6.4883 ms/op 1.26
computeProposers - vc 250000 24.829 ms/op 20.431 ms/op 1.22
computeEpochShuffling - vc 250000 202.22 ms/op 176.57 ms/op 1.15
getNextSyncCommittee - vc 250000 420.07 ms/op 351.81 ms/op 1.19
altair processAttestation - 250000 vs - 7PWei normalcase 44.100 ms/op 37.874 ms/op 1.16
altair processAttestation - 250000 vs - 7PWei worstcase 50.567 ms/op 51.875 ms/op 0.97
altair processAttestation - setStatus - 1/6 committees join 13.157 ms/op 10.628 ms/op 1.24
altair processAttestation - setStatus - 1/3 committees join 28.437 ms/op 20.997 ms/op 1.35
altair processAttestation - setStatus - 1/2 committees join 37.939 ms/op 32.914 ms/op 1.15
altair processAttestation - setStatus - 2/3 committees join 52.478 ms/op 43.339 ms/op 1.21
altair processAttestation - setStatus - 4/5 committees join 65.464 ms/op 47.105 ms/op 1.39
altair processAttestation - setStatus - 100% committees join 77.939 ms/op 60.640 ms/op 1.29
altair processAttestation - updateEpochParticipants - 1/6 committees join 12.383 ms/op 9.7740 ms/op 1.27
altair processAttestation - updateEpochParticipants - 1/3 committees join 26.549 ms/op 21.670 ms/op 1.23
altair processAttestation - updateEpochParticipants - 1/2 committees join 28.673 ms/op 21.449 ms/op 1.34
altair processAttestation - updateEpochParticipants - 2/3 committees join 31.071 ms/op 24.312 ms/op 1.28
altair processAttestation - updateEpochParticipants - 4/5 committees join 30.001 ms/op 23.529 ms/op 1.28
altair processAttestation - updateEpochParticipants - 100% committees join 31.906 ms/op 24.286 ms/op 1.31
altair processAttestation - updateAllStatus 27.213 ms/op 17.870 ms/op 1.52
altair processBlock - 250000 vs - 7PWei normalcase 47.982 ms/op 44.047 ms/op 1.09
altair processBlock - 250000 vs - 7PWei worstcase 144.68 ms/op 111.97 ms/op 1.29
altair processEpoch - pyrmont_e62330 521.19 ms/op 412.54 ms/op 1.26
pyrmont_e62330 - altair beforeProcessEpoch 181.85 ms/op 143.34 ms/op 1.27
pyrmont_e62330 - altair processJustificationAndFinalization 116.47 us/op 117.54 us/op 0.99
pyrmont_e62330 - altair processInactivityUpdates 9.4079 ms/op 7.5154 ms/op 1.25
pyrmont_e62330 - altair processRewardsAndPenalties 63.853 ms/op 51.060 ms/op 1.25
pyrmont_e62330 - altair processRegistryUpdates 19.525 us/op 17.780 us/op 1.10
pyrmont_e62330 - altair processSlashings 6.3290 us/op 6.9200 us/op 0.91
pyrmont_e62330 - altair processEth1DataReset 6.5350 us/op 6.2360 us/op 1.05
pyrmont_e62330 - altair processEffectiveBalanceUpdates 6.0685 ms/op 4.7394 ms/op 1.28
pyrmont_e62330 - altair processSlashingsReset 38.493 us/op 35.657 us/op 1.08
pyrmont_e62330 - altair processRandaoMixesReset 46.721 us/op 39.076 us/op 1.20
pyrmont_e62330 - altair processHistoricalRootsUpdate 7.6780 us/op 8.2790 us/op 0.93
pyrmont_e62330 - altair processParticipationFlagUpdates 46.709 ms/op 35.523 ms/op 1.31
pyrmont_e62330 - altair processSyncCommitteeUpdates 5.0710 us/op 6.0270 us/op 0.84
pyrmont_e62330 - altair afterProcessEpoch 132.20 ms/op 100.47 ms/op 1.32
altair processInactivityUpdates - 250000 normalcase 79.892 ms/op 62.388 ms/op 1.28
altair processInactivityUpdates - 250000 worstcase 82.401 ms/op 58.355 ms/op 1.41
altair processParticipationFlagUpdates - 250000 anycase 96.054 ms/op 88.570 ms/op 1.08
altair processRewardsAndPenalties - 250000 normalcase 143.12 ms/op 105.57 ms/op 1.36
altair processRewardsAndPenalties - 250000 worstcase 132.31 ms/op 114.24 ms/op 1.16
altair processSyncCommitteeUpdates - 250000 461.72 ms/op 334.90 ms/op 1.38
Tree 40 250000 create 677.68 ms/op 441.06 ms/op 1.54
Tree 40 250000 get(125000) 287.47 ns/op 218.26 ns/op 1.32
Tree 40 250000 set(125000) 1.9473 us/op 1.5250 us/op 1.28
Tree 40 250000 toArray() 44.474 ms/op 36.476 ms/op 1.22
Tree 40 250000 iterate all - toArray() + loop 47.350 ms/op 41.287 ms/op 1.15
Tree 40 250000 iterate all - get(i) 115.05 ms/op 98.092 ms/op 1.17
MutableVector 250000 create 27.068 ms/op 19.035 ms/op 1.42
MutableVector 250000 get(125000) 16.022 ns/op 12.029 ns/op 1.33
MutableVector 250000 set(125000) 711.93 ns/op 459.57 ns/op 1.55
MutableVector 250000 toArray() 9.7422 ms/op 7.5834 ms/op 1.28
MutableVector 250000 iterate all - toArray() + loop 10.364 ms/op 7.8243 ms/op 1.32
MutableVector 250000 iterate all - get(i) 3.9457 ms/op 3.0812 ms/op 1.28
Array 250000 create 6.1440 ms/op 4.2939 ms/op 1.43
Array 250000 clone - spread 2.1703 ms/op 1.8436 ms/op 1.18
Array 250000 get(125000) 0.91800 ns/op 0.84000 ns/op 1.09
Array 250000 set(125000) 1.0760 ns/op 0.73500 ns/op 1.46
Array 250000 iterate all - loop 152.75 us/op 112.59 us/op 1.36
aggregationBits - 2048 els - readonlyValues 271.27 us/op 193.10 us/op 1.40
aggregationBits - 2048 els - zipIndexesInBitList 46.159 us/op 32.341 us/op 1.43
ssz.Root.equals 1.5800 us/op 1.1600 us/op 1.36
ssz.Root.equals with valueOf() 2.1590 us/op 1.3760 us/op 1.57
byteArrayEquals with valueOf() 1.9160 us/op 1.3480 us/op 1.42
phase0 processBlock - 250000 vs - 7PWei normalcase 13.941 ms/op 11.444 ms/op 1.22
phase0 processBlock - 250000 vs - 7PWei worstcase 99.910 ms/op 74.235 ms/op 1.35
phase0 afterProcessEpoch - 250000 vs - 7PWei 239.95 ms/op 183.98 ms/op 1.30
phase0 beforeProcessEpoch - 250000 vs - 7PWei 737.37 ms/op 559.96 ms/op 1.32
phase0 processEpoch - mainnet_e58758 989.41 ms/op 770.58 ms/op 1.28
mainnet_e58758 - phase0 beforeProcessEpoch 597.73 ms/op 508.35 ms/op 1.18
mainnet_e58758 - phase0 processJustificationAndFinalization 114.00 us/op 106.90 us/op 1.07
mainnet_e58758 - phase0 processRewardsAndPenalties 99.093 ms/op 80.226 ms/op 1.24
mainnet_e58758 - phase0 processRegistryUpdates 89.927 us/op 78.348 us/op 1.15
mainnet_e58758 - phase0 processSlashings 6.1770 us/op 5.0320 us/op 1.23
mainnet_e58758 - phase0 processEth1DataReset 6.3460 us/op 6.9040 us/op 0.92
mainnet_e58758 - phase0 processEffectiveBalanceUpdates 10.071 ms/op 8.5585 ms/op 1.18
mainnet_e58758 - phase0 processSlashingsReset 34.476 us/op 32.546 us/op 1.06
mainnet_e58758 - phase0 processRandaoMixesReset 46.867 us/op 43.211 us/op 1.08
mainnet_e58758 - phase0 processHistoricalRootsUpdate 8.3120 us/op 7.9050 us/op 1.05
mainnet_e58758 - phase0 processParticipationRecordUpdates 31.686 us/op 30.976 us/op 1.02
mainnet_e58758 - phase0 afterProcessEpoch 200.21 ms/op 167.34 ms/op 1.20
phase0 processEffectiveBalanceUpdates - 250000 normalcase 13.391 ms/op 9.7129 ms/op 1.38
phase0 processEffectiveBalanceUpdates - 250000 worstcase 0.5 1.5058 s/op 1.2651 s/op 1.19
phase0 processRegistryUpdates - 250000 normalcase 94.512 us/op 83.829 us/op 1.13
phase0 processRegistryUpdates - 250000 badcase_full_deposits 3.9148 ms/op 3.5225 ms/op 1.11
phase0 processRegistryUpdates - 250000 worstcase 0.5 2.1469 s/op 1.7852 s/op 1.20
phase0 getAttestationDeltas - 250000 normalcase 41.570 ms/op 32.270 ms/op 1.29
phase0 getAttestationDeltas - 250000 worstcase 39.952 ms/op 31.946 ms/op 1.25
phase0 processSlashings - 250000 worstcase 39.114 ms/op 30.807 ms/op 1.27
shuffle list - 16384 els 15.276 ms/op 11.378 ms/op 1.34
shuffle list - 250000 els 213.44 ms/op 162.67 ms/op 1.31
getEffectiveBalances - 250000 vs - 7PWei 12.433 ms/op 9.2078 ms/op 1.35
computeDeltas 3.9237 ms/op 3.0832 ms/op 1.27
getPubkeys - index2pubkey - req 1000 vs - 250000 vc 2.8066 ms/op 2.3031 ms/op 1.22
getPubkeys - validatorsArr - req 1000 vs - 250000 vc 1.0921 ms/op 862.72 us/op 1.27
BLS verify - blst-native 2.2996 ms/op 1.7796 ms/op 1.29
BLS verifyMultipleSignatures 3 - blst-native 4.6944 ms/op 3.7231 ms/op 1.26
BLS verifyMultipleSignatures 8 - blst-native 10.171 ms/op 7.7871 ms/op 1.31
BLS verifyMultipleSignatures 32 - blst-native 37.693 ms/op 28.361 ms/op 1.33
BLS aggregatePubkeys 32 - blst-native 51.308 us/op 38.524 us/op 1.33
BLS aggregatePubkeys 128 - blst-native 196.51 us/op 150.25 us/op 1.31
getAttestationsForBlock 96.055 ms/op 69.678 ms/op 1.38
CheckpointStateCache - add get delete 39.253 us/op 14.867 us/op 2.64
validate gossip signedAggregateAndProof - struct 5.8201 ms/op 4.2741 ms/op 1.36
validate gossip signedAggregateAndProof - treeBacked 5.6750 ms/op 4.3165 ms/op 1.31
validate gossip attestation - struct 2.6770 ms/op 2.0373 ms/op 1.31
validate gossip attestation - treeBacked 2.7460 ms/op 2.0912 ms/op 1.31
Object access 1 prop 0.56400 ns/op 0.41300 ns/op 1.37
Map access 1 prop 0.54600 ns/op 0.37500 ns/op 1.46
Object get x1000 16.909 ns/op 13.916 ns/op 1.22
Map get x1000 1.1440 ns/op 0.89900 ns/op 1.27
Object set x1000 125.97 ns/op 82.302 ns/op 1.53
Map set x1000 81.239 ns/op 57.521 ns/op 1.41
Return object 10000 times 0.43530 ns/op 0.35860 ns/op 1.21
Throw Error 10000 times 7.9788 us/op 5.9114 us/op 1.35

by benchmarkbot/action

@dapplion dapplion marked this pull request as draft May 10, 2022 09:48
@dapplion dapplion changed the base branch from master to unstable May 27, 2022 04:35
@dapplion dapplion dismissed a stale review May 27, 2022 04:35

The base branch was changed.

@dapplion
Copy link
Contributor Author

dapplion commented Jul 12, 2022

Do not delete branch plz

@dapplion dapplion closed this Jul 12, 2022
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