Using GCC 4.4 (generally the max available for Android and IOS) is there a way to do compile time hashing of strings.
We have a resource manager that maps string keys to resources. While the lookup is fast the hashing and string creation is slow. Something like:
ResourcesManager::get<Texture>("someKey");
Spends a lot of time allocating the string “someKey” and then hashing it.
I’m wondering if there is a trick I can use to hash it at compile time.
You’d have to implement the right hashing algorithm, but this could work using C++11’s constexpr functions:
Then you could have an overload of
ResourcesManager::getwhich takes the result ofcompile_time_hash(an unsigned, in this case).This obviously depends on which hashing algorithm you’re applying. Implementing something like SHA* using constexpr will be pretty painful.
Note that you require GCC >= 4.6 or clang >= 3.1 in order to use constexpr.