Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(187)

Side by Side Diff: Source/wtf/HashFunctions.h

Issue 17583004: Improve WTF::HashTable performance by changing probing method (Closed) Base URL: https://chromium.googlesource.com/chromium/blink@master
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Source/wtf/HashTable.h » ('j') | Source/wtf/HashTable.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved.
3 * 3 *
4 * This library is free software; you can redistribute it and/or 4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public 5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either 6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version. 7 * version 2 of the License, or (at your option) any later version.
8 * 8 *
9 * This library is distributed in the hope that it will be useful, 9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 14 matching lines...) Expand all
25 #include <stdint.h> 25 #include <stdint.h>
26 26
27 namespace WTF { 27 namespace WTF {
28 28
29 template<size_t size> struct IntTypes; 29 template<size_t size> struct IntTypes;
30 template<> struct IntTypes<1> { typedef int8_t SignedType; typedef uint8_t U nsignedType; }; 30 template<> struct IntTypes<1> { typedef int8_t SignedType; typedef uint8_t U nsignedType; };
31 template<> struct IntTypes<2> { typedef int16_t SignedType; typedef uint16_t UnsignedType; }; 31 template<> struct IntTypes<2> { typedef int16_t SignedType; typedef uint16_t UnsignedType; };
32 template<> struct IntTypes<4> { typedef int32_t SignedType; typedef uint32_t UnsignedType; }; 32 template<> struct IntTypes<4> { typedef int32_t SignedType; typedef uint32_t UnsignedType; };
33 template<> struct IntTypes<8> { typedef int64_t SignedType; typedef uint64_t UnsignedType; }; 33 template<> struct IntTypes<8> { typedef int64_t SignedType; typedef uint64_t UnsignedType; };
34 34
35 // integer hash function
36
37 // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/intha sh.htm
38 inline unsigned intHash(uint8_t key8) 35 inline unsigned intHash(uint8_t key8)
39 { 36 {
40 unsigned key = key8; 37 return key8;
41 key += ~(key << 15);
42 key ^= (key >> 10);
43 key += (key << 3);
44 key ^= (key >> 6);
45 key += ~(key << 11);
46 key ^= (key >> 16);
47 return key;
48 } 38 }
49 39
50 // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/intha sh.htm
51 inline unsigned intHash(uint16_t key16) 40 inline unsigned intHash(uint16_t key16)
52 { 41 {
53 unsigned key = key16; 42 return key16;
54 key += ~(key << 15);
55 key ^= (key >> 10);
56 key += (key << 3);
57 key ^= (key >> 6);
58 key += ~(key << 11);
59 key ^= (key >> 16);
60 return key;
61 } 43 }
62 44
63 // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/intha sh.htm
64 inline unsigned intHash(uint32_t key) 45 inline unsigned intHash(uint32_t key)
65 { 46 {
66 key += ~(key << 15); 47 return key ^ (key >> 16);
67 key ^= (key >> 10);
68 key += (key << 3);
69 key ^= (key >> 6);
70 key += ~(key << 11);
71 key ^= (key >> 16);
72 return key;
73 } 48 }
74 49
75 // Thomas Wang's 64 bit Mix Function: http://www.cris.com/~Ttwang/tech/intha sh.htm
76 inline unsigned intHash(uint64_t key) 50 inline unsigned intHash(uint64_t key)
77 { 51 {
78 key += ~(key << 32); 52 key ^= key >> 32;
79 key ^= (key >> 22); 53 key ^= key >> 16;
80 key += ~(key << 13);
81 key ^= (key >> 8);
82 key += (key << 3);
83 key ^= (key >> 15);
84 key += ~(key << 27);
85 key ^= (key >> 31);
86 return static_cast<unsigned>(key); 54 return static_cast<unsigned>(key);
87 } 55 }
88 56
89 // Compound integer hash method: http://opendatastructures.org/versions/edit ion-0.1d/ods-java/node33.html#SECTION00832000000000000000 57 // Compound integer hash method: http://opendatastructures.org/versions/edit ion-0.1d/ods-java/node33.html#SECTION00832000000000000000
90 inline unsigned pairIntHash(unsigned key1, unsigned key2) 58 inline unsigned pairIntHash(unsigned key1, unsigned key2)
91 { 59 {
92 unsigned shortRandom1 = 277951225; // A random 32-bit value. 60 unsigned shortRandom1 = 277951225; // A random 32-bit value.
93 unsigned shortRandom2 = 95187966; // A random 32-bit value. 61 unsigned shortRandom2 = 95187966; // A random 32-bit value.
94 uint64_t longRandom = 19248658165952622LL; // A random 64-bit value. 62 uint64_t longRandom = 19248658165952622LL; // A random 64-bit value.
95 63
(...skipping 23 matching lines...) Expand all
119 87
120 // pointer identity hash function 88 // pointer identity hash function
121 89
122 template<typename T> struct PtrHash { 90 template<typename T> struct PtrHash {
123 static unsigned hash(T key) 91 static unsigned hash(T key)
124 { 92 {
125 #if COMPILER(MSVC) 93 #if COMPILER(MSVC)
126 #pragma warning(push) 94 #pragma warning(push)
127 #pragma warning(disable: 4244) // work around what seems to be a bug in MSVC's c onversion warnings 95 #pragma warning(disable: 4244) // work around what seems to be a bug in MSVC's c onversion warnings
128 #endif 96 #endif
129 return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key)); 97 uintptr_t keyIntPtr = reinterpret_cast<uintptr_t>(key);
98 keyIntPtr ^= keyIntPtr >> 6;
99 return IntHash<uintptr_t>::hash(keyIntPtr);
130 #if COMPILER(MSVC) 100 #if COMPILER(MSVC)
131 #pragma warning(pop) 101 #pragma warning(pop)
132 #endif 102 #endif
133 } 103 }
134 static bool equal(T a, T b) { return a == b; } 104 static bool equal(T a, T b) { return a == b; }
135 static const bool safeToCompareToEmptyOrDeleted = true; 105 static const bool safeToCompareToEmptyOrDeleted = true;
136 }; 106 };
137 template<typename P> struct PtrHash<RefPtr<P> > : PtrHash<P*> { 107 template<typename P> struct PtrHash<RefPtr<P> > : PtrHash<P*> {
138 using PtrHash<P*>::hash; 108 using PtrHash<P*>::hash;
139 static unsigned hash(const RefPtr<P>& key) { return hash(key.get()); } 109 static unsigned hash(const RefPtr<P>& key) { return hash(key.get()); }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 182
213 template<typename T, typename U> struct DefaultHash<std::pair<T, U> > { type def PairHash<T, U> Hash; }; 183 template<typename T, typename U> struct DefaultHash<std::pair<T, U> > { type def PairHash<T, U> Hash; };
214 184
215 } // namespace WTF 185 } // namespace WTF
216 186
217 using WTF::DefaultHash; 187 using WTF::DefaultHash;
218 using WTF::IntHash; 188 using WTF::IntHash;
219 using WTF::PtrHash; 189 using WTF::PtrHash;
220 190
221 #endif // WTF_HashFunctions_h 191 #endif // WTF_HashFunctions_h
OLDNEW
« no previous file with comments | « no previous file | Source/wtf/HashTable.h » ('j') | Source/wtf/HashTable.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698