Hi Thomas,
On Wed, Aug 22, 2018 at 5:33 PM, <[email protected]> wrote:
> I am trying to send a small payload over UDP and wanted to know, as the
> documentation is hard to find for unions, how the encoding works for unions.
>
>
> 1. Do unions take up a byte-size equivalent to their largest data type
> within?
> 2. And if not, how is union memory allocated?
>
> Unions work basically as you'd expect. The size of the union is the
maximum size of any member, plus 2 bytes for a tag.
There's one complication: Cap'n Proto data is split into fixed-width
primitive data (integers, etc.) and pointers (Text, Data, structs, lists,
and interfaces). In order to ensure that the message structure can be
understood without having the latest version of the schema, data and
pointers need to be kept in separate sections. That means that if you union
an integer and a pointer, they have to be allocated into different places
in the struct, so the total size of the union is the sum of the two rather
than the maximum of the two.
However, the usual way to use a union is to have a union of a bunch of
struct types. In this case, all of the members of the union are pointers to
external structs. So the size of the union is just the one pointer (plus
tag).
The size of a union is most complicated when you have a union-of-groups,
since groups are inlined into the parent struct. In this case the exact
member fields of each group matter.
>
> 1. Will I be using more data for a union with 5 members vs 100 members?
>
> It depends on the types of the members (see above).
>
> 1. Is it better to use a union or send an enum "packed type" to
> describe the data?
>
> The following definitions have exactly the same size (assuming Foo, Bar,
and Baz are struct types):
struct UnionVariant {
union {
foo @0 :Foo;
bar @1 :Bar;
baz @2 :Baz;
}
}
struct EnumVariant {
tag @0 :Tag;
enum Tag {
foo @0;
bar @1;
baz @2;
}
value @1 :AnyStruct; # type determined by `tag`
}
In fact, the two definitions above are wire-compatible! I strongly
recommend the union because it is type-safe.
However, if you were to hand-roll an encoding involving a tag followed by a
struct of that type, it would be smaller, because you'd avoid the overhead
of an extra pointer.
-Kenton
--
You received this message because you are subscribed to the Google Groups
"Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
Visit this group at https://groups.google.com/group/capnproto.