Skip to content

Clarify how to use black_box() - #133942

Merged
bors merged 1 commit into
rust-lang:masterfrom
BD103:black-box-docs
Dec 14, 2024
Merged

Clarify how to use black_box()#133942
bors merged 1 commit into
rust-lang:masterfrom
BD103:black-box-docs

Conversation

@BD103

@BD103 BD103 commented Dec 5, 2024

Copy link
Copy Markdown
Contributor

Closes #133923.

r? libs
^ (I think that's the right group, this is my first time!)

This PR adds further clarification on the black_box() documentation. Specifically, it teaches how to use it, instead of just when to use it.

I tried my best to make it clear and accurate, but a lot of my information is sourced from rust-lang/rust-clippy#12707 and manually inspecting assembly. Please tell me if I got anything wrong!

@rustbot

rustbot commented Dec 5, 2024

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @tgross35 (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 5, 2024
@rust-log-analyzer

This comment has been minimized.

Comment thread library/core/src/hint.rs Outdated
@tgross35

tgross35 commented Dec 6, 2024

Copy link
Copy Markdown
Contributor

Could this go into a new section like # Usage Guidelines? The docs for this method are getting pretty long.

I think it would also be better to move this new section to the end, the Note however, that black_box is only (and can only be) provided on a "best-effort" basis... section is important to have early.

@saethlin I think you've reviewed these docs a couple of times

@saethlin

saethlin commented Dec 6, 2024

Copy link
Copy Markdown
Member

I'm already subscribed to this issue :)
Do you want me to take the review?

@tgross35

tgross35 commented Dec 6, 2024

Copy link
Copy Markdown
Contributor

No preference, I'm happy to but all yours if you want (same for Jubilee)

@BD103

BD103 commented Dec 6, 2024

Copy link
Copy Markdown
Contributor Author

@rustbot author

I'll address the changes later today or sunday, thanks for looking this over!

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 6, 2024
@BD103

BD103 commented Dec 9, 2024

Copy link
Copy Markdown
Contributor Author

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 9, 2024
@BD103

BD103 commented Dec 9, 2024

Copy link
Copy Markdown
Contributor Author

Ok, I tried my best to address the feedback! Thank you for reviewing :)

@ryanavella

Copy link
Copy Markdown

There is one other trick I sometimes use to prevent the inlining of a function, not sure if it is worth illustrating in the docs but here is what it looks like:

#[inline(always)]
fn noop() {}

pub fn bad() {
    black_box(noop());
}

pub fn good() {
    black_box(noop as fn())();
}

@saethlin

saethlin commented Dec 9, 2024

Copy link
Copy Markdown
Member

Oh sweet that's a great example of when #[inline(always)] doesn't actually cause a function call to be inlined!

Comment thread library/core/src/hint.rs Outdated
Comment thread library/core/src/hint.rs Outdated
Comment thread library/core/src/hint.rs Outdated
Comment thread library/core/src/hint.rs Outdated
Comment thread library/core/src/hint.rs Outdated
@tgross35

Copy link
Copy Markdown
Contributor

r? @saethlin :)

@rustbot rustbot assigned saethlin and unassigned tgross35 Dec 10, 2024
@saethlin

Copy link
Copy Markdown
Member

Thanks! Squash this down to 1 commit (or I can do that for you), then I'll send this off to the queue.

@bors
bors merged commit 8abb823 into rust-lang:master Dec 14, 2024
@rustbot rustbot added this to the 1.85.0 milestone Dec 14, 2024
@BD103
BD103 deleted the black-box-docs branch December 14, 2024 17:16
@workingjubilee

Copy link
Copy Markdown
Member

Cool! Thanks for the fix, and thanks for taking the rest of the review, @saethlin!

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

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clarify black_box() docs when passed a unit-returning function

8 participants