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

Track balances as Number64UintType #2973

Merged
merged 9 commits into from
Aug 31, 2021
Merged

Conversation

twoeths
Copy link
Contributor

@twoeths twoeths commented Aug 19, 2021

Motivation

Track balances as the new Number64UintType of ssz for faster processing, same for slot, epoch

Description

This depends on ChainSafe/ssz#159

  • For all Number64 type, use the new Number64UintType of ssz
  • Track balances as a number too new ListType({elementType: Number64, limit: VALIDATOR_REGISTRY_LIMIT})
  • Update CachedBalancesList: remove persistent-ts array there to save memory, move it to balanceList.ts
  • Increase balance/decrease balance: use the new api tree_applyDeltaAtIndex() or tree_applyDeltaInBatch() to avoid the pattern balances[i] = balances[i] + delta (which have to do deserialize then serialize)
  • processRewardsAndPenalties: use the new api tree_newTreeFromDeltas() to build balances tree and avoid BigInt calculation and Uint8Array allocation, it return new number[] array to be used for next step of epoch transition

Closes #2972 #2049

More related improvement could be done after this PR:

@codeclimate
Copy link

codeclimate bot commented Aug 19, 2021

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

View more on Code Climate.

@dapplion
Copy link
Contributor

Killer improvements!! 🔥 amazing job

@twoeths twoeths force-pushed the tuyen/balance-as-number64 branch from b99accc to d1cdc41 Compare August 25, 2021 00:56
@twoeths
Copy link
Contributor Author

twoeths commented Aug 25, 2021

Killer improvements!! 🔥 amazing job

just want to clarify that most of the improvement come from the new ssz and we already merge that to master. The main improvement for this PR is processRewardsAndPenalties and removing a separate balances persistent-ts cache

@twoeths twoeths force-pushed the tuyen/balance-as-number64 branch from d1cdc41 to 5286a5d Compare August 26, 2021 02:24
@twoeths twoeths force-pushed the tuyen/balance-as-number64 branch from 5286a5d to fa28b64 Compare August 31, 2021 01:48
@github-actions
Copy link
Contributor

github-actions bot commented Aug 31, 2021

Performance Report

✔️ no performance regression detected

🚀🚀 Significant benchmark improvement detected

Benchmark suite Current: 4ca69eb Previous: 131c6cc Ratio
altair processRewardsAndPenalties - 250000 normalcase 131.26 ms/op 395.39 ms/op 0.33
Full benchmark results
Benchmark suite Current: 4ca69eb Previous: 131c6cc Ratio
BeaconState.hashTreeRoot - No change 767.00 ns/op 939.00 ns/op 0.82
BeaconState.hashTreeRoot - 1 full validator 87.770 us/op 94.952 us/op 0.92
BeaconState.hashTreeRoot - 32 full validator 1.1485 ms/op 1.2492 ms/op 0.92
BeaconState.hashTreeRoot - 512 full validator 14.642 ms/op 15.068 ms/op 0.97
BeaconState.hashTreeRoot - 1 validator.effectiveBalance 79.910 us/op 83.477 us/op 0.96
BeaconState.hashTreeRoot - 32 validator.effectiveBalance 1.0433 ms/op 1.1475 ms/op 0.91
BeaconState.hashTreeRoot - 512 validator.effectiveBalance 11.253 ms/op 11.481 ms/op 0.98
BeaconState.hashTreeRoot - 1 balances 75.243 us/op 78.802 us/op 0.95
BeaconState.hashTreeRoot - 32 balances 634.23 us/op 654.95 us/op 0.97
BeaconState.hashTreeRoot - 512 balances 5.9290 ms/op 5.9032 ms/op 1.00
BeaconState.hashTreeRoot - 250000 balances 108.46 ms/op 115.70 ms/op 0.94
processSlot - 1 slots 56.771 us/op 81.437 us/op 0.70
processSlot - 32 slots 2.8598 ms/op 3.7095 ms/op 0.77
getCommitteeAssignments - req 1 vs - 250000 vc 5.5190 ms/op 5.8059 ms/op 0.95
getCommitteeAssignments - req 100 vs - 250000 vc 7.5588 ms/op 8.4460 ms/op 0.89
getCommitteeAssignments - req 1000 vs - 250000 vc 8.1215 ms/op 8.9004 ms/op 0.91
altair processBlock - 250000 vs - 7PWei normalcase 106.57 ms/op 160.96 ms/op 0.66
altair processBlock - 250000 vs - 7PWei worstcase 276.75 ms/op 377.06 ms/op 0.73
altair processEpoch - pyrmont_e62330 501.28 ms/op 734.68 ms/op 0.68
pyrmont_e62330 - altair beforeProcessEpoch 102.55 ms/op 122.04 ms/op 0.84
pyrmont_e62330 - altair processJustificationAndFinalization 129.81 us/op 204.90 us/op 0.63
pyrmont_e62330 - altair processInactivityUpdates 75.005 ms/op 82.089 ms/op 0.91
pyrmont_e62330 - altair processRewardsAndPenalties 68.948 ms/op 172.73 ms/op 0.40
pyrmont_e62330 - altair processRegistryUpdates 17.316 us/op 23.350 us/op 0.74
pyrmont_e62330 - altair processSlashings 5.9700 us/op 6.6630 us/op 0.90
pyrmont_e62330 - altair processEth1DataReset 6.2110 us/op 5.8430 us/op 1.06
pyrmont_e62330 - altair processEffectiveBalanceUpdates 33.127 ms/op 28.086 ms/op 1.18
pyrmont_e62330 - altair processSlashingsReset 28.724 us/op 36.280 us/op 0.79
pyrmont_e62330 - altair processRandaoMixesReset 43.780 us/op 50.481 us/op 0.87
pyrmont_e62330 - altair processHistoricalRootsUpdate 7.1780 us/op 6.9780 us/op 1.03
pyrmont_e62330 - altair processParticipationFlagUpdates 45.397 ms/op 53.234 ms/op 0.85
pyrmont_e62330 - altair processSyncCommitteeUpdates 5.3150 us/op 5.1280 us/op 1.04
pyrmont_e62330 - altair afterProcessEpoch 119.89 ms/op 134.70 ms/op 0.89
altair processInactivityUpdates - 250000 normalcase 222.94 ms/op 287.12 ms/op 0.78
altair processInactivityUpdates - 250000 worstcase 234.47 ms/op 298.93 ms/op 0.78
altair processParticipationFlagUpdates - 250000 anycase 94.457 ms/op 102.95 ms/op 0.92
altair processRewardsAndPenalties - 250000 normalcase 131.26 ms/op 395.39 ms/op 0.33
altair processRewardsAndPenalties - 250000 worstcase 146.58 ms/op 369.12 ms/op 0.40
altair processSyncCommitteeUpdates - 250000 438.78 ms/op 527.48 ms/op 0.83
Tree 40 250000 create 633.69 ms/op 726.06 ms/op 0.87
Tree 40 250000 get(125000) 259.62 ns/op 1.8044 us/op 0.14
Tree 40 250000 set(125000) 1.8798 us/op 2.2520 us/op 0.83
Tree 40 250000 toArray() 44.093 ms/op 49.935 ms/op 0.88
Tree 40 250000 iterate all - toArray() + loop 44.263 ms/op 51.294 ms/op 0.86
Tree 40 250000 iterate all - get(i) 111.62 ms/op 518.83 ms/op 0.22
MutableVector 250000 create 24.519 ms/op 33.400 ms/op 0.73
MutableVector 250000 get(125000) 16.533 ns/op 16.437 ns/op 1.01
MutableVector 250000 set(125000) 748.82 ns/op 794.52 ns/op 0.94
MutableVector 250000 toArray() 8.6836 ms/op 10.445 ms/op 0.83
MutableVector 250000 iterate all - toArray() + loop 9.2044 ms/op 9.8675 ms/op 0.93
MutableVector 250000 iterate all - get(i) 4.2214 ms/op 4.2240 ms/op 1.00
Array 250000 create 6.6261 ms/op 6.2140 ms/op 1.07
Array 250000 clone - spread 2.6909 ms/op 2.9393 ms/op 0.92
Array 250000 get(125000) 0.87900 ns/op 0.95900 ns/op 0.92
Array 250000 set(125000) 0.93900 ns/op 0.99400 ns/op 0.94
Array 250000 iterate all - loop 132.68 us/op 151.38 us/op 0.88
aggregationBits - 2048 els - readonlyValues 295.18 us/op 347.09 us/op 0.85
aggregationBits - 2048 els - zipIndexesInBitList 60.007 us/op 34.975 us/op 1.72
ssz.Root.equals 1.4980 us/op 1.9850 us/op 0.75
ssz.Root.equals with valueOf() 1.7230 us/op 2.4580 us/op 0.70
byteArrayEquals with valueOf() 1.7750 us/op 2.4790 us/op 0.72
phase0 processBlock - 250000 vs - 7PWei normalcase 14.191 ms/op 22.072 ms/op 0.64
phase0 processBlock - 250000 vs - 7PWei worstcase 103.14 ms/op 129.30 ms/op 0.80
phase0 afterProcessEpoch - 250000 vs - 7PWei 217.14 ms/op 243.09 ms/op 0.89
phase0 beforeProcessEpoch - 250000 vs - 7PWei 676.13 ms/op 1.0153 s/op 0.67
phase0 processEpoch - mainnet_e58758 892.06 ms/op 1.3514 s/op 0.66
mainnet_e58758 - phase0 beforeProcessEpoch 452.03 ms/op 755.26 ms/op 0.60
mainnet_e58758 - phase0 processJustificationAndFinalization 132.50 us/op 189.37 us/op 0.70
mainnet_e58758 - phase0 processRewardsAndPenalties 99.137 ms/op 293.35 ms/op 0.34
mainnet_e58758 - phase0 processRegistryUpdates 122.53 us/op 114.35 us/op 1.07
mainnet_e58758 - phase0 processSlashings 6.4340 us/op 5.4290 us/op 1.19
mainnet_e58758 - phase0 processEth1DataReset 6.1600 us/op 5.2710 us/op 1.17
mainnet_e58758 - phase0 processEffectiveBalanceUpdates 40.146 ms/op 33.597 ms/op 1.19
mainnet_e58758 - phase0 processSlashingsReset 31.175 us/op 33.485 us/op 0.93
mainnet_e58758 - phase0 processRandaoMixesReset 44.601 us/op 48.676 us/op 0.92
mainnet_e58758 - phase0 processHistoricalRootsUpdate 7.6860 us/op 6.8800 us/op 1.12
mainnet_e58758 - phase0 processParticipationRecordUpdates 29.554 us/op 34.647 us/op 0.85
mainnet_e58758 - phase0 afterProcessEpoch 196.46 ms/op 217.77 ms/op 0.90
phase0 processEffectiveBalanceUpdates - 250000 normalcase 48.489 ms/op 47.998 ms/op 1.01
phase0 processEffectiveBalanceUpdates - 250000 worstcase 0.5 3.0603 s/op 3.6807 s/op 0.83
phase0 processRegistryUpdates - 250000 normalcase 133.17 us/op 123.61 us/op 1.08
phase0 processRegistryUpdates - 250000 badcase_full_deposits 6.0924 ms/op 8.0497 ms/op 0.76
phase0 processRegistryUpdates - 250000 worstcase 0.5 3.0630 s/op 3.6215 s/op 0.85
phase0 getAttestationDeltas - 250000 normalcase 54.270 ms/op 66.120 ms/op 0.82
phase0 getAttestationDeltas - 250000 worstcase 53.853 ms/op 67.284 ms/op 0.80
phase0 processSlashings - 250000 worstcase 40.480 ms/op 111.22 ms/op 0.36
shuffle list - 16384 els 13.184 ms/op 15.719 ms/op 0.84
shuffle list - 250000 els 191.13 ms/op 222.61 ms/op 0.86
getPubkeys - index2pubkey - req 1000 vs - 250000 vc 2.4937 ms/op 2.8330 ms/op 0.88
getPubkeys - validatorsArr - req 1000 vs - 250000 vc 488.60 us/op 534.56 us/op 0.91
getPubkeys - persistent - req 1000 vs - 250000 vc 17.965 us/op 21.390 us/op 0.84
BLS verify - blst-native 2.4110 ms/op 2.4430 ms/op 0.99
BLS verifyMultipleSignatures 3 - blst-native 4.9444 ms/op 4.9663 ms/op 1.00
BLS verifyMultipleSignatures 8 - blst-native 10.987 ms/op 10.575 ms/op 1.04
BLS verifyMultipleSignatures 32 - blst-native 42.098 ms/op 36.150 ms/op 1.16
BLS aggregatePubkeys 32 - blst-native 55.738 us/op 50.122 us/op 1.11
BLS aggregatePubkeys 128 - blst-native 215.15 us/op 200.45 us/op 1.07
getAttestationsForBlock 86.997 ms/op 114.23 ms/op 0.76
CheckpointStateCache - add get delete 20.345 us/op 31.920 us/op 0.64
validate gossip signedAggregateAndProof - struct 6.1910 ms/op 5.8467 ms/op 1.06
validate gossip signedAggregateAndProof - treeBacked 6.1976 ms/op 5.8662 ms/op 1.06
validate gossip attestation - struct 2.7721 ms/op 2.7244 ms/op 1.02
validate gossip attestation - treeBacked 2.8498 ms/op 2.7202 ms/op 1.05

by benchmarkbot/action

@twoeths twoeths marked this pull request as ready for review August 31, 2021 02:49
@twoeths
Copy link
Contributor Author

twoeths commented Aug 31, 2021

# this branch master ratio
pyrmont_e62330 - altair processRewardsAndPenalties 78.252 ms/op 213.63 ms/op 0.37
altair processRewardsAndPenalties - 250000 normalcase 123.93 ms/op 338.31 ms/op 0.37
altair processRewardsAndPenalties - 250000 worstcase 139.13 ms/op 281.75 ms/op 0.49
mainnet_e58758 - phase0 processRewardsAndPenalties 102.81 ms/op 273.29 ms/op 0.38

Copy link
Contributor

@dapplion dapplion left a comment

Choose a reason for hiding this comment

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

Looks great nice work!

@dapplion dapplion merged commit 1629df1 into master Aug 31, 2021
@dapplion dapplion deleted the tuyen/balance-as-number64 branch August 31, 2021 06:55
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.

Track balance as Number
2 participants