Union support not optimal in memory usage [13725] #165
Replies: 5 comments
-
Is this optimization still on the table? I'm interested in this feature. It looks to me that this could be done quite simply using an anonymous union:[https://en.cppreference.com/w/cpp/language/union] Basically, the emitted code is already a variant (aka discriminating union), in that it has an enum type for the active member, followed by the allowed member types. Simply surrounding these types in |
Beta Was this translation helpful? Give feedback.
-
To be explicit, surrounding here with |
Beta Was this translation helpful? Give feedback.
-
It is not as simple when string or structured types are used within the union, at that moment initialization and destruction requires additional code |
Beta Was this translation helpful? Give feedback.
-
Yes, and std::vector for the sequence too. The easier path would be to use std::variant which provides exactly the discriminating union type needed, but it can be an up-hill battle to get c++17 features accepted. |
Beta Was this translation helpful? Give feedback.
-
I see that this is a proposal for achieving a more optimal behavior regarding memory usage. For that reason, I proceed and convert this issue to a discussion. |
Beta Was this translation helpful? Give feedback.
-
The current code generation for IDL unions generates a class with for each case a plain member resulting in memory usage for all types independent of the discriminator, see for example https://github.com/eProsima/Fast-DDS/blob/6fa5e5cc8abcf506e70be203934bd3c522bd3edf/test/xtypes/idl/Types.h#L4693. This leads to a lot of memory usage when larger unions are used. This can be done much more optimized by using an union internally, see for example https://github.com/RemedyIT/idl2cpp11/blob/master/examples/union/testC.h for a possible solution
Beta Was this translation helpful? Give feedback.
All reactions