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

script: actually trigger the optimization in BuildScript #25709

Merged
merged 1 commit into from
Jul 30, 2022

Conversation

darosior
Copy link
Member

The counter is an optimization over calling ret.empty(). It was
suggested that the compiler would realize cnt is only 0 on the first
iteration, and not actually emit the check and conditional.

This optimization was actually not triggered at all, since we
incremented cnt at the beginning of the first iteration. Fix it by
incrementing at the end instead.

This was reported by Github user "Janus".

Fixes #25682. Note this does not change semantics. It only allows the optimization of moving instead of copying on first CScript element to actually be reachable.

The counter is an optimization over calling `ret.empty()`. It was
suggested that the compiler would realize `cnt` is only `0` on the first
iteration, and not actually emit the check and conditional.

This optimization was actually not triggered at all, since we
incremented `cnt` at the beginning of the first iteration. Fix it by
incrementing at the end instead.

This was reported by Github user "Janus".
@@ -600,6 +599,7 @@ CScript BuildScript(Ts&&... inputs)
// Otherwise invoke CScript::operator<<.
ret << input;
}
cnt++;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
cnt++;
++cnt;

Copy link
Member

Choose a reason for hiding this comment

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

@darosior did you want to address this comment?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was not planning to unless anyone felt strongly otherwise, as it's really a nit..

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I haven't yet seen an iterator type where this isn't optimized to the same assembly. Though, if we want this, it might be better enforced with a tidy check than with review comments that invalidate review

Copy link
Member

Choose a reason for hiding this comment

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

If we don't want this, it should be removed from the developer notes.

Copy link
Member

Choose a reason for hiding this comment

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

Agree. See also #24846 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

Choosing ++v; over v++; is generally good advice if you don't want to go into details, but in practically it really matters for performance in case v is a non-trivial, big, type. In this case where it's just an integer it won't make to any difference in the resulting binary code with any compiler created the past 20 years.

@sipa
Copy link
Member

sipa commented Jul 27, 2022

utACK 00897d0

@maflcko
Copy link
Member

maflcko commented Jul 27, 2022

review ACK 00897d0

@maflcko maflcko added this to the 24.0 milestone Jul 27, 2022
@maflcko maflcko merged commit 5215c80 into bitcoin:master Jul 30, 2022
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 1, 2022
…ldScript

00897d0 script: actually trigger the optimization in BuildScript (Antoine Poinsot)

Pull request description:

  The counter is an optimization over calling `ret.empty()`. It was
  suggested that the compiler would realize `cnt` is only `0` on the first
  iteration, and not actually emit the check and conditional.

  This optimization was actually not triggered at all, since we
  incremented `cnt` at the beginning of the first iteration. Fix it by
  incrementing at the end instead.

  This was reported by Github user "Janus".

  Fixes bitcoin#25682. Note this does *not* change semantics. It only allows the optimization of moving instead of copying on first `CScript` element to actually be reachable.

ACKs for top commit:
  sipa:
    utACK 00897d0
  MarcoFalke:
    review ACK 00897d0

Tree-SHA512: b575bd444b0cd2fe754ec5f3e2f3f53d2696d5dcebedcace1e38be372c82365e75938dfe185429ed5a83efe1a395e204bfb33efe56c10defc5811eaee50580e3
@bitcoin bitcoin locked and limited conversation to collaborators Jul 30, 2023
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.

Redundant code or latent bug
7 participants