Bug 729952 - Part 2: Use a better hash function in nsCRT, nsTHashtable, and pldhash. r=bz

This commit is contained in:
Justin Lebar 2012-03-02 17:20:44 -05:00
parent b3d50b3dad
commit 5816447226
3 changed files with 25 additions and 16 deletions

View File

@ -54,11 +54,10 @@
#include "nsCRT.h"
#include "nsIServiceManager.h"
#include "nsCharTraits.h"
#include "prbit.h"
#include "nsUTF8Utils.h"
#include "mozilla/HashFunctions.h"
#define ADD_TO_HASHVAL(hashval, c) \
hashval = PR_ROTATE_LEFT32(hashval, 4) ^ (c);
using namespace mozilla;
//----------------------------------------------------------------------
@ -221,11 +220,13 @@ PRUint32 nsCRT::HashCode(const char* str, PRUint32* resultingStrLen)
if (!str) return h;
unsigned char c;
while ( (c = *s++) )
ADD_TO_HASHVAL(h, c);
while ( (c = *s++) ) {
h = AddToHash(h, c);
}
if ( resultingStrLen )
*resultingStrLen = (s-str)-1;
return h;
}
@ -238,7 +239,7 @@ PRUint32 nsCRT::HashCode(const char* start, PRUint32 length)
unsigned char c;
while ( s < end ) {
c = *s++;
ADD_TO_HASHVAL(h, c);
h = AddToHash(h, c);
}
return h;
@ -253,10 +254,11 @@ PRUint32 nsCRT::HashCode(const PRUnichar* str, PRUint32* resultingStrLen)
PRUnichar c;
while ( (c = *s++) )
ADD_TO_HASHVAL(h, c);
h = AddToHash(h, c);
if ( resultingStrLen )
*resultingStrLen = (s-str)-1;
return h;
}
@ -269,7 +271,7 @@ PRUint32 nsCRT::HashCode(const PRUnichar* start, PRUint32 length)
PRUnichar c;
while ( s < end ) {
c = *s++;
ADD_TO_HASHVAL(h, c);
h = AddToHash(h, c);
}
return h;
@ -292,11 +294,10 @@ PRUint32 nsCRT::HashCodeAsUTF16(const char* start, PRUint32 length,
}
if (ucs4 < PLANE1_BASE) {
ADD_TO_HASHVAL(h, ucs4);
h = AddToHash(h, ucs4);
}
else {
ADD_TO_HASHVAL(h, H_SURROGATE(ucs4));
ADD_TO_HASHVAL(h, L_SURROGATE(ucs4));
h = AddToHash(h, H_SURROGATE(ucs4), L_SURROGATE(ucs4));
}
}

View File

@ -38,6 +38,9 @@
#include "nsTHashtable.h"
#include "nsHashKeys.h"
#include "prbit.h"
#include "mozilla/HashFunctions.h"
using namespace mozilla;
PRUint32
HashString( const nsAString& aStr )
@ -55,7 +58,7 @@ HashString( const nsAString& aStr )
#endif
while (begin != end) {
code = PR_ROTATE_LEFT32(code, 4) ^ PRUint32(*begin);
code = AddToHash(code, *begin);
++begin;
}
@ -78,7 +81,7 @@ HashString( const nsACString& aStr )
#endif
while (begin != end) {
code = PR_ROTATE_LEFT32(code, 4) ^ PRUint32(*begin);
code = AddToHash(code, *begin);
++begin;
}
@ -89,9 +92,10 @@ PRUint32
HashString(const char *str)
{
PRUint32 code = 0;
const char *origStr = str;
while (*str) {
code = PR_ROTATE_LEFT32(code, 4) ^ PRUint32(*str);
code = AddToHash(code, *str);
++str;
}
@ -102,9 +106,10 @@ PRUint32
HashString(const PRUnichar *str)
{
PRUint32 code = 0;
const PRUnichar *origStr = str;
while (*str) {
code = PR_ROTATE_LEFT32(code, 4) ^ PRUint32(*str);
code = AddToHash(code, *str);
++str;
}

View File

@ -48,6 +48,7 @@
#include <string.h>
#include "prbit.h"
#include "pldhash.h"
#include "mozilla/HashFunctions.h"
#include "nsDebug.h" /* for PR_ASSERT */
#ifdef PL_DHASHMETER
@ -108,6 +109,8 @@
#endif /* defined(DEBUG) */
using namespace mozilla;
void *
PL_DHashAllocTable(PLDHashTable *table, PRUint32 nbytes)
{
@ -128,7 +131,7 @@ PL_DHashStringKey(PLDHashTable *table, const void *key)
h = 0;
for (s = (const unsigned char *) key; *s != '\0'; s++)
h = PR_ROTATE_LEFT32(h, 4) ^ *s;
h = AddToHash(h, *s);
return h;
}