Changeset 143280 in webkit
- Timestamp:
- Feb 18, 2013 5:45:02 PM (11 years ago)
- Location:
- trunk/Source/WTF
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WTF/ChangeLog
r143265 r143280 1 2013-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 1 25 2013-02-18 Alexey Proskuryakov <ap@apple.com> 2 26 -
trunk/Source/WTF/wtf/StringHasher.h
r143116 r143280 19 19 * 20 20 */ 21 21 22 #ifndef WTF_StringHasher_h 22 23 #define WTF_StringHasher_h … … 26 27 namespace WTF { 27 28 28 // Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's29 static const unsigned stringHashingStartValue = 0x9e3779b9U;30 31 29 // Paul Hsieh's SuperFastHash 32 30 // 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 36 35 // 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. 38 static const unsigned stringHashingStartValue = 0x9E3779B9U; 39 37 40 class StringHasher { 38 41 public: … … 52 55 } 53 56 54 void addCharacter(UChar ch )57 void addCharacter(UChar character) 55 58 { 56 59 if (m_hasPendingCharacter) { 57 addCharactersToHash(m_pendingCharacter, ch );60 addCharactersToHash(m_pendingCharacter, character); 58 61 m_hasPendingCharacter = false; 59 62 return; 60 63 } 61 64 62 m_pendingCharacter = ch ;65 m_pendingCharacter = character; 63 66 m_hasPendingCharacter = true; 64 67 } … … 75 78 } 76 79 77 bool rem = length & 1;80 bool remainder = length & 1; 78 81 length >>= 1; 79 82 … … 83 86 } 84 87 85 if (rem )88 if (remainder) 86 89 addCharacter(Converter(*data)); 87 90 } … … 93 96 // Reserving space from the high bits for flags preserves most of the hash's 94 97 // value, since hash lookup typically masks out the high bits anyway. 95 result &= (1 u<< (sizeof(result) * 8 - flagCount)) - 1;98 result &= (1U << (sizeof(result) * 8 - flagCount)) - 1; 96 99 97 100 // This avoids ever returning a hash code of 0, since that is used to … … 122 125 { 123 126 StringHasher hasher; 124 bool rem = length & 1;127 bool remainder = length & 1; 125 128 length >>= 1; 126 129 … … 130 133 } 131 134 132 if (rem )135 if (remainder) 133 136 hasher.addCharacter(Converter(*data)); 134 137 … … 203 206 } 204 207 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 205 217 template<size_t length> static unsigned hashMemory(const void* data) 206 218 { 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); 215 221 } 216 222 217 223 private: 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; 226 232 } 227 233 … … 229 235 { 230 236 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); 233 238 m_hash += m_hash >> 11; 234 239 }
Note: See TracChangeset
for help on using the changeset viewer.