Skip to content

bench: Construct CTxOut and COutPoint in a single expression - #36019

Open
alexanderwiederin wants to merge 1 commit into
bitcoin:masterfrom
alexanderwiederin:bench-declarative
Open

bench: Construct CTxOut and COutPoint in a single expression#36019
alexanderwiederin wants to merge 1 commit into
bitcoin:masterfrom
alexanderwiederin:bench-declarative

Conversation

@alexanderwiederin

@alexanderwiederin alexanderwiederin commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Replaces field-by-field mutation of CTxOut and COutPoint in two bench files with brace initialisation, which requires the size_t conversions to be made explicit.

Noticed while looking at #35994, where switching the proposed fix-it to {} surfaces implicit narrowing conversions like these.

The constructed values are unchanged.

Note: Only the sites where a conversion is involved are included in this PR; the remaining field-by-field construction in bench/ would be covered by #35994's follow-ups.

@DrahtBot DrahtBot added the Tests label Aug 19, 2026
@DrahtBot

DrahtBot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

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

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/36019.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK josibake

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Comment thread src/bench/blockencodings.cpp Outdated
Replace separate member assignments with brace initialization. The
narrowing conversions from size_t are now explicit rather than implicit.

@josibake josibake left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK 5d53932

LGTM! Left a non-blocking readability suggestion.

tx1.vout[i].scriptPubKey = CScript();
// Each output progressively larger
tx1.vout[i].nValue = i * CENT;
tx1.vout[i] = CTxOut{static_cast<CAmount>(i) * CENT, CScript()};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think slightly better is:

tx1.vout.reserve(number_outputs);

for (size_t i = 0; i < number_outputs; ++i) {
    tx1.vout.push_back(
        CTxOut{static_cast<CAmount>(i) * CENT, CScript{}}
    );
}

.. as I find it improves the readability and also keeps the type being constructed clearly visible. If performance were a concern you could do:

tx1.vout.reserve(number_outputs);

for (size_t i = 0; i < number_outputs; ++i) {
    tx1.vout.emplace_back(
        static_cast<CAmount>(i) * CENT, 
        CScript{}
    );
}

but considering this really hurts the readability I wouldn't suggest it here.

for (size_t i = 0; i < tx2.vin.size(); i++) {
tx2.vin[i].prevout.hash = parent_txid;
tx2.vin[i].prevout.n = i;
tx2.vin[i].prevout = COutPoint{parent_txid, static_cast<uint32_t>(i)};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same suggestion as above with:

tx2.reserve(tx1.vout.size());
for (size_t i = 0; i < tx2.vin.size(); i++) {
    tx2.push_back(
        COutPoint{parent_txid, static_cast<uint32_t>(i)}
    );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants