Skip to content

Conversation

@maflcko
Copy link
Member

@maflcko maflcko commented Jun 9, 2024

A copy of the partial signatures is not required before serializing them.

For reference, this was found by switching the codebase to std::span (not sure why it wasn't found with Span though):

./psbt.h:240:23: error: loop variable is copied but only used as const reference; consider making it a const reference [performance-for-range-copy,-warnings-as-errors]
  240 |             for (auto sig_pair : partial_sigs) {
      |                       ^
      |                  const  &

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 9, 2024

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 theStack, tdb3

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

@maflcko
Copy link
Member Author

maflcko commented Jun 9, 2024

Reference CI run: https://github.com/bitcoin/bitcoin/runs/25847037350

Copy link
Contributor

@tdb3 tdb3 left a comment

Choose a reason for hiding this comment

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

Approach ACK.
Less copying makes sense. Briefly searched for other instances in psbt.h where potentially unnecessary copying is done.

Would it also make sense to adjust these references to const (or is there a specific reason why const should not be used here)? Adjusted to const auto& entry and ran unit and functional tests (no issues observed).

bitcoin/src/psbt.h

Lines 355 to 359 in fab01b5

// Write unknown things
for (auto& entry : unknown) {
s << entry.first;
s << entry.second;
}

bitcoin/src/psbt.h

Lines 780 to 784 in fab01b5

// Write unknown things
for (auto& entry : unknown) {
s << entry.first;
s << entry.second;
}

bitcoin/src/psbt.h

Lines 1013 to 1017 in fab01b5

// Write the unknown things
for (auto& entry : unknown) {
s << entry.first;
s << entry.second;
}

@maflcko
Copy link
Member Author

maflcko commented Jun 9, 2024

Less copying makes sense. Briefly searched for other instances in psbt.h where potentially unnecessary copying is done.

The examples you provided are already const references. In C++, when calling a member method that is marked const, member access will be const. Moreover, auto& is a reference that hides the type, including the const-ness, so const auto& and auto& should be identical in this context (const references that do not copy), and no further change should be needed.

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

utACK fab01b5

@DrahtBot DrahtBot requested a review from tdb3 June 9, 2024 17:51
@tdb3
Copy link
Contributor

tdb3 commented Jun 9, 2024

no further change should be needed.

Thanks.
ACK fab01b5

@fanquake fanquake merged commit ea88a75 into bitcoin:master Jun 10, 2024
@maflcko maflcko deleted the 2406-performance-for-range-copy branch June 10, 2024 08:35
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Jun 13, 2024
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Feb 20, 2025
fab01b5 refactor: performance-for-range-copy in psbt.h (MarcoFalke)

Pull request description:

  A copy of the partial signatures is not required before serializing them.

  For reference, this was found by switching the codebase to `std::span` (not sure why it wasn't found with `Span` though):

  ```
  ./psbt.h:240:23: error: loop variable is copied but only used as const reference; consider making it a const reference [performance-for-range-copy,-warnings-as-errors]
    240 |             for (auto sig_pair : partial_sigs) {
        |                       ^
        |                  const  &

ACKs for top commit:
  tdb3:
    ACK fab01b5
  theStack:
    utACK fab01b5

Tree-SHA512: b55513d8191118499716684190ee568d171b50ae9171d246ca6e047f0cfd3ec14c9453d721e88af55e47bb41fa66cbafdbfb47bc5f9b8d82789e0a9b634b371b
PastaPastaPasta added a commit to dashpay/dash that referenced this pull request Feb 21, 2025
c34e7ab Merge bitcoin#30534: guix: move bison from global scope, to Linux (merge-script)
da11d29 Merge bitcoin#30282: Revert "contrib: macdeploy: monkey-patch gen-sdk to be deterministic" (merge-script)
20bcee8 Merge bitcoin#30281: Update leveldb subtree to latest upstream (merge-script)
a74b1e2 Merge bitcoin#30253: refactor: performance-for-range-copy in psbt.h (merge-script)
2ea479f Merge bitcoin#29650: depends: drop 1 Qt determinism patch (fanquake)
6506f52 Merge bitcoin#28833: wallet: refactor: remove unused `SignatureData` instances in spkm's `FillPSBT` methods (Ava Chow)
6235590 Merge bitcoin#29213: doc, test: test and explain service flag handling (Ava Chow)
34bbe69 Merge bitcoin#28965: guix: remove input labels (fanquake)
490b390 Merge bitcoin#28859: guix: update signapple (drop macho & altgraph) (fanquake)
a12ad80 Merge bitcoin#28325: test: wallet_backup.py, fix intermittent failure in "restore using dumped wallet" (fanquake)
15f8b74 Merge bitcoin#27779: guix: remove cURL from build env (fanquake)
1289d32 Merge bitcoin#27801: wallet: Add tracing for sqlite statements (fanquake)
ba3aff7 Merge bitcoin-core/gui#729: test: Add missed header (Hennadii Stepanov)
536a265 Merge bitcoin#27209: ci: Remove unused EXPECTED_TESTS_DURATION_IN_SECONDS env var (glozow)
f2caccd Merge bitcoin#27174: ci: bump lint task to bookworm for git v2.38 (fanquake)
0be2932 Merge bitcoin#27009: validation: Skip VerifyDB checks of level >=3 if dbcache is too small (fanquake)

Pull request description:

  ## Issue being fixed or feature implemented
  Batch of trivial back ports

  ## What was done?

  ## How Has This Been Tested?
  Built locally; haven't reviewed commits yet.

  ## Breaking Changes

  ## Checklist:
    _Go over all the following points, and put an `x` in all the boxes that apply._
  - [ ] I have performed a self-review of my own code
  - [ ] I have commented my code, particularly in hard-to-understand areas
  - [ ] I have added or updated relevant unit/integration/functional/e2e tests
  - [ ] I have made corresponding changes to the documentation
  - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_

ACKs for top commit:
  UdjinM6:
    utACK c34e7ab
  kwvg:
    utACK c34e7ab

Tree-SHA512: e20270e48273ad5fd5926e75b025fe69a70e698a82f0a11a1fa646c44a7affbd7a5f33eebcd7faa606e7adc2a888df141c660339d7240e6ed64f8525558215de
@bitcoin bitcoin locked and limited conversation to collaborators Jun 10, 2025
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.

5 participants