block.hash() unnecessarily expensive to call
#3.372 aberto em 29 de jun. de 2020
Métricas do repositório
- Stars
- (4.876 stars)
- Métricas de merge de PR
- (Mesclagem média 6d 11h) (25 fundiu PRs em 30d)
Description
We call write() on Writeable when serializing things to bytes.
Not just for serializing full data but also when we serialize during a call to hash().
Block hash -> BlockHeader hash -> ProofOfWork hash -> Proof hash
Every call to block.hash() or header.hash() delegates to the following code to produce the data to be hashed -
We build the bitvec "on the fly" every time we call hash().
Ideally hash() on any type is as cheap as possible.
I suspect, but have no benchmarks or data to confirm, that this is relatively expensive over time. We make a lot of calls to block.hash() and header.hash().
Possible improvement -
- Consider initializing the bitvec when we initially
readthe Proof instance. - Use this "cached" value when we write it in Hash serialization mode.
- Ignore the "cached" value when writing full data.
- Make sure
noncesoredge_bitscannot be written to or modified in unexpected ways, leaving the cache stale.
Related - https://github.com/mimblewimble/grin/issues/2584
Maybe we don't need to cache "hashes" in a general way (yet), maybe simply making block.hash() cheap will give us most of the benefit for the least effort.