perf: cache packed proof nonces for cheaper header/block hash#3899
Open
iho wants to merge 1 commit into
Open
Conversation
block.hash() / header.hash() re-bitpacked Proof nonces on every call. Keep the on-wire packed form when deserializing a Proof so subsequent Hash serialization skips pack_bits. Locally built proofs pack on demand; clear_packed_cache after mutating edge_bits/nonces (stratum share path). Closes mimblewimble#3372
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
block.hash()/header.hash()ultimately hash the PoWProofby bit-packing all nonces on every call. That packing is relatively expensive and runs very often during sync and validation.This caches the packed form when a
Proofis deserialized (the on-wire bytes we already read), so later hash/write uses that buffer instead of re-runningpack_bits.Closes #3372
Approach
packed_nonces: Option<Vec<u8>>onProof(ignored byPartialEq/ serde)Readable::readstores the packed bytes alongside extracted noncesWriteable::writeprefers the cache; otherwise packs on demand (mining / constructed proofs)Proof::clear_packed_cache()for safe mutation ofedge_bits/noncesLocally constructed proofs (mining templates, tests, genesis) keep
Noneand behave as before.Test plan
cargo test -p grin_core --lib pow::typescargo test -p grin_core --lib genesiscargo check -p grin_servers