Performance improvements for hash function.

This makes the code a little less pretty, but it's important to get this function to be really fast, since it gets called for every cached request.