Skip to content
Snippets Groups Projects
Commit c074b18f authored by Christian Kamm's avatar Christian Kamm
Browse files

C++: Improve Literal::hashCode.

This can have a dramatic impact on performance when a file contains lots
of unique literals.

Change-Id: I5309b28f704d7f53e164dc8084ae08354c09354b
Reviewed-on: http://codereview.qt.nokia.com/4312


Reviewed-by: default avatarRoberto Raggi <roberto.raggi@nokia.com>
parent dcf835a5
No related branches found
No related tags found
No related merge requests found
......@@ -75,9 +75,21 @@ unsigned Literal::hashCode() const
unsigned Literal::hashCode(const char *chars, unsigned size)
{
unsigned h = 0;
for (unsigned i = 0; i < size; ++i)
h = (h >> 5) - h + chars[i];
/* Hash taken from QtCore's qHash for strings, which in turn has the note:
These functions are based on Peter J. Weinberger's hash function
(from the Dragon Book). The constant 24 in the original function
was replaced with 23 to produce fewer collisions on input such as
"a", "aa", "aaa", "aaaa", ...
*/
uint h = 0;
while (size--) {
h = (h << 4) + *chars++;
h ^= (h & 0xf0000000) >> 23;
h &= 0x0fffffff;
}
return h;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment