[SCM] WebKit Debian packaging branch, debian/experimental, updated. upstream/1.3.3-9427-gc2be6fc
commit-queue at webkit.org
commit-queue at webkit.org
Wed Dec 22 13:44:55 UTC 2010
The following commit has been merged in the debian/experimental branch:
commit 489c94ff36c341868c4aca1f377b835e21063638
Author: commit-queue at webkit.org <commit-queue at webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Date: Fri Sep 24 21:06:27 2010 +0000
2010-09-24 Patrick Gansterer <paroga at paroga.com>
Reviewed by Gavin Barraclough.
Add WTF::StringHasher
https://bugs.webkit.org/show_bug.cgi?id=45970
StringHasher is a class for calculation stringHash out of character string.
This class will unify the different usages of the same algorithm.
* wtf/StringHashFunctions.h:
(WTF::StringHasher::StringHasher):
(WTF::StringHasher::addCharacters):
(WTF::StringHasher::addCharacter):
(WTF::StringHasher::hash):
(WTF::StringHasher::createHash):
(WTF::StringHasher::defaultCoverter):
(WTF::StringHasher::addCharactersToHash):
(WTF::stringHash):
git-svn-id: http://svn.webkit.org/repository/webkit/trunk@68289 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/JavaScriptCore/ChangeLog b/JavaScriptCore/ChangeLog
index 84aeb39..dc389d3 100644
--- a/JavaScriptCore/ChangeLog
+++ b/JavaScriptCore/ChangeLog
@@ -1,3 +1,23 @@
+2010-09-24 Patrick Gansterer <paroga at paroga.com>
+
+ Reviewed by Gavin Barraclough.
+
+ Add WTF::StringHasher
+ https://bugs.webkit.org/show_bug.cgi?id=45970
+
+ StringHasher is a class for calculation stringHash out of character string.
+ This class will unify the different usages of the same algorithm.
+
+ * wtf/StringHashFunctions.h:
+ (WTF::StringHasher::StringHasher):
+ (WTF::StringHasher::addCharacters):
+ (WTF::StringHasher::addCharacter):
+ (WTF::StringHasher::hash):
+ (WTF::StringHasher::createHash):
+ (WTF::StringHasher::defaultCoverter):
+ (WTF::StringHasher::addCharactersToHash):
+ (WTF::stringHash):
+
2010-09-24 Oliver Hunt <oliver at apple.com>
Reviewed by Geoffrey Garen.
diff --git a/JavaScriptCore/wtf/StringHashFunctions.h b/JavaScriptCore/wtf/StringHashFunctions.h
index 07f117f..64988cb 100644
--- a/JavaScriptCore/wtf/StringHashFunctions.h
+++ b/JavaScriptCore/wtf/StringHashFunctions.h
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2005, 2006, 2008, 2010 Apple Inc. All rights reserved.
+ * Copyright (C) 2010 Patrick Gansterer <paroga at paroga.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@@ -27,129 +28,152 @@ namespace WTF {
// Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's
static const unsigned stringHashingStartValue = 0x9e3779b9U;
-// stringHash methods based on Paul Hsieh's SuperFastHash.
+// Paul Hsieh's SuperFastHash
// http://www.azillionmonkeys.com/qed/hash.html
// char* data is interpreted as latin-encoded (zero extended to 16 bits).
+class StringHasher {
+public:
+ inline StringHasher()
+ : m_hash(stringHashingStartValue)
+ , m_cachedCharacter(invalidCharacterValue)
+ {
+ }
-inline unsigned stringHash(const UChar* data, unsigned length)
-{
- unsigned hash = WTF::stringHashingStartValue;
- unsigned rem = length & 1;
- length >>= 1;
-
- // Main loop
- for (; length > 0; length--) {
- hash += data[0];
- unsigned tmp = (data[1] << 11) ^ hash;
- hash = (hash << 16) ^ tmp;
- data += 2;
- hash += hash >> 11;
+ inline void addCharacters(UChar a, UChar b)
+ {
+ ASSERT(m_cachedCharacter == invalidCharacterValue);
+ addCharactersToHash(a, b);
}
- // Handle end case
- if (rem) {
- hash += data[0];
- hash ^= hash << 11;
- hash += hash >> 17;
+ inline void addCharacter(UChar ch)
+ {
+ ASSERT(ch != invalidCharacterValue);
+ if (m_cachedCharacter != invalidCharacterValue) {
+ addCharactersToHash(m_cachedCharacter, ch);
+ m_cachedCharacter = invalidCharacterValue;
+ return;
+ }
+
+ m_cachedCharacter = ch;
}
- // Force "avalanching" of final 127 bits
- hash ^= hash << 3;
- hash += hash >> 5;
- hash ^= hash << 2;
- hash += hash >> 15;
- hash ^= hash << 10;
+ inline unsigned hash() const
+ {
+ unsigned result = m_hash;
- hash &= 0x7fffffff;
+ // Handle end case.
+ if (m_cachedCharacter != invalidCharacterValue) {
+ result += m_cachedCharacter;
+ result ^= result << 11;
+ result += result >> 17;
+ }
- // this avoids ever returning a hash code of 0, since that is used to
- // signal "hash not computed yet", using a value that is likely to be
- // effectively the same as 0 when the low bits are masked
- if (hash == 0)
- hash = 0x40000000;
+ // Force "avalanching" of final 31 bits.
+ result ^= result << 3;
+ result += result >> 5;
+ result ^= result << 2;
+ result += result >> 15;
+ result ^= result << 10;
- return hash;
-}
+ // First bit is used in UStringImpl for m_isIdentifier.
+ result &= 0x7fffffff;
-inline unsigned stringHash(const char* data, unsigned length)
-{
- unsigned hash = WTF::stringHashingStartValue;
- unsigned rem = length & 1;
- length >>= 1;
-
- // Main loop
- for (; length > 0; length--) {
- hash += static_cast<unsigned char>(data[0]);
- unsigned tmp = (static_cast<unsigned char>(data[1]) << 11) ^ hash;
- hash = (hash << 16) ^ tmp;
- data += 2;
- hash += hash >> 11;
- }
+ // This avoids ever returning a hash code of 0, since that is used to
+ // signal "hash not computed yet", using a value that is likely to be
+ // effectively the same as 0 when the low bits are masked.
+ if (!result)
+ return 0x40000000;
- // Handle end case
- if (rem) {
- hash += static_cast<unsigned char>(data[0]);
- hash ^= hash << 11;
- hash += hash >> 17;
+ return result;
}
- // Force "avalanching" of final 127 bits
- hash ^= hash << 3;
- hash += hash >> 5;
- hash ^= hash << 2;
- hash += hash >> 15;
- hash ^= hash << 10;
+ template<typename T, UChar Coverter(T)> static inline unsigned createHash(const T* data, unsigned length)
+ {
+ StringHasher hasher;
+ bool rem = length & 1;
+ length >>= 1;
- hash &= 0x7fffffff;
+ while (length--) {
+ hasher.addCharacters(Coverter(data[0]), Coverter(data[1]));
+ data += 2;
+ }
- // this avoids ever returning a hash code of 0, since that is used to
- // signal "hash not computed yet", using a value that is likely to be
- // effectively the same as 0 when the low bits are masked
- if (hash == 0)
- hash = 0x40000000;
+ if (rem)
+ hasher.addCharacter(Coverter(*data));
- return hash;
-}
+ return hasher.hash();
+ }
-inline unsigned stringHash(const char* data)
-{
- unsigned hash = WTF::stringHashingStartValue;
-
- // Main loop
- for (;;) {
- unsigned char b0 = data[0];
- if (!b0)
- break;
- unsigned char b1 = data[1];
- if (!b1) {
- hash += b0;
- hash ^= hash << 11;
- hash += hash >> 17;
- break;
+ template<typename T, UChar Coverter(T)> static inline unsigned createHash(const T* data)
+ {
+ StringHasher hasher;
+
+ while (true) {
+ UChar b0 = Coverter(*data++);
+ if (!b0)
+ break;
+ UChar b1 = Coverter(*data++);
+ if (!b1) {
+ hasher.addCharacter(b0);
+ break;
+ }
+
+ hasher.addCharacters(b0, b1);
}
- hash += b0;
- unsigned tmp = (b1 << 11) ^ hash;
- hash = (hash << 16) ^ tmp;
- data += 2;
- hash += hash >> 11;
+
+ return hasher.hash();
}
- // Force "avalanching" of final 127 bits.
- hash ^= hash << 3;
- hash += hash >> 5;
- hash ^= hash << 2;
- hash += hash >> 15;
- hash ^= hash << 10;
+ template<typename T> static inline unsigned createHash(const T* data, unsigned length)
+ {
+ return createHash<T, defaultCoverter>(data, length);
+ }
- hash &= 0x7fffffff;
+ template<typename T> static inline unsigned createHash(const T* data)
+ {
+ return createHash<T, defaultCoverter>(data);
+ }
- // This avoids ever returning a hash code of 0, since that is used to
- // signal "hash not computed yet", using a value that is likely to be
- // effectively the same as 0 when the low bits are masked.
- if (hash == 0)
- hash = 0x40000000;
+private:
+ static inline UChar defaultCoverter(UChar ch)
+ {
+ return ch;
+ }
- return hash;
+ static inline UChar defaultCoverter(char ch)
+ {
+ return static_cast<unsigned char>(ch);
+ }
+
+ inline void addCharactersToHash(UChar a, UChar b)
+ {
+ m_hash += a;
+ unsigned tmp = (b << 11) ^ m_hash;
+ m_hash = (m_hash << 16) ^ tmp;
+ m_hash += m_hash >> 11;
+ }
+
+ unsigned m_hash;
+ UChar m_cachedCharacter;
+
+ static const UChar invalidCharacterValue = 0xfffe;
+};
+
+
+
+inline unsigned stringHash(const UChar* data, unsigned length)
+{
+ return StringHasher::createHash<UChar>(data, length);
+}
+
+inline unsigned stringHash(const char* data, unsigned length)
+{
+ return StringHasher::createHash<char>(data, length);
+}
+
+inline unsigned stringHash(const char* data)
+{
+ return StringHasher::createHash<char>(data);
}
} // namespace WTF
--
WebKit Debian packaging
More information about the Pkg-webkit-commits
mailing list