Game Development Reference
In-Depth Information
Your penalty for using the SlowStruct over FastStruct isabout5percentonmytest
machine. The penalty for using ReallySlowStruct is code that runs 1.5 times as slowly.
The first structure isn
t even aligned properly on bit boundaries, hence the name
ReallySlowStruct . The definition of the 6-bit char variable throws the entire
structure out of alignment. The second structure, SlowStruct ,isalsooutofalign-
ment, but at least the byte boundaries are aligned. The last structure, FastStruc t, is
completely aligned for each member. The last member, unused, ensures that the struc-
ture fills out to an 8-byte boundary in case someone declares an array of FastStruct .
Notice the #pragma pack(push, 1 ) at the top of the source example? It ' s accompa-
nied by a #pragma pack(pop ) at the bottom. Without them, the compiler, depend-
ing on your project settings, will choose to spread out the member variables and
place each one on an optimal byte boundary. When the member variables are spread
out like that, the CPU can access each member quickly, but all that unused space can
add up. If the compiler were left to optimize SlowStruct by adding unused bytes,
each structure would be 24 bytes instead of just 14. Seven extra bytes are padded after
the first char variable, and the remaining bytes are added at the end. This ensures
that the entire structure always starts on an 8-byte boundary. That
'
s about 40 percent
of wasted space, all due to a careless ordering of member variables.
Don
'
'
t let the compiler waste precious memory space. Put some of your brain cells to
work and align your own member variables. You don ' t get many opportunities to
save memory and optimize CPU at the same time.
Virtual Memory
Virtual memory increases the addressable memory space by caching unused memory
blocks to the hard disk. The scheme depends on the fact that even though you might
have a 500MB data structure, you aren
t going to be playing with the whole thing at
the same time. The unused bits are saved off to your hard disk until you need them
again. You should be cheering and wincing at the same time. Cheering because every
programmer likes having a big memory playground, and wincing because anything
involving the hard disk wastes a lot of time.
'
Cache Misses Can Cost You Dearly
Any time a cache is used inefficiently, you can degrade the overall performance
of your game by many orders of magnitude. This is commonly called
and it is your worst nightmare. If your game is
thrashing cache, you might be able to solve the problem by reordering some
code, but most likely you will need to reduce the size of the data.
thrashing the cache,
 
 
Search WWH ::




Custom Search