[HN Gopher] Assertions Are Pessimistic, Assumptions Are Optimist...
___________________________________________________________________
Assertions Are Pessimistic, Assumptions Are Optimistic (2014)
Author : pantalaimon
Score : 12 points
Date : 2023-03-06 16:24 UTC (1 days ago)
(HTM) web link (blog.regehr.org)
(TXT) w3m dump (blog.regehr.org)
| byteware wrote:
| I don't expect the compiler to produce "optimal" code in non-
| trivial cases, but being able to prove to the compiler that some
| high level code corresponds to some optimized assembly is high on
| my wishlist (obligatory list: coq, lean, framac, k-framework, f*
| and many others)
| raldi wrote:
| Brilliant. I'm actually surprised to see, nine years later, that
| this isn't on track to make it into the next version of the C
| standard.
| leni536 wrote:
| It's part of C++23, in form of an attribute
| https://en.cppreference.com/w/cpp/language/attributes/assume.
| Slix wrote:
| 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
___________________________________________________________________
(page generated 2023-03-07 23:01 UTC)