Changeset 143280 in webkit


Ignore:
Timestamp:
Feb 18, 2013 5:45:02 PM (11 years ago)
Author:
Darin Adler
Message:

Style tweaks to StringHasher.h
https://bugs.webkit.org/show_bug.cgi?id=110042

Reviewed by Alexey Proskuryakov.

I have a bug fix coming soon, but figured I'd separate out style tweaks and land them
first to make that patch easier to undrestand.

  • wtf/StringHasher.h: Tweak and rearrange comments a bit.

(WTF::StringHasher::addCharacter): Use character instead of ch.
(WTF::StringHasher::addCharacters): Use remainder instead of rem. Also capitalize the U
that we use to make an unsigned 1 constant.
(WTF::StringHasher::computeHashAndMaskTop8Bits): Ditto.
(WTF::StringHasher::hashMemory): Change version that takes size as a template argument
to call the version that takes the size at runtime, since both generate the same code
anyway. Added a FIXME questioning why this function uses the "mask top 8 bits" version
of the hash. Fix incorrect compile assertion that required sizes that are multiples
of four. This function works on sizes that are multiples of two. Also fixed a spelling
error where we called it a "multible".
(WTF::StringHasher::defaultConverter): Use character instead of ch.
(WTF::StringHasher::addCharactersToHash): Eliminated unnecessary local variable.

Location:
trunk/Source/WTF
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r143265 r143280  
     12013-02-18  Darin Adler  <darin@apple.com>
     2
     3        Style tweaks to StringHasher.h
     4        https://bugs.webkit.org/show_bug.cgi?id=110042
     5
     6        Reviewed by Alexey Proskuryakov.
     7
     8        I have a bug fix coming soon, but figured I'd separate out style tweaks and land them
     9        first to make that patch easier to undrestand.
     10
     11        * wtf/StringHasher.h: Tweak and rearrange comments a bit.
     12        (WTF::StringHasher::addCharacter): Use character instead of ch.
     13        (WTF::StringHasher::addCharacters): Use remainder instead of rem. Also capitalize the U
     14        that we use to make an unsigned 1 constant.
     15        (WTF::StringHasher::computeHashAndMaskTop8Bits): Ditto.
     16        (WTF::StringHasher::hashMemory): Change version that takes size as a template argument
     17        to call the version that takes the size at runtime, since both generate the same code
     18        anyway. Added a FIXME questioning why this function uses the "mask top 8 bits" version
     19        of the hash. Fix incorrect compile assertion that required sizes that are multiples
     20        of four. This function works on sizes that are multiples of two. Also fixed a spelling
     21        error where we called it a "multible".
     22        (WTF::StringHasher::defaultConverter): Use character instead of ch.
     23        (WTF::StringHasher::addCharactersToHash): Eliminated unnecessary local variable.
     24
    1252013-02-18  Alexey Proskuryakov  <ap@apple.com>
    226
  • trunk/Source/WTF/wtf/StringHasher.h

    r143116 r143280  
    1919 *
    2020 */
     21
    2122#ifndef WTF_StringHasher_h
    2223#define WTF_StringHasher_h
     
    2627namespace WTF {
    2728
    28 // Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's
    29 static const unsigned stringHashingStartValue = 0x9e3779b9U;
    30 
    3129// Paul Hsieh's SuperFastHash
    3230// http://www.azillionmonkeys.com/qed/hash.html
    33 // char* data is interpreted as latin-encoded (zero extended to 16 bits).
    34 
    35 // NOTE: This class must stay in sync with the create_hash_table script in
     31
     32// LChar data is interpreted as Latin-1-encoded (zero extended to 16 bits).
     33
     34// NOTE: The hash computation here must stay in sync with the create_hash_table script in
    3635// JavaScriptCore and the CodeGeneratorJS.pm script in WebCore.
     36
     37// Golden ratio. Arbitrary start value to avoid mapping all zeros to a hash value of zero.
     38static const unsigned stringHashingStartValue = 0x9E3779B9U;
     39
    3740class StringHasher {
    3841public:
     
    5255    }
    5356
    54     void addCharacter(UChar ch)
     57    void addCharacter(UChar character)
    5558    {
    5659        if (m_hasPendingCharacter) {
    57             addCharactersToHash(m_pendingCharacter, ch);
     60            addCharactersToHash(m_pendingCharacter, character);
    5861            m_hasPendingCharacter = false;
    5962            return;
    6063        }
    6164
    62         m_pendingCharacter = ch;
     65        m_pendingCharacter = character;
    6366        m_hasPendingCharacter = true;
    6467    }
     
    7578        }
    7679
    77         bool rem = length & 1;
     80        bool remainder = length & 1;
    7881        length >>= 1;
    7982
     
    8386        }
    8487
    85         if (rem)
     88        if (remainder)
    8689            addCharacter(Converter(*data));
    8790    }
     
    9396        // Reserving space from the high bits for flags preserves most of the hash's
    9497        // value, since hash lookup typically masks out the high bits anyway.
    95         result &= (1u << (sizeof(result) * 8 - flagCount)) - 1;
     98        result &= (1U << (sizeof(result) * 8 - flagCount)) - 1;
    9699
    97100        // This avoids ever returning a hash code of 0, since that is used to
     
    122125    {
    123126        StringHasher hasher;
    124         bool rem = length & 1;
     127        bool remainder = length & 1;
    125128        length >>= 1;
    126129
     
    130133        }
    131134
    132         if (rem)
     135        if (remainder)
    133136            hasher.addCharacter(Converter(*data));
    134137
     
    203206    }
    204207
     208    static unsigned hashMemory(const void* data, unsigned length)
     209    {
     210        // FIXME: Why does this function use the version of the hash that drops the top 8 bits?
     211        // We want that for all string hashing so we can use those bits in StringImpl and hash
     212        // strings consistently, but I don't see why we'd want that for general memory hashing.
     213        ASSERT(!(length % 2));
     214        return computeHashAndMaskTop8Bits<UChar>(static_cast<const UChar*>(data), length / sizeof(UChar));
     215    }
     216
    205217    template<size_t length> static unsigned hashMemory(const void* data)
    206218    {
    207         COMPILE_ASSERT(!(length % 4), length_must_be_a_multible_of_four);
    208         return computeHashAndMaskTop8Bits<UChar>(static_cast<const UChar*>(data), length / sizeof(UChar));
    209     }
    210 
    211     static unsigned hashMemory(const void* data, unsigned size)
    212     {
    213         ASSERT(!(size % 2));
    214         return computeHashAndMaskTop8Bits<UChar>(static_cast<const UChar*>(data), size / sizeof(UChar));
     219        COMPILE_ASSERT(!(length % 2), length_must_be_a_multiple_of_two);
     220        return hashMemory(data, length);
    215221    }
    216222
    217223private:
    218     static UChar defaultConverter(UChar ch)
    219     {
    220         return ch;
    221     }
    222 
    223     static UChar defaultConverter(LChar ch)
    224     {
    225         return ch;
     224    static UChar defaultConverter(UChar character)
     225    {
     226        return character;
     227    }
     228
     229    static UChar defaultConverter(LChar character)
     230    {
     231        return character;
    226232    }
    227233
     
    229235    {
    230236        m_hash += a;
    231         unsigned tmp = (b << 11) ^ m_hash;
    232         m_hash = (m_hash << 16) ^ tmp;
     237        m_hash = (m_hash << 16) ^ ((b << 11) ^ m_hash);
    233238        m_hash += m_hash >> 11;
    234239    }
Note: See TracChangeset for help on using the changeset viewer.