Search code examples
c++hash

Compile Time Hashing C++0x


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.


Solution

  • You'd have to implement the right hashing algorithm, but this could work using C++11's constexpr functions:

    #include <iostream>
    
    // Dummy hashing algorithm. Adds the value of every char in the cstring.
    constexpr unsigned compile_time_hash(const char* str) {
        // Modify as you wish
        return (*str == 0) ? 0 : (*str + compile_time_hash(str + 1));
    }   
    
    int main() {
        unsigned some_hash = compile_time_hash("hallou");
        std::cout << some_hash << std::endl;
    }
    

    Then you could have an overload of ResourcesManager::get which takes the result of compile_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.