ABSTRACT

Every now and then in game programming, there comes a need to look up some entity, component, or resource by name. Even though true string-based lookups are mostly avoided where possible, there are situations where they are the best solution to the problem at hand. Examples include looking up certain bones in an animated rig in order to attach special effects, or looking up hardcoded assets such as meshes or textures in case a resource is missing or could not be loaded correctly. In professional game engines, expensive string operations such as strcmp(), strlen(), and others are avoided where possible in run-time code. Thus, the prevalent technique for performing string-based lookups is to use hash-based lookups instead. Note that this does not imply that a hash-based data structure is used for storing the data, but instead refers to the fact that all string-based identifiers such as resource names have been hashed by the engine’s content pipeline in an offline process. The benefits of this approach are two-fold. First, a hash is a single integer value of fixed size, which simplifies the data and memory layout. Second, lookups in the engine code can be performed by using integer comparisons rather than costly string comparisons, as exemplified in Listing 14.1. While this approach is a vast improvement over using std::string or const char * for lookup purposes, there is still overhead present, namely in all code that calls functions that accept a hash as an argument, as shown in Listing 14.2. Every time the FindBoneIndex() function is called, a hash must be generated at run time from a given string first.