Hacker News new | past | comments | ask | show | jobs | submit login

Rust has unreachable_unchecked and intrinsics::assume. And a clever crate that provides an assume!() macro for stating assumptions.

I toyed with them while learning some unsafe Rust by calling unsafe Windows APIs. Sometimes there were obvious relationships between function parameters and return values that my code couldn't know because the Windows API was an opaque FFI to C. Like if I ask a Windows API to fill an 8-element array, my code can assume it returns an int <= 8 instead of considering INT_MAX as a possibility.

But anything more advanced like assuming that an array's values are sorted seemed questionable. At worst, it looked like LLVM preserved expensive assumes even if they weren't useful to the optimizer.

Since it was a toy project, I didn't care. It's neat to tell my code facts about the world and imagine that a perfect code optimizer can use all those facts. Even if there's a C API in the way.

https://doc.rust-lang.org/core/hint/fn.unreachable_unchecked...

https://doc.rust-lang.org/core/intrinsics/fn.assume.html

https://crates.io/crates/assume




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: