Game Engine Architecture

(Ben Green) #1

230 5. Engine Support Systems


However, the STL also has numerous drawbacks, including:


  • STL has a steep learning curve. The documentation is now quite good,
    but the header fi les are cryptic and diffi cult to understand on most plat-
    forms.

  • STL is oft en slower than a data structure that has been craft ed specifi -
    cally for a particular problem.

  • STL also almost always eats up more memory than a custom-designed
    data structure.

  • STL does a lot of dynamic memory allocation, and it’s sometimes chal-
    lenging to control its appetite for memory in a way that is suitable for
    high-performance, memory-limited console games.

  • STL’s implementation and behavior varies slightly from compiler to
    compiler, making its use in multiplatform engines more diffi cult.
    As long as the programmer is aware of the pitfalls of STL and uses it ju-
    diciously, it can have a place in game engine programming. It is best suited
    to a game engine that will run on a personal computer platform, because the
    advanced virtual memory systems on modern PCs make memory allocation
    cheaper, and the probability of running out of physical RAM is oft en negli-
    gible. On the other hand, STL is not generally well-suited for use on memory-
    limited consoles that lack advanced CPUs and virtual memory. And code that
    uses STL may not port easily to other platforms. Here are some rules of thumb
    that I use:

  • First and foremost, be aware of the performance and memory character-
    istics of the particular STL class you are using.

  • Try to avoid heavier-weight STL classes in code that you believe will be
    a performance bott leneck.

  • Prefer STL in situations where memory is not at a premium. For ex-
    ample, embedding a std::list inside a game object is OK, but em-
    bedding a std::list inside every vertex of a 3D mesh is probably not
    a good idea. Adding every vertex of your 3D mesh to a std::list is
    probably also not OK—the std::list class dynamically allocates a
    small “link” object for every element inserted into it, and that can result
    in a lot of tiny, fragmented memory allocations.

  • If your engine is to be multiplatform, I highly recommend STLport
    (htt p://www.stlport.org), an implementation of STL that was specifi cally
    designed to be portable across a wide range of compilers and target
    platforms, more effi cient, and more feature-rich than the original STL
    implementations.

Free download pdf