Graphics Reference
In-Depth Information
Platforms based on the MIPS architecture, for instance, have the alignment
restrictions mentioned above. On them, given the following structure definitions
struct X {
struct Y {
struct Z {
int8 a;
int8 a, pad_a[7];
int64 b;
int64 b;
int64 b;
int64 e;
int8 c;
int8 c, pad_c[1];
float f;
int16 d;
int16 d, pad_d[2];
int16 d;
int64 e;
int64 e;
int8 a;
float f;
float f,pad_f[1];
int8 c;
};
};
};
most compilers will have sizeof(X) == 40 , sizeof(Y) == 40 , and sizeof(Z) == 24
(assuming 4-byte floats). The padding added to structure X (as indicated by struc-
ture Y ) constitutes 40% of its memory requirements, compared to the identical, but
size-sorted, Z structure. Other ways of decreasing the size of a structure include the
following.
Do not store data that can be easily computed fromalready stored values. For example,
if the three vertices of a triangle have already been fetched from memory, it may
actually be more efficient to derive a normal or a plane equation directly from the
vertices instead of potentially incurring additional cache line fetches by reading
additional data from the triangle's data structure.
Use smaller integers where possible. Instead of always using the default-size inte-
gers (frequently of 32 bits or more), consider whether the data will fit in just 16
or perhaps even 8 bits. For example, if the player health is given as an integral
percentage in the range 0 to 100, store it as an 8-bit byte and not a 32-bit integer.
Use bit fields instead of Booleans. Instead of declaring flags as bool variables, pack
all flags together as bits in a bit field. The size of a bool is machine dependent,
but is at least a byte and can be as much as the size of a long integer. Storing
flags as bits will therefore reduce the memory required for them by at least a
factor of 8 and often up to a factor of 32 or more.
Use offsets instead of pointers to index data structures. Whereas pointers are always
a fixed size of usually 4 (or more) bytes, the size of an offset can be changed
depending on how many items must be indexed. For example, a 2-byte offset
can index an array of 2 16 objects (or even more if the offset is taken relative to
the current object). Few situations require indexing of that many objects.
With data cache misses being very expensive, more elaborate schemes to compress
and uncompress data can also prove worthwhile. Some data compression examples
are given in Sections 13.3.2 and 13.4.
 
Search WWH ::




Custom Search