Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Branch prediction hints (i.e. Likely/Unlikely) #49

Open
aclysma opened this issue Sep 2, 2019 · 1 comment
Open

Branch prediction hints (i.e. Likely/Unlikely) #49

aclysma opened this issue Sep 2, 2019 · 1 comment
Labels
Needs Champion We need someone who can help drive this forward.

Comments

@aclysma
Copy link
Contributor

aclysma commented Sep 2, 2019

In some performance sensitive code, hinting to the compiler that a certain path is likely or unlikely to be taken can be a helpful optimization:

  • Sometimes the unlikely path is extremely unlikely/unintended (for example, a fixed-size object pool being full, trying to normalize a 0-length vector). Sometimes, the path is completely unreachable.
  • Sometimes you may want to optimize for the unlikely path - for example a spin loop waiting for an event.

There is a discussion here: rust-lang/rust#26179

The RFC was accepted but the intrinsics are not stabilized, I think mainly because:

  • The LLVM team recommends (and clang uses) branch weights instead. (These intrinsics disable certain optimizations in LLVM, which might defeat the purpose of using them.)
  • This doesn't support hinting if let or match statements.

Workarounds:

  • Compiler intrinsics std::intrinsics::likely and std::intrinsics::unlikely are available in nightly, even though they could be harmful the way they work now
  • #[cold] will hint to llvm that a certain path is less likely to be called. This also prevents inlining which can reduce icache pressure
  • unreachable!() and unreachable_unchecked!() are worth considering for the unreachable case. It's not clear to me if unreachable!() does any optimization, but unreachable_unchecked does. (not sure, but probably via llvm's assume)
  • Profile-guided optimization
@mtak-
Copy link

mtak- commented Oct 2, 2019

My crate nudge is possibly a good workaround for now. It does not require nightly.

@Lokathor Lokathor added the Needs Champion We need someone who can help drive this forward. label Dec 11, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Needs Champion We need someone who can help drive this forward.
Projects
None yet
Development

No branches or pull requests

3 participants