Optimal structure field ordering

Steven Ryan smryan at garth.UUCP
Tue Jun 28 08:43:03 AEST 1988


>	"The space requirement of any given C structure may be easily
>	optimized by reorganizing the structure fields in order of decreasing
>	base type size."

Some machines provide an item offset, base address+n chars or words or ...,
where the hardware shifts the offset before adding to the base, and some of
these same machines handle small offsets or a small range of offsets. In these
cases, it is more efficient to put all the small fields first.

For example, Cyber 205 provides bit, byte, halfword, and word loads with item
offsets of the form  LODx  [base-address-register,offset-register],destination.
If elements are arranged 1 bit, 1 byte, 1 halfword, 1 word, the item offsets
are (halfword=32 bits, word=64 bits):
          small to large                   large to small
            +0 bit                             +0 word
            +1 byte                            +2 halfword
            +1 halfword                        +12 byte
            +1 word                            +104 bit
In one case, the program only needs two constant registers and the other
needs 4. It is very difficult to find a general rule to best pack a structure.
One of the easiest is to throw whole mess at the compiler and say gimme your
best shot.



More information about the Comp.lang.c mailing list