Changeset 3736 in webkit for trunk/JavaScriptCore/kjs/ustring.cpp


Ignore:
Timestamp:
Mar 3, 2003, 5:13:26 PM (22 years ago)
Author:
mjs
Message:

JavaScriptCore:

Reviewed by Trey.

  • fixed 3158833 - ebay prefs page is so slow, it seems like a hang.

92% speed improvement on ebay prefs page.
1% speed improvement on js-ibench and js-performance plt suites.

There were a couple of problems with the identifier hash table that
I fixed:

  • kjs/identifier.cpp: (void Identifier::remove): Adjust the shrink threshold to avoid constantly growing and shrinking.
  • kjs/ustring.cpp: (UString::Rep::computeHash): Use a better hash function that avoids collisions for obvious data sets.

WebFoundation:

Reviewed by Trey.

Updated string hash function to match the new, improved one in
JavaScriptCore.

  • Database.subproj/WebLRUFileList.m: (cStringHash):

WebCore:

Reviewed by Trey.

Updated string hash function to match the new, improved one in
JavaScriptCore.

  • kwq/KWQCharsets.mm: (encodingNameHash):
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/ustring.cpp

    r3373 r3736  
    178178}
    179179
     180// Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's
     181// or anything like that.
     182const unsigned PHI = 0x9e3779b9U;
     183
     184// This hash algorithm comes from:
     185// http://burtleburtle.net/bob/hash/hashfaq.html
     186// http://burtleburtle.net/bob/hash/doobs.html
    180187unsigned UString::Rep::computeHash(const UChar *s, int length)
    181188{
     
    183190    int suffixPosition = length < 16 ? 8 : length - 8;
    184191
    185     unsigned h = length;
    186     for (int i = 0; i < prefixLength; i++)
    187         h = 127 * h + s[i].uc;
    188     for (int i = suffixPosition; i < length; i++)
    189         h = 127 * h + s[i].uc;
     192    unsigned h = PHI;
     193    h += length;
     194    h += (h << 10);
     195    h ^= (h << 6);
     196
     197    for (int i = 0; i < prefixLength; i++) {
     198        h += s[i].uc;
     199        h += (h << 10);
     200        h ^= (h << 6);
     201    }
     202    for (int i = suffixPosition; i < length; i++){
     203        h += s[i].uc;
     204        h += (h << 10);
     205        h ^= (h << 6);
     206    }
     207
     208    h += (h << 3);
     209    h ^= (h >> 11);
     210    h += (h << 15);
     211 
    190212    if (h == 0)
    191213        h = 0x80000000;
     214
    192215    return h;
    193216}
    194217
     218// This hash algorithm comes from:
     219// http://burtleburtle.net/bob/hash/hashfaq.html
     220// http://burtleburtle.net/bob/hash/doobs.html
    195221unsigned UString::Rep::computeHash(const char *s)
    196222{
     
    199225    int suffixPosition = length < 16 ? 8 : length - 8;
    200226
    201     unsigned h = length;
    202     for (int i = 0; i < prefixLength; i++)
    203         h = 127 * h + (unsigned char)s[i];
    204     for (int i = suffixPosition; i < length; i++)
    205         h = 127 * h + (unsigned char)s[i];
     227    unsigned h = PHI;
     228    h += length;
     229    h += (h << 10);
     230    h ^= (h << 6);
     231
     232    for (int i = 0; i < prefixLength; i++) {
     233        h += (unsigned char)s[i];
     234        h += (h << 10);
     235        h ^= (h << 6);
     236    }
     237    for (int i = suffixPosition; i < length; i++) {
     238        h += (unsigned char)s[i];
     239        h += (h << 10);
     240        h ^= (h << 6);
     241    }
     242
     243    h += (h << 3);
     244    h ^= (h >> 11);
     245    h += (h << 15);
     246
    206247    if (h == 0)
    207248        h = 0x80000000;
     249
    208250    return h;
    209251}
Note: See TracChangeset for help on using the changeset viewer.