-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Add panic_unreachable_unchecked feature flag to the standard library
#139196
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
Conversation
This is similar to `panic_immediate_abort` except that all panics are considered immediate UB and can therefore be optimized away as unreachable by the compiler. This has been tested on [regalloc3](https://github.com/Amanieu/regalloc3) where it resulted in a 10% speedup compared to using a normal standard library, mainly due to the elimination of bounds checks. While it may seem that this feature merely to satisfy those with a reckless thirst for performance at any cost, it is also useful for saner heads as a profiling tool to investigate the impact of unnecessary safety check and find places where unsafe code could be used to avoid them.
| #[rustc_const_stable_indirect] // must follow stable const rules since it is exposed to stable | ||
| pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! { | ||
| if cfg!(feature = "panic_unreachable_unchecked") { | ||
| // SAFETY: it's not... |
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.
This safety comment is inadequate, the condition of soundness relies on the programmer asserting panics will never happen, so a more accurate comment is:
| // SAFETY: it's not... | |
| // SAFETY: The user of this flag asserts that a panic will never happen in their codebase |
if you want to do april fools, go all in :^)
|
|
| # Make panics and failed asserts immediately abort without formatting any message | ||
| panic_immediate_abort = ["core/panic_immediate_abort"] | ||
| # Make the optimizer assume panics are unreachable. | ||
| panic_unreachable_unchecked = [ |
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 propose to rename this feature to unsafe_panic_unreachable_unchecked, so that its name includes word unsafe, because this is actually an unsafe API (like #[no_mangle] attribute), and AFAIU all unsafe APIs tend to include unsafe in their usage (as part of their name, as a surrounding unsafe block, etc).
Alternatively, if this PR is a joke, I'd consider crabs_never_panic as an alternative name.
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.
lgtm
|
It turns out that @saethlin has a much better implementation of this that doesn't even require |
This is similar to
panic_immediate_abortexcept that all panics are considered immediate UB and can therefore be optimized away as unreachable by the compiler.This has been tested on regalloc3 where it resulted in a 10% speedup compared to using a normal standard library, mainly due to the elimination of bounds checks.
While it may seem that this feature merely to satisfy those with a reckless thirst for performance at any cost, it is also useful for saner heads as a profiling tool to investigate the impact of unnecessary safety check and find places where unsafe code could be used to avoid them.