c - Efficiency of structs of length 8, and uint64_t -
tldr: 8-byte structures handled efficiently 8-byte uint64_t?
i have 3 data structures similar. 6, 7 , 8 bytes long. want put them in uint64_t variables. goal comparisons , assignments efficient. (these values used key in several (large) trees).
example: have defined following data-structure, 1 7 bytes long.
typedef struct { union { uint64_t raw; struct { uint8_t unused; uint8_t node_number; uint8_t systemid[systemid_length]; /* systemid_length 6 bytes. */ } nodeid; }; } nodeid_t;
i can quick assignments , copies via raw union member.
nodeid_t x, y; x.raw = y.raw if (x.raw > y.raw) {
etc, etc.
my question use in functions , in assignments. when pass struct value, compiler (gcc) recognize these structures 8 bytes long. , therefore treat if int64_t?
example: there efficiency/performance differences between:
int64_t my_function(); nodeid_t my_function();
in other words, gcc use 1 instruction put nodeid_t on stack, if integer? or create loop, , copy 8 bytes 1 one? depend on -o optimization?
same question assignment.
int64_t a, b; nodeid_t x, y; = b; /* 1 machine instruction, hope. */ x = y; /* 1 instruction, or loop ? */
you cannot union
has same size uint64_t
.
this due packing in nodeid struct
: compilers insert gaps between struct
members. compilers allow change packing arrangements code not portable.
it safer have array of uint8_t
: memory contiguous.
a compiler copy memory on assignment, may use nodeid_t
function return types.
your second job rename nodeid_t
: _t
suffixes reserved in posix c.
Comments
Post a Comment