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

wallet: Avoid multiple BlockHash() calculations #2295

Merged
merged 5 commits into from
Nov 9, 2023

Conversation

matheusd
Copy link
Member

@matheusd matheusd commented Nov 9, 2023

This PR makes some changes to the wallet, spv syncing and peer packages to avoid having to hash the exact same header multiple times.

This improves both cpu and memory allocation performance during the initial sync process.

On an informal sync on mainnet using spvconnect, this reduces the space allocated by BlockHash() calls by ~60% and the time spent in these functions by ~72%.

While this would be naturally checked later in the startup sync process,
doing it in the p2p package just after receiving the header message
avoids performing useless work if the peer is misbehaving.
This moves the code that initializes a batch of wallet.BlockNodes to
outside the cfilter fetching code during initial sync.

This will make it easier to move around code in later refactorings.
This is not currently used for anything.
This makes a change to the block node initialization code to, instead of
calculating the block hash of each header, use the PrevBlock member of
the header when multiple headers are available.

This is safe to do because the lower P2P level has already validated
that a received header batch connects in sequence (and therefore the
PrevBlock of a header actually is equal to the prior header's block hash).

This avoids unnecessarily performing duplicate work in calculating the
block hash and reduces overall cpu usage, specially during the initial
sync process.
This changes the signature of the ExtendMainChain functions in the
wallet and udb to directly take header's block hash instead of
recalculating.

This avoids having to re-hash a header that has already been hashed
earlier in the call stack, reducing both cpu and allocated space
requirements during the initial sync process.
@jrick jrick merged commit 541381a into decred:master Nov 9, 2023
2 checks passed
@matheusd matheusd deleted the avoid-multiple-hashing branch November 9, 2023 15:41
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.

2 participants