Game Engine Architecture

(Ben Green) #1
223


  • Therefore, functions in a single translation unit are always contiguous
    in memory. That is, the linker never splits up a complied translation unit
    (.obj fi le) in order to intersperse code from some other translation unit.
    So, following the same principles that apply to avoiding D-cache misses,
    we should follow the rules of thumb listed below.

  • Keep high-performance code as small as possible, in terms of number of
    machine language instructions. (The compiler and linker take care of
    keeping our functions contiguous in memory.)

  • Avoid calling functions from within a performance-critical section of
    code.

  • If you do have to call a function, place it as close as possible to the calling
    function—preferably immediately before or aft er the calling function
    and never in a diff erent translation unit (because then you completely
    lose control over its proximity to the calling function).

  • Use inline functions judiciously. Inlining a small function can be a big
    performance boost. However, too much inlining bloats the size of the
    code, which can cause a performance-critical section of code to no
    longer fi t within the cache. Let’s say we write a tight loop that processes
    a large amount of data—if the entire body of that loop doesn’t fi t into
    the cache, then we are signing up for two I-cache misses during every
    iteration of the loop. In such a situation, it is probably best to rethink the
    algorithm and/or implementation so that less code is required within
    critical loops.


5.3 Containers


Game programmers employ a wide variety of collection-oriented data struc-
tures, also known as containers or collections. The job of a container is always
the same—to house and manage zero or more data elements; however, the
details of how they do this varies greatly, and each type of container has its
pros and cons. Common container data types include, but are certainly not
limited to, the following.



  • Array. An ordered, contiguous collection of elements accessed by index.
    The length of the array is usually statically defi ned at compile time. It
    may be multidimensional. C and C++ support these natively (e.g., int
    a[5]).

  • Dynamic array. An array whose length can change dynamically at
    runtime (e.g., STL’s std::vector)


5.3. Containers

Free download pdf