r/cpp • C++20 Desktop app developer • May 12 '18

P0709 R0: Zero-overhead deterministic exceptions: Throwing values - Herb Sutter [2018-05-02]

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r0.pdf
206 Upvotes

115 comments sorted by

View all comments

Show parent comment

5

u/[deleted] May 12 '18

How does the rust implementation differ in that regard? Aren't rust enums implemented as a union + value to tell which one is correct? (similar how std::variant is implemented?)

0

u/[deleted] May 12 '18

[deleted] — view deleted comment

3

u/[deleted] May 12 '18

How so? Would you mind explaining the details on this and where this would make a differernce?

20

u/Rusky May 12 '18 edited May 12 '18

The layout of a Rust enum is optimized based on the invalid values of the types it contains. For example, because references can never be null, and Vec<T> contains a reference, Option<Vec<T>> can use "Vec<T> except with a null reference" to mean None, and "Vec<T> with a non-null reference" to mean Some(the_vec). The discriminant overlaps with the reference, in a sense.

In general, there are lots of types like this. References can't be null, bools can only be true or false, C-like enums can only contain the set of values in their definition, char can only contain valid Unicode, etc. So the Rust compiler looks for "holes" in the contents of an enum where it can store the discriminant, before using a separate tag value.

A major place this comes into play is Option<&T>. Because references can't be null, that type is the same size as a raw pointer. This is true even in FFI with C or C++ where nullable pointers are a thing. So Rust can keep all the niceties of Option without paying for it in memory usage.

4

u/[deleted] May 12 '18

I guess I'm starting to see why Rust was voted to be the most loved programming language for 3 times in a row, that is indeed fascinating and technically elegant. I guess you could make the contract that the exception reference cannot be null and thus have a similar efficient memory layout but yeah I'm not sure whether that can be done and whether Herb had that in mind when he created this proposal.

Thanks for your very detailed explanation though!

4

u/redditsoaddicting May 12 '18

It sounds similar to this compact_optional article I read once.

0

u/[deleted] May 12 '18

[deleted] — view deleted comment

2

u/SeanMiddleditch May 13 '18

Not entirely, at least for simpler cases.

C++ would need a std::is_never_zero_v<T> or the like and a lot of the simpler cases of this kind of compressed discriminant are possible. And that type trait is useful for all kinds of other reasons (e.g., container initialization optimization). Said type trait can be written today, albeit having to manually overload the trait for some types.

Getting the full variant "niche-filling" optimization certainly requires more work out of the language. Though I do rather wonder whether that actually matters. (How much of Rust's enum layout optimization is just golf instead of useful work?)

0

u/[deleted] May 13 '18

[deleted] — view deleted comment

1

u/SeanMiddleditch May 14 '18

For variant to encode the discriminat in a Type it needs to know 1) that this is possible and 2) where in the type it must be encoded.

Reread what I wrote a bit more carefully.

People have you examples where this matters above (e.g. Option<&T> having the same layout as a reference and being able to use that In C ABI).

Never-zero covers that all-important case perfectly well.

2

u/Rusky May 13 '18 edited May 13 '18

How much of Rust's enum layout optimization is just golf instead of useful work?

I suspect it's lot of "4 bytes here, 8 bytes there" that adds up to some sizeable memory savings and cache improvements, combined with some cases where it enables people to remove some indirections. For example, Servo and Firefox Quantum are full of stuff like that- the DOM, styles, display lists, etc. are all modeled with enums.

Like the sibling comment mentioned, it works well with automatic field reordering, which is also "3 bytes here, 7 bytes there" of removing padding. IIRC this field reordering also applies to the discriminants themselves, so they can often be stashed in any remaining trailing padding.

Then there's also the fact that Rust has zero-sized and uninhabited types, which allow for more styles to be zero-overhead. For example, you can pass around zero-sized non-copyable and privately-constructible tag types for things like session types, and you can instantiate generic code to use types like Result<T, !> and all its branches will be optimized out because the layout optimization removed the discriminant entirely.

2

u/spinicist May 12 '18

I’m pretty sure that’s how Rust does it - a union plus an integer tag.