[libcxx-commits] [libcxx] [libc++] Cooperation between `std::optional` and other standard types (PR #93672)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Jun 3 11:02:44 PDT 2024
philnik777 wrote:
I think this is a really cool optimization, and we should definitely look into adding it in the unstable ABI. However, I think there lurks something more general in this.
I think with a very similar approach we could improve `variant` and `expected` as well. e.g. `std::variant<int*, int>` could be represented in just 8 bytes instead of the 16 it currently takes by introducing the concept of a payload. Pointers with an alignment of at least two will never have the lowest bit set, allowing us to put a flag in there. If it's 1, there isn't a pointer stored, otherwise there is. If there is no pointer, we can have a payload size of 7 bytes, allowing us to store whether the `variant` is valueless by exception, or the `int`. (Here is a small example of the data layout: https://godbolt.org/z/r59PnnP8M) I realize that this is a much more complex problem to solve, but I think this could be an incredible optimization.
https://github.com/llvm/llvm-project/pull/93672
More information about the libcxx-commits
mailing list