Skip to content

Conversation

maflcko
Copy link
Member

@maflcko maflcko commented Oct 4, 2023

Constructing a temporary unnamed object only to copy or move it into a container seems both verbose in code and a strict performance penalty.

Fix both issues via the modernize-use-emplace tidy check.

@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 4, 2023

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

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK Sjors, hebasto, TheCharlatan
Concept ACK fanquake
Stale ACK aureleoules

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:

  • #28609 (wallet: Reload watchonly and solvables wallets after migration by achow101)
  • #28560 (wallet, rpc: FundTransaction refactor by josibake)
  • #28201 (Silent Payments: sending by josibake)
  • #27865 (wallet: Track no-longer-spendable TXOs separately by achow101)
  • #27286 (wallet: Keep track of the wallet's own transaction outputs in memory by achow101)
  • #26711 (validate package transactions with their in-package ancestor sets by glozow)
  • #26326 (net: don't lock cs_main while reading blocks in net processing by andrewtoth)
  • #25722 (refactor: Use util::Result class for wallet loading by ryanofsky)
  • #25273 (wallet: Pass through transaction locktime and preset input sequences and scripts to CreateTransaction by achow101)
  • #21283 (Implement BIP 370 PSBTv2 by achow101)

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.

@hebasto
Copy link
Member

hebasto commented Oct 4, 2023

Concept ACK.

@hebasto
Copy link
Member

hebasto commented Oct 4, 2023

Can we do something with code like that:

result.push_back(JSONRPCRequestObj("getnetworkinfo", NullUniValue, ID_NETWORKINFO));
?

(however, not related to this PR changes)

@maflcko
Copy link
Member Author

maflcko commented Oct 4, 2023

Can we do something with code like that:

result.push_back(JSONRPCRequestObj("getnetworkinfo", NullUniValue, ID_NETWORKINFO));

?

(however, not related to this PR changes)

No. JSONRPCRequestObj is not a constructor, but a function call, so I don't think anything can be done there to improve anything. Also, recall that UniValue::push_back takes an UniValue, which avoids a copy, starting with C++17.

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

ACK dddd734, I've followed clang-tidy's warnings locally.

In your opinion, which value of the IgnoreImplicitConstructors options is better/safer for us?

@maflcko
Copy link
Member Author

maflcko commented Oct 9, 2023

In your opinion, which value of the IgnoreImplicitConstructors options is better/safer for us?

Not sure why that option exists. Edit: Looking at the tests (clang-tools-extra/test/clang-tidy/checkers/modernize/use-emplace-ignore-implicit-constructors.cpp) and the documentation, I could not figure out the use case.

Unrelated: An emplace function is able to call explicit constructors, even though they won't be spelled out explicitly in the source code. A push function is not able to call explicit constructors. However, if the code compiles with a push function, it should also compile with an emplace function.

@DrahtBot DrahtBot removed the CI failed label Oct 9, 2023
@fanquake
Copy link
Member

Concept ACK

@Sjors
Copy link
Member

Sjors commented Oct 11, 2023

utACK dddd734

A push function is not able to call explicit constructors. However, if the code compiles with a push function, it should also compile with an emplace function.

Someone on the internet says that this ability to call explicit constructors can sometimes lead to bugs. But since everything here was first compiled with push_back, that's not an issue. Just something to keep an eye on for new uses of emplace_back.

@DrahtBot DrahtBot requested a review from fanquake October 11, 2023 10:03
@maflcko
Copy link
Member Author

maflcko commented Oct 11, 2023

@hebasto For reference, I edited my previous replies. I realized that UniValue::push_back has a different behavior/signature compared to std::vector<UniValue>::push_back.

Copy link
Contributor

@aureleoules aureleoules left a comment

Choose a reason for hiding this comment

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

ACK dddd734

@maflcko
Copy link
Member Author

maflcko commented Oct 11, 2023

Someone on the internet says that this ability to call explicit constructors can sometimes lead to bugs. But since everything here was first compiled with push_back, that's not an issue. Just something to keep an eye on for new uses of emplace_back.

Jup, this should only affect new code. Other things to consider: Named args and designated initializers won't be possible with emplace. Also: Theoretically there could be resource leak, when using new to construct an argument passed to emplace on a container that holds smart pointers. But no new instances are introduced in this pull and fixing this issue is unrelated from this pull and already exists on current master (in tests):

src/test/denialofservice_tests.cpp:    vNodes.emplace_back(new CNode{id++,
src/test/fuzz/coinscache_sim.cpp:            caches.emplace_back(new CCoinsViewCache(&bottom, /*deterministic=*/true));
src/test/fuzz/coinscache_sim.cpp:                    caches.emplace_back(new CCoinsViewCache(&*caches.back(), /*deterministic=*/true));

@maflcko maflcko force-pushed the 2310-tidy-modernize-use-emplace- branch 3 times, most recently from fa3c80c to fa2876c Compare October 11, 2023 13:04
@maflcko
Copy link
Member Author

maflcko commented Oct 11, 2023

rebased for clang-17. Excluded nanobench for now due to it being C++11, but here it is compiled as C++17, also clang-tidy-17 introduced a new rule that requires C++20. 🥲

Should be trivial to re-ACK.

@maflcko maflcko marked this pull request as draft October 11, 2023 13:41
@maflcko
Copy link
Member Author

maflcko commented Oct 11, 2023

I guess the only way to fix the CI failure would be:

  • Go back to clang-tidy-16
  • Fix the bug in clang-tidy-17
  • Bump to C++20
  • Edit: NOLINTNEXTLINE
  • ?

@Sjors
Copy link
Member

Sjors commented Oct 12, 2023

Example of the CI error:

clang-tidy-17 -p=/ci_container_base/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu -quiet -load=/tidy-build/libbitcoin-tidy.so /ci_container_base/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/src/script/signingprovider.cpp
script/signingprovider.cpp:371:41: error: unnecessary temporary object created while calling emplace_back [modernize-use-emplace,-warnings-as-errors]
  371 |     if (track) node.leaves.emplace_back(LeafInfo{.script = std::vector<unsigned char>(script.begin(), script.end()), .leaf_version = leaf_version, .merkle_branch = {}});
      |                 

Fix the bug in clang-tidy-17

Is there an upstream issue?

@maflcko maflcko force-pushed the 2310-tidy-modernize-use-emplace- branch from fa2876c to fac590b Compare October 12, 2023 08:02
@maflcko maflcko marked this pull request as ready for review October 12, 2023 08:09
@maflcko
Copy link
Member Author

maflcko commented Oct 12, 2023

Just recalled that NOLINTNEXTLINE exists, so I used that.

@maflcko maflcko force-pushed the 2310-tidy-modernize-use-emplace- branch from fac590b to fa05a72 Compare October 12, 2023 09:27
@Sjors
Copy link
Member

Sjors commented Oct 12, 2023

re-utACK fa05a72

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

ACK fa05a72.

Comment on lines +11 to +12
"src/bench/nanobench.cpp",
"src/bench/nanobench.h",
Copy link
Member

Choose a reason for hiding this comment

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

nit: They might be ordered lexicographically?

Copy link
Contributor

@TheCharlatan TheCharlatan left a comment

Choose a reason for hiding this comment

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

ACK fa05a72

@fanquake fanquake merged commit 08ea835 into bitcoin:master Oct 16, 2023
@maflcko maflcko deleted the 2310-tidy-modernize-use-emplace- branch October 16, 2023 14:57
Frank-GER pushed a commit to syscoin/syscoin that referenced this pull request Oct 21, 2023
jonatack added a commit to jonatack/bitcoin that referenced this pull request Jan 8, 2024
@bitcoin bitcoin locked and limited conversation to collaborators Oct 15, 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.

7 participants