llvm.org GIT mirror llvm / 6235ea2
1. Make table size prime -- improves hashing performance vastly. 2. Reduce #hashes from 2 to 1 for initial inserts. 3. Fix incorrect assertion from last checkin. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@7417 91177308-0d34-0410-b5e6-96231b3b80d8 Vikram S. Adve 17 years ago
1 changed file(s) with 46 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
4545 const FULLEMPTY EMPTY = '\0';
4646 const FULLEMPTY FULL = '\1';
4747
48 // List of primes closest to powers of 2 in [2^20 -- 2^30], obtained from
49 // http://www.utm.edu/research/primes/lists/2small/0bit.html.
50 // Use these as the successive sizes of the hash table.
51 #define NUMPRIMES 11
52 #define FIRSTENTRY 2
53 const uint PRIMES[NUMPRIMES] = { (1<<20)-3, (1<<21)-9, (1<<22)-3, (1<<23)-15,
54 (1<<24)-3, (1<<25)-39, (1<<26)-5, (1<<27)-39,
55 (1<<28)-57, (1<<29)-3, (1<<30)-35 };
56 uint CurrentSizeEntry = FIRSTENTRY;
57
4858 const uint MAX_NUM_PROBES = 4;
4959
5060 typedef struct PtrValueHashEntry_struct {
6070 } PtrValueHashTable;
6171
6272
63 extern Generic LookupOrInsertPtr(PtrValueHashTable* ptrTable,
64 void* ptr, ACTION action);
65
66 extern void Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value);
67
68 extern void Delete(PtrValueHashTable* ptrTable, void* ptr);
69
70 /* Returns NULL if the item is not found. */
73 static Generic LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr,
74 ACTION action, Generic value);
75
76 static void Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value);
77
78 static void Delete(PtrValueHashTable* ptrTable, void* ptr);
79
80 /* Returns 0 if the item is not found. */
7181 /* void* LookupPtr(PtrValueHashTable* ptrTable, void* ptr) */
7282 #define LookupPtr(ptrTable, ptr) \
73 LookupOrInsertPtr(ptrTable, ptr, FIND)
83 LookupOrInsertPtr(ptrTable, ptr, FIND, (Generic) 0)
7484
7585 void
7686 InitializeTable(PtrValueHashTable* ptrTable, Index newSize)
119129
120130 #ifndef NDEBUG
121131 for (i=0; i < oldCapacity; ++i)
122 if (! oldFlags[i])
132 if (oldFlags[i] == FULL)
123133 assert(LookupPtr(ptrTable, oldTable[i].key) == oldTable[i].value);
124134 #endif
125135
150160 DeleteAtIndex(PtrValueHashTable* ptrTable, Index index)
151161 {
152162 assert(ptrTable->fullEmptyFlags[index] == FULL && "Deleting empty slot!");
153 ptrTable->table[index].key = NULL;
154 ptrTable->table[index].value = (Generic) NULL;
163 ptrTable->table[index].key = 0;
164 ptrTable->table[index].value = (Generic) 0;
155165 ptrTable->fullEmptyFlags[index] = EMPTY;
156166 ptrTable->size--;
157167 }
184194
185195 if (numProbes == MAX_NUM_PROBES)
186196 { /* table is too full: reallocate and search again */
187 ReallocTable(ptrTable, 1 + 2*ptrTable->capacity);
197 if (CurrentSizeEntry >= NUMPRIMES-1) {
198 fprintf(stderr, "Out of PRIME Numbers!!!");
199 abort();
200 }
201 ReallocTable(ptrTable, PRIMES[++CurrentSizeEntry]);
188202 return FindIndex(ptrTable, ptr);
189203 }
190204 else
195209 }
196210 }
197211
212 /* Look up hash table using 'ptr' as the key. If an entry exists, return
213 * the value mapped to 'ptr'. If not, and if action==ENTER is specified,
214 * create a new entry with value 'value', but return 0 in any case.
215 */
198216 Generic
199 LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr, ACTION action)
217 LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr, ACTION action,
218 Generic value)
200219 {
201220 Index index = FindIndex(ptrTable, ptr);
202221 if (ptrTable->fullEmptyFlags[index] == FULL &&
204223 return ptrTable->table[index].value;
205224
206225 /* Lookup failed: item is not in the table */
207 if (action != ENTER)
208 return (Generic) NULL;
209
210 /* Insert item into the table and return its index */
211 InsertAtIndex(ptrTable, ptr, (Generic) NULL, index);
212 return (Generic) NULL;
226 /* If action is ENTER, insert item into the table. Return 0 in any case. */
227 if (action == ENTER)
228 InsertAtIndex(ptrTable, ptr, value, index);
229
230 return (Generic) 0;
213231 }
214232
215233 void
237255 *===---------------------------------------------------------------------===*/
238256
239257 PtrValueHashTable* SequenceNumberTable = NULL;
240 Index INITIAL_SIZE = 1 << 22;
258 #define INITIAL_SIZE (PRIMES[FIRSTENTRY])
241259
242260 #define MAX_NUM_SAVED 1024
243261
258276 assert(MAX_NUM_PROBES < INITIAL_SIZE+1 && "Initial size too small");
259277 SequenceNumberTable = CreateTable(INITIAL_SIZE);
260278 }
261 seqnum = (SequenceNumber) LookupPtr(SequenceNumberTable, ptr);
262 if (seqnum == 0)
263 {
264 Insert(SequenceNumberTable, ptr, ++count);
265 seqnum = count;
266 }
279 seqnum = (SequenceNumber)
280 LookupOrInsertPtr(SequenceNumberTable, ptr, ENTER, count+1);
281
282 if (seqnum == 0) /* new entry was created with value count+1 */
283 seqnum = ++count; /* so increment counter */
284
285 assert(seqnum <= count && "Invalid sequence number in table!");
267286 return seqnum;
268287 }
269288