-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Improve the documentation of black_box
#106144
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @joshtriplett (or someone else) soon. Please see the contribution instructions for more information. |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
This comment has been minimized.
This comment has been minimized.
@rustbot label +A-docs |
library/core/src/hint.rs
Outdated
/// This makes our benchmark much more realistic to how the function would be used in situ, where | ||
/// arguments are usually not known at compile time and the result is used in some way. | ||
/// | ||
/// It is important to note that `black_box` cannot stop all optimizations across its call in all |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might be understating a little - I know there was some discussion prior to stabilization/on the release blog post about emphasizing more of the lack of guarantees (cc @nagisa @saethlin).
I think at minimum, we shouldn't try to phrase it as "not stopping all optimizations", which makes it sound like some are guaranteed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would something like this be better?
A word on guarantees
It is important to reiterate the above:
black_box
does not guarantee with any certainty what optimizations may or may not happen. Variations such as different compiler backends, different targets,
or even just corner case code may mean it has no effects at all. The Rust team attempts to make sure this doesn't happen, but it cannot be guaratnteed: for that reason,black_box
should never be relied on for controlling behavior outside of test cases or benchmarks.For the most performance-critical cases, there is no substitute for validating the generated assembly and using performance tools (e.g.
perf
orcallgrind
) to analyze your code. That being said,black_box
is suitable for creating fairly accurate benchmarks in most cases, for most users.
I don't think I know enough about the specific context to know how accurate that is, so open to suggestions (didn't see too much on #64102)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer changing the wording of the text you're proposing adding, as opposed to adding another section. Documentation here should emphasize everywhere that black_box
having any effect at all on a program is entirely best-effort, it may do nothing at all, and therefore this is only for benchmarks. The are already people who think it is for writing cryptography code, and that is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In practice this function is pretty reliable, but it is absolutely NOT completely reliable, and even if it were, documenting what it actually accomplishes if it were reliable sounds like a nightmare.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've rearranged this somewhat and now that bit is at the beginning: https://github.com/rust-lang/rust/blob/79cf860f397aff83d283ac8de0ac77057ec59147/library/core/src/hint.rs#L225-L233
6f5c50f
to
79cf860
Compare
I really like the changes you've made. The documentation as-written now answers all my concerns, I've just left some grammar/phrasing tweaks. |
Thank you, I've applied your changes & rebased |
I don't think there are further changes (let me know if so) so r? Mark-Simulacrum (Not sure if you can merge saethlin) |
@bors r+ rollup |
Improve the documentation of `black_box` There don't seem to be many great resources on how `black_box` should be used, so I added some information here
Thanks! |
Improve the documentation of `black_box` There don't seem to be many great resources on how `black_box` should be used, so I added some information here
…mpiler-errors Rollup of 8 pull requests Successful merges: - rust-lang#103418 (Add `SEMICOLON_IN_EXPRESSIONS_FROM_MACROS` to future-incompat report) - rust-lang#106113 (llvm-wrapper: adapt for LLVM API change) - rust-lang#106144 (Improve the documentation of `black_box`) - rust-lang#106578 (Label closure captures/generator locals that make opaque types recursive) - rust-lang#106749 (Update cc to 1.0.77) - rust-lang#106935 (Fix `SingleUseLifetime` ICE) - rust-lang#107015 (Re-enable building rust-analyzer on riscv64) - rust-lang#107029 (Add new bootstrap members to triagebot.toml) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
There don't seem to be many great resources on how
black_box
should be used, so I added some information here