#Copy Elision?

12 messages · Page 1 of 1 (latest)

dawn compass
#
struct io_uring_sqe *sqe = io_uring_get_sqe(ring);
*sqe = (struct io_uring_sqe){
    .opcode = IORING_OP_SOCKET,
    .fd = domain,
    .off = type,
    .len = protocol,
    .user_data = user_data,
    .flags = flags
};

if I do this the compiler is responsible for zeroing the unused fields as well as assigning the set ones right?
becaus it's prvalue I belive I should not end up with an allocation even on the stack right? This ends up being better than setting all the fields manually including zeroing them out right?

rapid viperBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

safe whale
#

This isn't valid C++, compound literals are a C-only thing

#

In C++ this would be

*sqe = {
    .opcode = IORING_OP_SOCKET,
    .fd = domain,
    .off = type,
    .len = protocol,
    .user_data = user_data,
    .flags = flags
};
#

But yes, both languages zero the uninitialized fields when doing partial initialization. Whether or not the temporary object will get optimized away is bing_shrug
It should be

dawn compass
#

Oh whoops my bad, okay gotcha. I thought that this type of optimization was "guaranteed"

safe whale
#

In C++ when you do something like io_uring_sqe blah = io_uring_sqe{...};, it's guaranteed that it's the same as io_uring_sqe blah{...}; in the sense that no copy/move ctors are called.

But that's only for creating a new object. When assigning to an existing object, you can't optimize out the assignment

#

The C/C++ standards don't describe what the stack is, so they can't guarantee this kind of optimizations (what is or isn't created on the stack). All they can guarantee is how many times copy/move ctors/etc are called, which doesn't apply here, because the struct has all of them trivial, so you can't observe (from the standard's pov) how often they're called

dawn compass
#

HUmm okay, ill see what the asm looks like and compare to memcpy to zero and then assignment to each field on it's own.

#

Thank you

rapid viperBOT
#

@dawn compass Has your question been resolved? If so, type !solved :)

dawn compass
#

!solved