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

refactor: Avoid UniValue copies #25429

Closed
wants to merge 3 commits into from

Conversation

maflcko
Copy link
Member

@maflcko maflcko commented Jun 20, 2022

UniValue copies have been a source of performance related bugs and regressions, so it would be good to remove the copy constructors.

This is the first change toward the goal. This change uses a const UniValue& where possible.

See also:

@maflcko
Copy link
Member Author

maflcko commented Jun 20, 2022

Review note: const will compile, but kill performance when std::move or RVO is intended.

@theStack
Copy link
Contributor

Concept ACK

@fanquake
Copy link
Member

cc @martinus

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 20, 2022

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

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept ACK theStack, hebasto
Stale ACK martinus, fanquake

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #26988 ([rpc]: Add test-only RPC addrmaninfo for new/tried table address count by stratospher)
  • #26840 (refactor: importpubkey, importprivkey, importaddress, importmulti, and importdescriptors rpc by KolbyML)
  • #26780 (rpc: simplify scan blocks by furszy)
  • #26621 (refactor: Continue moving application data from CNode to Peer by dergoegge)
  • #26612 (refactor: RPC: pass named argument value as string_view by stickies-v)
  • #26426 (WIP: Fix coinstatsindex overflow issue by fjahr)
  • #25974 (test, build: Separate read_json function into its own module by hebasto)
  • #25390 (sync: introduce a thread-safe generic container and use it to remove a bunch of "GlobalMutex"es by vasild)
  • #25344 (New outputs argument for bumpfee/psbtbumpfee by rodentrabies)
  • #15294 (refactor: Extract RipeMd160 by Empact)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@martinus
Copy link
Contributor

martinus commented Jun 21, 2022

Nice! concept ACK. How about removing the copy constructor and copy assignment from the UniValue class, as done here? Fixing all copies would make the PR much bigger though.

@maflcko
Copy link
Member Author

maflcko commented Jun 22, 2022

I am actually not sure how to fix all copies. Sometimes the std lib will copy if asked to:

std::vector<UniValue> a;
std::vector<UniValue> b;
a.insert(a.end(), b.begin(), b.end());

@laanwj
Copy link
Member

laanwj commented Jun 22, 2022

I guess you could add an explicit 'clone' method (like rust) and use that when it's really necessary to make a copy. It will probably make some things much more verbose though.

@martinus
Copy link
Contributor

a.insert(a.end(), b.begin(), b.end());

In that case you can use

a.insert(a.end(), std::make_move_iterator(b.begin()), std::make_move_iterator(b.end()));

But I there might still be case where copy constructor is necessary, I remember that I had difficulties in my old PR as well

@fanquake
Copy link
Member

Concept ACK

Copy link
Member

@fanquake fanquake left a comment

Choose a reason for hiding this comment

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

ACK bbbbcfd - as a first step.

@maflcko
Copy link
Member Author

maflcko commented Jun 29, 2022

While the changes are correct, I'll try to make them easier to review and more complete. For now, please review actual UniValue-copy bugfixes, like #25464

@hebasto
Copy link
Member

hebasto commented Dec 27, 2022

Concept ACK.

maflcko pushed a commit to bitcoin-core/gui that referenced this pull request Jan 11, 2023
…move` clang-tidy check

9567bfe clang-tidy: Add `performance-no-automatic-move` check (Hennadii Stepanov)

Pull request description:

  Split from bitcoin/bitcoin#26642 as [requested](bitcoin/bitcoin#26642 (comment)).

  For the problem description see https://clang.llvm.org/extra/clang-tidy/checks/performance/no-automatic-move.html.

  The following types are affected:
  - `std::pair<CAddress, NodeSeconds>`
  - `std::vector<CAddress>`
  - `UniValue`, also see bitcoin/bitcoin#25429
  - `QColor`
  - `CBlock`
  - `MempoolAcceptResult`
  - `std::shared_ptr<CWallet>`
  - `std::optional<SelectionResult>`
  - `CTransactionRef`, which is `std::shared_ptr<const CTransaction>`

ACKs for top commit:
  andrewtoth:
    ACK 9567bfe
  aureleoules:
    ACK 9567bfe

Tree-SHA512: 9b6a5d539205b41d2c86402d384318ed2e1d89e66333ebd200a48fd7df3ce6f6c60a3e989eda5cc503fb34b8d82526f95e56776e1af51e63b49e3a1fef72dbcb
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jan 11, 2023
…ang-tidy check

9567bfe clang-tidy: Add `performance-no-automatic-move` check (Hennadii Stepanov)

Pull request description:

  Split from bitcoin#26642 as [requested](bitcoin#26642 (comment)).

  For the problem description see https://clang.llvm.org/extra/clang-tidy/checks/performance/no-automatic-move.html.

  The following types are affected:
  - `std::pair<CAddress, NodeSeconds>`
  - `std::vector<CAddress>`
  - `UniValue`, also see bitcoin#25429
  - `QColor`
  - `CBlock`
  - `MempoolAcceptResult`
  - `std::shared_ptr<CWallet>`
  - `std::optional<SelectionResult>`
  - `CTransactionRef`, which is `std::shared_ptr<const CTransaction>`

ACKs for top commit:
  andrewtoth:
    ACK 9567bfe
  aureleoules:
    ACK 9567bfe

Tree-SHA512: 9b6a5d539205b41d2c86402d384318ed2e1d89e66333ebd200a48fd7df3ce6f6c60a3e989eda5cc503fb34b8d82526f95e56776e1af51e63b49e3a1fef72dbcb
MacroFake added 3 commits January 21, 2023 11:20
std::pair<A, B> is different from std::map<A, B>::value_type (aka
std::pair<const A, B>). So fix the type to also avoid a copy.
@DrahtBot
Copy link
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

@maflcko maflcko closed this Feb 23, 2023
@maflcko maflcko deleted the 2206-uni-copy-👾 branch February 23, 2023 17:52
@bitcoin bitcoin locked and limited conversation to collaborators Feb 23, 2024
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.

None yet

7 participants