Skip to content

Conversation

@promag
Copy link
Contributor

@promag promag commented Dec 17, 2018

Instead of calling pushKV(hash, info), which incurs in duplicate key checks, call __pushKV which (currently) doesn't.

Improves RPC getrawmempool and REST /rest/mempool/contents.json.

Fixes #14765.

@promag
Copy link
Contributor Author

promag commented Dec 17, 2018

Thanks @MarcoFalke, the report says >100k. I don't think this matters for small mempool.

@promag
Copy link
Contributor Author

promag commented Dec 17, 2018

mempool with 100k transactions:

Before:

time bitcoin-cli -regtest getrawmempool true  > /dev/null
bitcoin-cli -regtest getrawmempool true > /dev/null  3.75s user 0.90s system 8% cpu 52.170 total

After:

time bitcoin-cli -regtest getrawmempool true  > /dev/null
bitcoin-cli -regtest getrawmempool true > /dev/null  3.43s user 0.73s system 45% cpu 9.174 total

I guess you could duplicate one of the benchmarks

Can you point me in the right direction?

or write a functional test.

What should I test?

@Empact
Copy link
Contributor

Empact commented Dec 18, 2018

Could call __pushKV instead.

@promag
Copy link
Contributor Author

promag commented Dec 18, 2018

I don't think that's "public"?

@Empact
Copy link
Contributor

Empact commented Dec 18, 2018

It's declared publicly, may be __ because it does not check the type of target, which is set properly here.

@promag
Copy link
Contributor Author

promag commented Dec 18, 2018

__pushKV was added in jgarzik/univalue@ceb1194.

Looks like it could be either private or have another name. I don't have strong preference as the goal is to avoid the duplicate key check, but I'm more inclined to not use the "internal/private/reserved" method.

OTOH it would be reasonable to add duplicate key checks to pushKVs and then we would have a performance regression.

Maybe we should rename __pushKV to pushKVUnchecked?

@promag promag force-pushed the 2018-12-speedup-getrawmempool branch from b243d5e to 26b5806 Compare February 21, 2019 17:01
@promag
Copy link
Contributor Author

promag commented Feb 21, 2019

@MarcoFalke not sure about the benchmark. This is a small change to fix the original issue. But I think univalue should have an official API to avoid duplicate key checks and so the benchmark should be there, or am I wrong?

Pushed with your suggestion.

@DrahtBot
Copy link
Contributor

DrahtBot commented Feb 25, 2019

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

No conflicts as of last run.

@promag promag force-pushed the 2018-12-speedup-getrawmempool branch from 26b5806 to 2d5cf4c Compare March 8, 2019 15:50
@promag
Copy link
Contributor Author

promag commented Mar 8, 2019

Rebased, switched to UniValue::__pushKV and adjusted the comment.

maflcko pushed a commit that referenced this pull request Apr 23, 2019
710a713 rpc: Speedup getaddressesbylabel (João Barbosa)

Pull request description:

  Fixes #15447. Same approach of #14984, this change avoids duplicate key check when building the JSON response in memory.

ACKs for commit 710a71:
  MarcoFalke:
    utACK 710a713
  ryanofsky:
    utACK 710a713. Just new comments and assert since last review.

Tree-SHA512: 77c95df9ff3793e348619aa070e6fd36df9da1b461d708ab146652cb3699f1a472ef6eb38dafdb8374375cbc97daef07635fcb0501961f167a023309513742e2
@maflcko maflcko added this to the 0.19.0 milestone May 7, 2019
@promag
Copy link
Contributor Author

promag commented May 14, 2019

Anything left to do here? This is pretty much the same as #15463.

maflcko pushed a commit to maflcko/bitcoin-core that referenced this pull request May 15, 2019
2d5cf4c rpc: Speedup getrawmempool when verbose=true (João Barbosa)

Pull request description:

  Instead of calling `pushKV(hash, info)`, which incurs in duplicate key checks, call `__pushKV` which (currently) doesn't.

  Improves RPC `getrawmempool` and REST `/rest/mempool/contents.json`.

  Fixes bitcoin#14765.

ACKs for commit 2d5cf4:

Tree-SHA512: c3e91371bb41f39e79dcef820815e1dc27fb689ca3c4bf3a00467d2215b3baecd44d9792f7a481577a5b7ae1fc6cbaa07b1cd62123b845082eba65b35c2b3ca5
@maflcko maflcko merged commit 2d5cf4c into bitcoin:master May 15, 2019
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request May 15, 2019
2d5cf4c rpc: Speedup getrawmempool when verbose=true (João Barbosa)

Pull request description:

  Instead of calling `pushKV(hash, info)`, which incurs in duplicate key checks, call `__pushKV` which (currently) doesn't.

  Improves RPC `getrawmempool` and REST `/rest/mempool/contents.json`.

  Fixes bitcoin#14765.

ACKs for commit 2d5cf4:

Tree-SHA512: c3e91371bb41f39e79dcef820815e1dc27fb689ca3c4bf3a00467d2215b3baecd44d9792f7a481577a5b7ae1fc6cbaa07b1cd62123b845082eba65b35c2b3ca5
@0xB10C
Copy link
Contributor

0xB10C commented May 15, 2019

I've seen this PR a bit late (just after it got merged), but fwiw here is some real world improvement data:

Time it takes to GET /rest/mempool/contents.json (querys over a minute apart):

in v0.18.0 :

57.9s for 59474 unconfirmed Transactions (36.11 MB)
48.7s for 60199 unconfirmed Transactions (37.02 MB) 
59.2s for 55544 unconfirmed Transactions (36.14 MB) 
45.5s for 55464 unconfirmed Transactions (35.45 MB)
avg. of 52.8s

v0.18.0 with the proposed change:

35.8s for 54308 unconfirmed Transactions (34.94 MB)
40.4s for 55065 unconfirmed Transactions (35.17 MB)
37.0s for 55914 unconfirmed Transactions (35.47 MB)
33.4s for 56739 unconfirmed Transactions (35.73 MB) 
avg. of 36.6s

Which for my sample is a reduction of 16.2s or 30%!! 🎉

@promag promag deleted the 2018-12-speedup-getrawmempool branch May 15, 2019 18:43
@promag
Copy link
Contributor Author

promag commented May 15, 2019

Thanks @0xB10C for the feedback.

@promag
Copy link
Contributor Author

promag commented May 16, 2019

This is an easy backport but I'm not sure if it's a candidate.

@promag
Copy link
Contributor Author

promag commented May 16, 2019

@laanwj I'm going to backport this 0.18.1, not sure if milestone should be changed.

@fanquake
Copy link
Member

@promag the milestone can remain 0.19.0 here, your backport PR will get 0.18.1.

@promag
Copy link
Contributor Author

promag commented May 23, 2019

@0xB10C after all this is not a backport candidate.

deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request May 8, 2020
Summary:
2d5cf4c41d rpc: Speedup getrawmempool when verbose=true (João Barbosa)

Pull request description:

  Instead of calling `pushKV(hash, info)`, which incurs in duplicate key checks, call `__pushKV` which (currently) doesn't.

  Improves RPC `getrawmempool` and REST `/rest/mempool/contents.json`.

  Fixes #14765.

ACKs for commit 2d5cf4:

Tree-SHA512: c3e91371bb41f39e79dcef820815e1dc27fb689ca3c4bf3a00467d2215b3baecd44d9792f7a481577a5b7ae1fc6cbaa07b1cd62123b845082eba65b35c2b3ca5

Backport of Core [[bitcoin/bitcoin#14984 | PR14984]]

Related to D5975

Test Plan:
Compile two versions of bitcoind, one with this patch and the other without,
then record timings for fetching the mempool via REST.
```
git ch master
ninja bitcoind bitcoin-cli
bitcoind -rest -connect=0     # -connect=0 ensures we do not download any new transactions while testing
bitcoin-cli invalidateblock 000000000000000000e311a3b4f53085a427a22dcc8c790b266d9f882c795564  # some block that is not recent
bitcoin-cli getmempoolinfo    # confirm you have at least thousands of txs in the mempool
for X in {1..100} ; do { time curl localhost:8332/rest/mempool/contents.json ; } 2>> results-without-patch ; done

git ch <this-patch>
ninja bitcoind bitcoin-cli
bitcoind -rest -connect=0     # -connect=0 ensures we do not download any new transactions while testing
bitcoin-cli getmempoolinfo    # confirm you have the same number of transactions in the mempool as the previous test
for X in {1..100} ; do { time curl localhost:8332/rest/mempool/contents.json ; } 2>> results-with-patch ; done
```

Now compare the average of the timings:
```
cat results-without-patch | grep real | sed 's/real[ \t]//g' | sed 's/[ms]//g' | awk '{total+=$1; count+=1} END {print total/count}'
cat results-with-patch | grep real | sed 's/real[ \t]//g' | sed 's/[ms]//g' | awk '{total+=$1; count+=1} END {print total/count}'
```

My results:
Without patch: `0.32937`
With patch: `0.17476`
**~47% reduction**

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D6007
pravblockc pushed a commit to pravblockc/dash that referenced this pull request Sep 29, 2021
710a713 rpc: Speedup getaddressesbylabel (João Barbosa)

Pull request description:

  Fixes bitcoin#15447. Same approach of bitcoin#14984, this change avoids duplicate key check when building the JSON response in memory.

ACKs for commit 710a71:
  MarcoFalke:
    utACK 710a713
  ryanofsky:
    utACK 710a713. Just new comments and assert since last review.

Tree-SHA512: 77c95df9ff3793e348619aa070e6fd36df9da1b461d708ab146652cb3699f1a472ef6eb38dafdb8374375cbc97daef07635fcb0501961f167a023309513742e2
dzutto pushed a commit to dzutto/dash that referenced this pull request Oct 12, 2021
2d5cf4c rpc: Speedup getrawmempool when verbose=true (João Barbosa)

Pull request description:

  Instead of calling `pushKV(hash, info)`, which incurs in duplicate key checks, call `__pushKV` which (currently) doesn't.

  Improves RPC `getrawmempool` and REST `/rest/mempool/contents.json`.

  Fixes bitcoin#14765.

ACKs for commit 2d5cf4:

Tree-SHA512: c3e91371bb41f39e79dcef820815e1dc27fb689ca3c4bf3a00467d2215b3baecd44d9792f7a481577a5b7ae1fc6cbaa07b1cd62123b845082eba65b35c2b3ca5
UdjinM6 added a commit to dashpay/dash that referenced this pull request Oct 13, 2021
pravblockc pushed a commit to pravblockc/dash that referenced this pull request Nov 18, 2021
2d5cf4c rpc: Speedup getrawmempool when verbose=true (João Barbosa)

Pull request description:

  Instead of calling `pushKV(hash, info)`, which incurs in duplicate key checks, call `__pushKV` which (currently) doesn't.

  Improves RPC `getrawmempool` and REST `/rest/mempool/contents.json`.

  Fixes bitcoin#14765.

ACKs for commit 2d5cf4:

Tree-SHA512: c3e91371bb41f39e79dcef820815e1dc27fb689ca3c4bf3a00467d2215b3baecd44d9792f7a481577a5b7ae1fc6cbaa07b1cd62123b845082eba65b35c2b3ca5
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getrawmempool true RPC call is O(n^2)

6 participants