Description
Proposal
Problem statement
Prompted by this comment:
Similarly, you can already write:
unsafe { a.checked_mul(b).unwrap_unchecked() }so it's strange to me that additional functions like
unchecked_mul
are being added tou32
for this specific operation.
I began searching for concrete examples of situations where using unwrap_unchecked
leads to bad codegen. I didn't have to search long.
The problem with unwrap_unchecked
is that it's the programming equivalent of littering - with every unwrap we're sprinkling in a condition for LLVM to keep around, even though frequently what we really mean is "I know this is Some
/Ok
/Err
, please give me the contents without checking the variant." In a perfect world, these conditions would just get ignored when irrelevant and cleanly DCE'd, but...
Motivating examples or use cases
type NZ = core::num::NonZeroU32;
type U = u32;
#[no_mangle]
unsafe fn sum_unwrap(a: &[Option<NZ>; 16]) -> U {
a.iter().map(|&v| v.unwrap_unchecked().get()).sum()
}
This innocuous snippet leads to staggeringly bad assembly - 80+ lines of LLVM IR and 30+ ARM instructions... to add together 16 numbers. For reference, a non-vectorized implementation would be 15 adds, 8 pair-loads, and a return. Autovectorized it's just 8 instructions.
Solution sketch
Maybe we should just stop littering.
We can restore good codegen if we express the unwrap in a different way, without invoking unreachable_unchecked
; for example, like this:
trait OptionExt {
type T;
unsafe fn get_unchecked(self) -> Self::T;
}
impl<T> OptionExt for Option<T> {
type T = T;
#[allow(invalid_value)]
unsafe fn get_unchecked(self) -> T {
match self {
Some(v) => v,
None => core::mem::MaybeUninit::uninit().assume_init(),
}
}
}
Add the above method (and analogous methods on Result
) to core
.
Alternatives
Change implementation of unwrap_unchecked
Idly browsing through uses of unwrap_unchecked
, I notice that a significant portion (perhaps even majority!) of them probably don't care to keep their conditions around. Worth investigating with benchmarks.
Not convinced it's relevant, but clang does not generate an assume
for an std::optional
dereference. godbolt
Additionally, the "unchecked" wording sort of implies a lack of checks, which is... well, ostensibly true...
Change current implementation and add new methods
Assuming get_unchecked
is on average better than unwrap_unchecked
, we might want to replace the functionality for current code and also keep providing the previous functionality for the cases where it is useful. Call it unwrap_assume
or something.
Do nothing
This can be implemented in user code just fine, as an extension method. The problem is discoverability - if you're reaching for unwrap_unchecked
, you probably care about performance, and with unwrap_unchecked
being the only unchecked
method on Option
/Result
you might not think to search further, or consider what the method does under the hood (and whether that's something you want to happen).
Improve LLVM
Presumably a long-term effort. I don't have the necessary knowledge to properly consider this alternative.