Description
On IRC, we recently discussed the speed of addition. I think Rust currently has no way to say “I know this cannot overflow, so just optimize and UB if it actually can”.
Hence the idea of adding unchecked_add
, etc., that would be unsafe
functions. (addition of an Unchecked<u*>
type is left to later assessment)
Specific list of functions that'd be available as unchecked_*
is the same set of functions that are currently available as checked_*
, overflowing_*
and wrapping_*
forms:
unchecked_add
unchecked_sub
unchecked_mul
unchecked_div
unchecked_div_euc
unchecked_rem
unchecked_mod_euc
unchecked_neg
unchecked_shl
unchecked_shr
unchecked_pow
Is there any major roadblock/risks that you think I should be aware of before starting to write this RFC? Do you like this idea or not?
For the potential for improvement, even though it's hard to pinpoint the exact reason why, using signed arithmetic instead of unsigned arithmetic in C here made the code run 10% faster -- this is likely an equivalent gain we could have using unchecked
variants instead of overflowing
(or equivalent) ones. :) (not even counting debug builds, where most of the huge gain could already be had by using eg. overflowing_*
, even though it wouldn't really be semantically correct)
Note: this is the same idea as rust-lang/rust#52205 , except it'd be available to user code