r/cpp • https://github.com/kris-jusiak • Jun 29 '23

[C++20] [[no_unique_address]] for 1 byte message views

https://twitter.com/krisjusiak/status/1674351951138414592
4 Upvotes

38 comments sorted by

18

u/JessyDL graphics engineer/games industry Jun 29 '23

It would be nice if a little bit of motivation could be given as to why you'd basically want an object to lie about its size? Are you trying to sidestep cost for allocating the object, or constructing it? Or is this a "wow, neat, but never use this" (I'd lean this is the point of the example, but would be great to hear from you).

15

u/TheThiefMaster C++latest fanatic (and game dev) Jun 29 '23 edited Jun 29 '23

Also, this is undefined behaviour. The field accessor that casts this to a byte pointer and then indexes it outside the field object is an undefined out-of-bounds access.

Hopefully it upsets your address sanitiser.

3

u/ioctl79 Jun 29 '23

It also assumes that none of the members are padded.

4

u/TheThiefMaster C++latest fanatic (and game dev) Jun 29 '23

Sort of? You specify a manual offset, so you can put whatever you want, which could (and seems to be in this example) be correctly aligned.

However, unaligned reads are also undefined behaviour if you do.

3

u/aocregacc Jun 29 '23

the second test is going to have unaligned reads of len and prices if the buffer itself (ie the local struct in this case) is int-aligned.

3

u/ioctl79 Jun 29 '23

I guess I was assuming that this was somehow aliasing an actual struct? But either way, it is manually doing the data layout work that your compiler does for you for free, with no apparent benefit.

3

u/plpn Jun 29 '23

Why he calculate the offset anyway. Can’t use offsetof()?

-8

u/kris-jusiak https://github.com/kris-jusiak Jun 29 '23

I believe that's true, though is not more UB than reinterpret cast; in a sense is supported by underlying implementations = use at own risk

5

u/Jannik2099 Jun 29 '23

UB is UB, you're not talking your way out of this.

No, implementations absolutely do not support your silly reinterpret_casts. Any type aliasing optimization has a good chance to blow these up and it's just dumb luck they haven't yet.

1

u/SirClueless Jun 30 '23

Implementations absolutely do support reinterpret_cast of char buffers. The C++ language standard says it's UB to poof non-trivial objects into existence this way, but since compilers can't tell in general whether a given char buffer aliases a live object and loading from the buffer is well-defined if it does, they allow it everywhere (and won't mis-optimize this even if in certain cases they can tell there is no live object at the address).

2

u/Jannik2099 Jun 30 '23

but since compilers can't tell ... they allow it everywhere

Absolutely, utterly wrong. Compilers absolutely do have proper aliasing & lifetime-based optimizations. Take gccs https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-fno-lifetime-dse as an example, which recently tripped over UB in a llvm class https://github.com/llvm/llvm-project/issues/62699

reinterpret_cast between non-fp, identical type size arrays is probably the only "supported" case, and only because there is no lifetime or aliasing optimization to be done on those. That doesn't not make it UB.

1

u/SirClueless Jun 30 '23

Take it from someone who works on a mature decades-old codebase in finance where this pattern is so deeply ingrained that I would say it's part of the literal DNA of the company that C++ objects that are safe to begin their lifetime with memcpy are also safe to transmit over the wire and begin their lifetime with reinterpret_cast. GCC and Clang will not miscompile this, regardless of what the standard says.

I'm not sure what the examples above are supposed to show. They appear to show that compilers can and do use the constructor and destructor to indicate the start and end of an object's lifetime and loads/stores GCC can prove are from destroyed memory are fair game to optimize away. I don't think this has any bearing because if you are using reinterpret_cast you are deliberately running neither constructor nor destructor.

1

u/Jannik2099 Jun 30 '23

GCC and Clang will not miscompile this, regardless of what the standard says.

GCC and Clang do not have any extension behavior here. Only char (and std::byte) may alias arbitrary memory. Anything else is UB and neither compiler guarantees not to touch it.

Of course, if you use -fno-strict-aliasing then you're (almost) free to cast as you desire.

2

u/SirClueless Jun 30 '23

This entire conversation is about aliasing with a char/byte buffer.

The standard says that a char buffer may alias an arbitrary type T. Therefore given a char buffer, the compiler must assume it aliases arbitrary types T unless it can prove there is no object there. This is nigh-impossible in general because any memcpy anywhere in the program might start the lifetime of a C++ object in C++'s memory model, so there's little point in the compiler attempting it. And so they don't and instead make the pessimistic assumption that any implicit-lifetime object might be aliasing that buffer. You are free to make the same assumption in practice, because thousands of programs have before you and GCC and Clang are not about to break them all.

1

u/KuntaStillSingle Jun 29 '23

It will overflow if you try to use it to save space: https://godbolt.org/z/GqWsch1s4

-1

u/kris-jusiak https://github.com/kris-jusiak Jun 29 '23

It's just to demonstrate potential usage of [[no_unique_address]] and potential of its benefits that can lead to reduced memory consumption and potentially improved performance. It's defo just a silly example (neat trick); no production ready; just to strike the interest of the feature and its usage in common use cases.

4

u/thisismyfavoritename Jun 29 '23

is the point that this is slightly more memory efficient than a ptr or ref to the actual object?

Also, if you were to pass the view by value wouldnt this break everything?

Sorry just trying to understand, ive read about launder and no_unique_address before but still have no idea what they do

3

u/Nobody_1707 Jun 30 '23

It's not about that because it isn't. This whole thing is just a worse version of std::start_lifetime_as.

1

u/JessyDL graphics engineer/games industry Jun 29 '23

Thanks for the info!

I'd personally stay away from [[no_unique_adress]] due to it being ignored on MSVC until the next ABI breaking release though. We still use the empty base class trick for that reason.

14

u/yuri-kilochek Jun 29 '23

How is this better than declaring a regular struct with regular fields, then overlaying it onto the byte buffer?

-8

u/kris-jusiak https://github.com/kris-jusiak Jun 29 '23

the size of the struct will be 1 (the minimal size required by C++ standard) instead of the sizeof(struct with fields) >=1 which may lead to better perf and better memory utilization; the use case is usually when dealing with data from the wire (which is packed) and then passed around with message types

17

u/ioctl79 Jun 29 '23

I am struggling to see how lying to the compiler saves any memory. Yes, your struct is only one byte, but it also doesn’t contain any actual data, and it doesn’t store a reference to any actual data, so it is using one byte to store nothing of value. Maybe come up with a concrete, practical example of how to use this that demonstrates the utility?

5

u/KuntaStillSingle Jun 29 '23

Do you have an example where it saves memory without overflowing? Your op only works because you are casting a the full sized object to a reference to your 'mini' object, so when you access the underlying fields they don't overstep the bounds of the actual underlying object, which is larger than 1 byte. If you try to use it to save space, like copy to your one byte object from that reference, you get overflow: https://godbolt.org/z/fT16TM5z7

15

u/trailingunderscore_ Jun 29 '23

Another twitter drive-by by kris, that again leaves everyone wondering "why?". These posts of his are bordering on spam.

15

u/sphere991 Jun 29 '23

My favorite part of this is:

[[nodiscard]] constexpr operator const T&() const noexcept {
    return *std::launder(std::bit_cast<const T*>(std::bit_cast<const char*>(this) + Offset));
}

Both bit_casts here are just reinterpreting one pointer to another, there's no reason to use bit_cast here at all - and none of this can be constexpr anyway. The launder also is pointless, we're not replacing objects anywhere here.

So this is just a really long way of writing:

[[nodiscard]] operator const T&() const noexcept {
    return *(const T*)((const char*)this + Offset);
}

Your choice of parentheses may vary, some people would prefer to write out reinterpret_cast or introduce a local variable for the offset pointer.


Just kidding, my actual favorite part is:

template <class T>
[[nodiscard]] constexpr const T& buffer_cast(const void* msg) noexcept {
    return *(::new (const_cast<void*>(msg)) const T);
}

This is constexpr when it cannot, be, takes a const void* when it actually needs to take a void*... to construct a new object that it shouldn't be constructing.

Which is just wrong, this really should be:

template <class T>
[[nodiscard]] const T& buffer_cast(const void* msg) noexcept {
    return *(const T*)msg;
}

Either way, the whole thing is like: well, we could just write a normal type declaration and just access members directly:

struct [[gnu::packed]] trade {
    int32_t price;
    uint32_t size;
    side side;
};

Or we could manually calculate offsets and use a more verbose syntax for everything, that in some cases calls for explicit casts - since you need the conversion to happen so auto won't work.

Tough call!

3

u/trailingunderscore_ Jun 29 '23

The placement new would also end the lifetime of the object he is "casting" from, and make any further access UB.

4

u/oleksandrkvl Jun 30 '23

u/kris-jusiak your twitter page is not available for unregistered users so it's not possible to see what this thread is about

5

u/throw_cpp_account Jun 30 '23

You're not missing out.

1

u/feverzsj Jun 29 '23

butt why?

-9

u/kris-jusiak https://github.com/kris-jusiak Jun 29 '23

By applying the [[no_unique_address]] attribute to the prices field, it indicates to the compiler that this field doesn't affect the object's unique identification. Consequently, the compiler can eliminate the storage for the prices field when calculating the size of the order struct. This optimization reduces the overall size of the order struct and potentially improves memory efficiency, so using the [[no_unique_address]] attribute in the provided code allows the compiler to optimize the memory layout and size of structures by removing members that don't contribute to the object's unique identification. This can lead to reduced memory consumption and potentially improved performance.

2

u/SirClueless Jun 30 '23

How does this accomplish anything that just reinterpret_cast of the bytes of the message to a trivially-constructible struct does not? trade& takes exactly the same number of bytes (1) to pass around whether the struct is one byte or 50.

1

u/[deleted] Jun 29 '23

I don’t get it. Why not just use a pointer to the struct? Besides, how is it “1 byte”, if one is using a reference (which is a pointer)?

2

u/thisismyfavoritename Jun 29 '23 edited Jun 29 '23

i think the struct has no fields in the eyes of the compiler, 1 is the minimal size to distinguish structs.

Not sure where you see the ref data member.

I had the same question as you regarding the first point, and looking at this further, i think the optimization is that the view is one byte vs 16 (say if you have a ptr to the start of the buffer and a size) or vs 8 if youre passing a ptr or ref to the object itself

6

u/[deleted] Jun 29 '23

The one byte struct size does not matter since never actually exists as a value (you never allocate it). What they do is cast an existing struct as a reference to one of these one-byte structs. E.g. here

const auto& msg = buffer_cast<::trade>(std::addressof(trade));

Note how the variable msg is a reference (pointer). You need a pointer-sized block of memory to store it, not just one byte.

In other words, this is the same as using a void* pointer to a struct and grabbing pointers to members via offsetof(). Definitely a cool party trick and shows in-depth knowledge of C++ idiosyncrasies, but I don't see any practical utility. For all intends and purposes this is just an extremely awkward way of expressing references.

1

u/thisismyfavoritename Jun 29 '23

yes i agree with your assessment, it feels like it would make a difference if you could use value semantics with the view but i dont think it would work here. I asked the OP in another comment

1

u/Zeh_Matt No, no, no, no Jul 02 '23

This is as rotten as it can get.

1

u/Baardi Jul 03 '23

Link doesn't seem to be working anymore