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

net: Optimize locator construction for "getblocks" messages #1880

Merged

Conversation

cyrossignol
Copy link
Member

@cyrossignol cyrossignol commented Sep 14, 2020

This caches the block locators used to ask for a range of blocks. Due to a sub-optimal workaround in our old net messaging code, a node will ask each peer that advertises a block for the next range. The node generates a subset of hashes from the current blockchain used as a locator for the block in the chain of the peer. Creating locators is extremely expensive—a node needs to scan the entire chain—so this PR adds a cache for the locators and reuses them if the node sends the same request. For nodes with many connections, this can dramatically improve the performance of the messaging system when it needs to respond to new blocks.

This optimization will become unnecessary when we backport newer chain and net messaging code from Bitcoin. For now, this cache can greatly improve a node's ability to serve a higher number of connections, and it reduces some CPU usage for non-listening nodes as well.

Here's the observed CPU usage of a VPS that serves almost 400 connections after applying the optimization on 09-11:

image

This should reduce the huge ping and latency spikes that I see on large listening nodes when a new block arrives (up to 60 seconds sometimes). The same problem causes the high CPU usage when synchronizing blocks near the chain tip. I'm working on a more general solution for that.

This caches the block locators used to ask for a range of blocks. Due to a
sub-optimal workaround in our old net messaging code, a node will ask each
peer that advertises a block for the next range. The node generates a sub-
set of hashes from the current block chain used as a locator for the block
in the chain of the peer. Creating locators is extremely expensive--a node
needs to scan the entire chain--so we cache the locators and reuse them if
the node sends the same request. For nodes with many connections, this can
dramatically improve the performance of the messaging system when it needs
to respond to new blocks.

This optimization will become unnecessary when we backport newer chain and
net messaging code from Bitcoin. For now, this cache can greatly improve a
node's ability to serve a higher number of connections.
Copy link
Member

@jamescowens jamescowens left a comment

Choose a reason for hiding this comment

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

Very simple. Looks good I think. utACK.

@jamescowens jamescowens added this to the Gladys milestone Sep 14, 2020
@jamescowens jamescowens merged commit 67e0145 into gridcoin-community:development Sep 14, 2020
@cyrossignol cyrossignol deleted the optimize-locator branch September 14, 2020 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants