chromium / subspace

A concept-centered standard library for C++20, enabling safer and more reliable products and a more modern feel for C++ code.; Also home of Subdoc the code-documentation generator.
https://suslib.cc
Apache License 2.0
89 stars 15 forks source link

Tell the optimizer the value in unwrap_unchecked #396

Closed danakj closed 1 year ago

danakj commented 1 year ago

Result::unwrapunchecked writes to state but also assumes it is always an Ok value. The optimizer fails to remove branches that construct the Result currently, but succeeds if we tell it that the state_ is an Ok value before clobbering it.

Repeat the same thing in Result::unwrap_err_unchecked and in Option::unwrap_unchecked for good measure.

The difference can be seen in here: https://godbolt.org/z/Gax47shsb

Without it, the signed code generation is terrible. But with the optimizer hint, it's as good as working with ints directly.