Improve addrman Select() performance when buckets are nearly empty #6530

Merged
merged 1 commit into from Aug 26, 2015

Conversation

Projects
None yet
6 participants
@sipa
Member

sipa commented Aug 6, 2015

Earlier, when there are only a few entries in the addrman tables, Select() could spin for long periods of time (~200ms), resulting in high CPU usage (up to 40% here). This reduces it to around 0.3ms (0.5% CPU).

@dgenr8

This comment has been minimized.

Show comment
Hide comment
@dgenr8

dgenr8 Aug 7, 2015

Contributor

utACK

Contributor

dgenr8 commented Aug 7, 2015

utACK

@laanwj laanwj added the P2P label Aug 7, 2015

@TheBlueMatt

This comment has been minimized.

Show comment
Hide comment
@TheBlueMatt

TheBlueMatt Aug 8, 2015

Contributor

utACK

Contributor

TheBlueMatt commented Aug 8, 2015

utACK

@gmaxwell

This comment has been minimized.

Show comment
Hide comment
@gmaxwell

gmaxwell Aug 22, 2015

Member

ACK

Member

gmaxwell commented Aug 22, 2015

ACK

@laanwj

View changes

src/addrman.cpp
@@ -341,8 +341,11 @@ CAddrInfo CAddrMan::Select_()
while (1) {
int nKBucket = GetRandInt(ADDRMAN_TRIED_BUCKET_COUNT);
int nKBucketPos = GetRandInt(ADDRMAN_BUCKET_SIZE);
- if (vvTried[nKBucket][nKBucketPos] == -1)
- continue;
+ while (1) {

This comment has been minimized.

@laanwj

laanwj Aug 24, 2015

Member

Equivalent but maybe slightly more readable:

while(vvTried[nKBucket][nKBucketPos] == -1) {
    nKBucket = (nKBucket + insecure_rand()) % ADDRMAN_TRIED_BUCKET_COUNT;
    nKBucketPos = (nKBucketPos + insecure_rand()) % ADDRMAN_BUCKET_SIZE;
}
@laanwj

laanwj Aug 24, 2015

Member

Equivalent but maybe slightly more readable:

while(vvTried[nKBucket][nKBucketPos] == -1) {
    nKBucket = (nKBucket + insecure_rand()) % ADDRMAN_TRIED_BUCKET_COUNT;
    nKBucketPos = (nKBucketPos + insecure_rand()) % ADDRMAN_BUCKET_SIZE;
}

This comment has been minimized.

@sipa

sipa Aug 26, 2015

Member

Nit addressed.

@sipa

sipa Aug 26, 2015

Member

Nit addressed.

@dcousens

This comment has been minimized.

Show comment
Hide comment
@dcousens

dcousens Aug 26, 2015

Contributor

utACK

Contributor

dcousens commented Aug 26, 2015

utACK

@laanwj laanwj merged commit c33c11e into bitcoin:master Aug 26, 2015

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

laanwj added a commit that referenced this pull request Aug 26, 2015

Merge pull request #6530
c33c11e Improve addrman Select() performance when buckets are nearly empty (Pieter Wuille)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment