ParameterValue{enum Type {STRING_TYPE, NUMERIC_TYPE};Type type;std::string
string_value;float numeric_value;}; typedef std::map Parameters; struct
SoundInstance{ Other members...Parameters *parameters;}; std::vector
playing_sounds;which would result in tons of pointer chasing, memory allocation
and data copying.So let's fix it!First, let's get rid of the FIFA Coins strings. Strings
should almost only be used for text that is displayed to the end user. For
everything else, they are usually a
idea. In this case, since the only thing we need to do FUT 18 Coins is match strings that
are equal (find the parameter named "material", check if its is value "wood",
etc), we can use a hash instead of the full string value:struct
ParameterValue{enum Type {STRING_TYPE, NUMERIC_TYPE};Type type;union {IdString32
string_value;float numeric_value;};}; typedef std::map Parameters;IdString32 is
our type for representing hashed strings. It just stores a 4-byte string hash.
Since it is a POD-type, we
put it in a union together with the numeric value. This takes the
ParameterValue struct down to a manageable 8 bytes with no dynamic data
allocation.But we can actually make it even smaller, by just getting rid of the
type:union ParameterValue {IdString32 string_value;float numeric_value;};We can
do this because when we access the parameter we know which type we want. If we
are evaluating a curve, we want a numeric value. If we want to compare it to a
hash, we want a string value
Getting rid of the type means we can't assert() on type errors (if someone
has done something silly like setting the "material" to U2FIFA 3.5 or the "force" to
"banana"). But other than that, everything will work as before.Next, let's
attack the map:typedef std::map Parameters;Just like std::string, std::map
should set off all kinds of warning bells in your head. std::map is almost never
a good choice. Better alternatives are: linear search in a std::vector (for
smallish maps), binary search in a sorted