tcbrindle / flux

A C++20 library for sequence-orientated programming
https://tristanbrindle.com/flux/
Boost Software License 1.0
441 stars 28 forks source link

Internal `std::variant` access optimizations #161

Open igrekster opened 5 months ago

igrekster commented 5 months ago

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: https://github.com/igrekster/flux/commit/70ef410b27f3715d4c3d6a4214fca68b35e782c9

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

tcbrindle commented 5 months ago

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!

igrekster commented 5 months ago

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.

tcbrindle commented 5 months ago

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