Use RlpArrayBufWriter for rlp encoding in stateroot computation#4115
Use RlpArrayBufWriter for rlp encoding in stateroot computation#4115
Conversation
|
Benchmark results... The in memory unit test is at least 2x faster with this change. For the full stateroot computation on startup most of the time is spent reading and writing to the database so the speedup is less noticeable but still significant. master branch: RlpArrayBufWriter branch: master branch: Memory usage - 6.1G NTC 2026-04-11 23:53:12.103+08:00 Importing era1 archive start=5000001 dataDir=/home/user/.local/state/nimbus/mainnet era1Dir=/media/user/BlockchainData/era1/mainnet RlpArrayBufWriter branch: Memory usage - 6.1G NTC 2026-04-11 23:45:14.974+08:00 Importing era1 archive start=5000001 dataDir=/home/user/.local/state/nimbus/mainnet era1Dir=/media/user/BlockchainData/era1/mainnet |
Jenkins BuildsClick to see older builds (5)
|
The RlpArrayBufWriter only uses stack memory for the rlp encoding and using it improves the performance of the stateroot computation. When returning the rlp bytes we also don't allocate any seqs.
The RlpArrayBufWriter is based on the RlpDefaultWriter but replaces the usages of seqs with ArrayBufs to avoid the heap allocations. It turns out that these writers are faster than the two pass writer for small/simple rlp encodings and so this implementation is best suited to the stateroot computation which only encodes simple objects.
Also I don't think using the RlpHashWriter to rlp and hash in place will be any faster than this because it does two passes of the data (similar to the two pass writer). It needs to do this to calculate the length prefix but it turns out that going over the data 2 times is slower (at least this was my conclusion from doing some quick benchmarks).