tcbrindle/flux

Internal `std::variant` access optimizations

igrekster opened this issue · 3 comments

Been playing around with flux and noticed that the generated code had checks for safe std::variant access, even though the logic ensures that it was safe. I've identified a few places in the code, but there are more patterns like this: igrekster@70ef410

Here is before/after on the Compiler Explorer: https://godbolt.org/z/azfqMbT5j

And a Rust equivalent to compare: https://rust.compiler-explorer.com/z/K6E5xYc3W

Wow, that's a huge improvement! And it's always nice to know when we generate better code than Rust iterators :)

It's a similar story with GCC: with your changes the example optimises down to an astonishingly small amount of assembly https://godbolt.org/z/c1dexE4Y9.

I'd be very happy to take a PR that implements this optimisation (here and in other places if you find more). I'd prefer not to require C++23 yet on GCC and Clang, but adding our own version of std::unreachable shouldn't be too difficult.

Thanks @igrekster!

Unfortunately, I don't have much capacity ATM, but I might look into it some time in the next few weeks. Just wanted to bring attention to this optimization.

No problem, I can take a stab at it at some point