1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
// IWYU pragma: private, include "WAVM/Inline/HashTable.h"
// You should only include this file indirectly by including HashMap.h.
// Use these macros to compress the boilerplate template declarations in a non-inline member
// function definition for HashTable.
#define HASHTABLE_PARAMETERS \
typename Key, typename Element, typename HashTablePolicy, typename AllocPolicy
#define HASHTABLE_ARGUMENTS Key, Element, HashTablePolicy, AllocPolicy
template<HASHTABLE_PARAMETERS>
Uptr HashTable<HASHTABLE_ARGUMENTS>::calcProbeCount(Uptr bucketIndex) const
{
WAVM_ASSERT(buckets[bucketIndex].hashAndOccupancy);
WAVM_ASSERT(!(hashToBucketIndexMask & Bucket::isOccupiedMask));
const Uptr idealBucketIndex = buckets[bucketIndex].hashAndOccupancy & hashToBucketIndexMask;
if(idealBucketIndex <= bucketIndex) { return bucketIndex - idealBucketIndex; }
else
{
return numBuckets() - idealBucketIndex + bucketIndex;
}
}
template<HASHTABLE_PARAMETERS> void HashTable<HASHTABLE_ARGUMENTS>::clear()
{
destruct();
buckets = nullptr;
numElements = 0;
hashToBucketIndexMask = UINTPTR_MAX;
}
template<HASHTABLE_PARAMETERS> void HashTable<HASHTABLE_ARGUMENTS>::resize(Uptr newNumBuckets)
{
WAVM_ASSERT(!(newNumBuckets & (newNumBuckets - 1)));
const Uptr oldNumBuckets = numBuckets();
Bucket* oldBuckets = buckets;
if(!newNumBuckets)
{
WAVM_ASSERT(!numElements);
buckets = nullptr;
}
else
{
// Allocate the new buckets.
buckets = new Bucket[newNumBuckets]();
}
hashToBucketIndexMask = newNumBuckets - 1;
if(numElements)
{
WAVM_ASSERT(oldBuckets);
WAVM_ASSERT(buckets);
// Iterate over the old buckets, and reinsert their contents in the new buckets.
for(Uptr bucketIndex = 0; bucketIndex < oldNumBuckets; ++bucketIndex)
{
if(oldBuckets[bucketIndex].hashAndOccupancy)
{
Bucket& oldBucket = oldBuckets[bucketIndex];
const Key& oldKey = HashTablePolicy::getKey(oldBucket.storage.get());
// Find the new bucket to write the element to.
Bucket& newBucket = getBucketForWrite(oldBucket.hashAndOccupancy, oldKey);
// Move the element from the old bucket to the new.
newBucket.storage.construct(std::move(oldBucket.storage.get()));
newBucket.hashAndOccupancy = oldBucket.hashAndOccupancy;
oldBucket.storage.destruct();
oldBucket.hashAndOccupancy = 0;
}
}
// Free the old buckets.
delete[] oldBuckets;
}
}
template<HASHTABLE_PARAMETERS>
bool HashTable<HASHTABLE_ARGUMENTS>::remove(Uptr hash, const Key& key)
{
// Find the bucket (if any) holding the key.
const Uptr hashAndOccupancy = hash | Bucket::isOccupiedMask;
const HashTableBucket<Element>* bucket = getBucketForRead(hashAndOccupancy, key);
if(!bucket) { return false; }
else
{
// Remove the element in the bucket.
eraseHashBucket(bucket - getBuckets());
// Decrease the number of elements and resize the table if the occupancy is too low.
--numElements;
const Uptr maxDesiredBuckets = AllocPolicy::getMaxDesiredBuckets(numElements);
if(numBuckets() > maxDesiredBuckets) { resize(maxDesiredBuckets); }
return true;
}
}
template<HASHTABLE_PARAMETERS>
const HashTableBucket<Element>* HashTable<HASHTABLE_ARGUMENTS>::getBucketForRead(
Uptr hash,
const Key& key) const
{
if(!buckets) { return nullptr; }
// Start at the bucket indexed by the lower bits of the hash.
const Uptr hashAndOccupancy = hash | Bucket::isOccupiedMask;
Uptr probeCount = 0;
while(true)
{
const Uptr bucketIndex = (hashAndOccupancy + probeCount) & hashToBucketIndexMask;
const Bucket& bucket = buckets[bucketIndex];
if(!bucket.hashAndOccupancy)
{
// If the bucket is empty, return null.
return nullptr;
}
else if(bucket.hashAndOccupancy == hashAndOccupancy
&& HashTablePolicy::areKeysEqual(
HashTablePolicy::getKey(buckets[bucketIndex].storage.get()), key))
{
// If the bucket holds the specified key, return null.
return &bucket;
}
else
{
const Uptr bucketProbeCount = calcProbeCount(bucketIndex);
if(probeCount > bucketProbeCount)
{
// If the bucket holds a key whose ideal bucket follows the specified key's ideal
// bucket, then the following buckets cannot hold the key, so return null.
return nullptr;
}
else
{
// Otherwise, continue to the next bucket.
++probeCount;
WAVM_ASSERT(probeCount < numBuckets());
}
}
};
}
template<HASHTABLE_PARAMETERS>
HashTableBucket<Element>* HashTable<HASHTABLE_ARGUMENTS>::getBucketForModify(Uptr hash,
const Key& key)
{
return const_cast<Bucket*>(getBucketForRead(hash, key));
}
template<HASHTABLE_PARAMETERS>
HashTableBucket<Element>& HashTable<HASHTABLE_ARGUMENTS>::getBucketForAdd(Uptr hash, const Key& key)
{
// Make sure there's enough space to add a new key to the table.
const Uptr minDesiredBuckets = AllocPolicy::getMinDesiredBuckets(numElements + 1);
if(numBuckets() < minDesiredBuckets) { resize(minDesiredBuckets); }
// Find the bucket to write the new key to.
Bucket& bucket = getBucketForWrite(hash, key);
// If the bucket is empty, increment the number of elements in the table.
// The caller is expected to fill the bucket once this function returns.
if(!bucket.hashAndOccupancy) { ++numElements; }
else
{
WAVM_ASSERT(bucket.hashAndOccupancy == (hash | Bucket::isOccupiedMask));
}
return bucket;
}
template<HASHTABLE_PARAMETERS>
HashTableBucket<Element>& HashTable<HASHTABLE_ARGUMENTS>::getBucketForWrite(Uptr hash,
const Key& key)
{
WAVM_ASSERT(buckets);
// Start at the bucket indexed by the lower bits of the hash.
const Uptr hashAndOccupancy = hash | Bucket::isOccupiedMask;
Uptr probeCount = 0;
while(true)
{
const Uptr bucketIndex = (hashAndOccupancy + probeCount) & hashToBucketIndexMask;
Bucket& bucket = buckets[bucketIndex];
if(!bucket.hashAndOccupancy)
{
// If the bucket is empty, return it.
return bucket;
}
else if(bucket.hashAndOccupancy == hashAndOccupancy
&& HashTablePolicy::areKeysEqual(HashTablePolicy::getKey(bucket.storage.get()),
key))
{
// If the bucket already holds the specified key, return it.
return bucket;
}
else
{
const Uptr bucketProbeCount = calcProbeCount(bucketIndex);
if(probeCount > bucketProbeCount)
{
// If the bucket holds an element with a lower probe count (i.e. its ideal
// bucket is after this key's ideal bucket), then evict the element and return
// this bucket.
evictHashBucket(bucketIndex);
return bucket;
}
else
{
// Otherwise, continue to the next bucket.
++probeCount;
WAVM_ASSERT(probeCount < numBuckets());
}
}
};
}
template<HASHTABLE_PARAMETERS>
void HashTable<HASHTABLE_ARGUMENTS>::evictHashBucket(Uptr bucketIndex)
{
WAVM_ASSERT(buckets);
// Move the bucket's element into a local variable and empty the bucket.
Bucket& evictedBucket = buckets[bucketIndex];
WAVM_ASSERT(evictedBucket.hashAndOccupancy);
Element evictedElement{std::move(evictedBucket.storage.get())};
Uptr evictedHashAndOccupancy = evictedBucket.hashAndOccupancy;
evictedBucket.hashAndOccupancy = 0;
evictedBucket.storage.destruct();
while(true)
{
// Consider the next bucket
bucketIndex = (bucketIndex + 1) & hashToBucketIndexMask;
Bucket& bucket = buckets[bucketIndex];
if(!bucket.hashAndOccupancy)
{
// If the bucket is empty, then fill it with the evicted element and return.
bucket.storage.construct(std::move(evictedElement));
bucket.hashAndOccupancy = evictedHashAndOccupancy;
return;
}
else
{
// Otherwise, swap the evicted element with the bucket's element and continue
// searching for an empty bucket.
std::swap(evictedElement, bucket.storage.get());
std::swap(evictedHashAndOccupancy, bucket.hashAndOccupancy);
}
};
}
template<HASHTABLE_PARAMETERS>
void HashTable<HASHTABLE_ARGUMENTS>::eraseHashBucket(Uptr eraseBucketIndex)
{
WAVM_ASSERT(buckets);
while(true)
{
// Consider the bucket following the erase bucket
Bucket& bucketToErase = buckets[eraseBucketIndex];
const Uptr bucketIndex = (eraseBucketIndex + 1) & hashToBucketIndexMask;
Bucket& bucket = buckets[bucketIndex];
if(!bucket.hashAndOccupancy)
{
// If the following bucket is empty, empty the erase bucket and return.
WAVM_ASSERT(bucketToErase.hashAndOccupancy);
bucketToErase.hashAndOccupancy = 0;
bucketToErase.storage.destruct();
return;
}
else
{
const Uptr bucketProbeCount = calcProbeCount(bucketIndex);
if(bucketProbeCount == 0)
{
// If the bucket contains an element in its ideal bucket, it and its successors
// don't need to be shifted into the erase bucket, so just empty the erase
// bucket and return.
WAVM_ASSERT(bucketToErase.hashAndOccupancy);
bucketToErase.hashAndOccupancy = 0;
bucketToErase.storage.destruct();
return;
}
else
{
// Otherwise, shift the contents of the following bucket into the erase bucket
// and continue with the following bucket as the new erase bucket.
bucketToErase.storage.get() = std::move(bucket.storage.get());
bucketToErase.hashAndOccupancy = bucket.hashAndOccupancy;
eraseBucketIndex = bucketIndex;
}
}
};
}
template<HASHTABLE_PARAMETERS>
void HashTable<HASHTABLE_ARGUMENTS>::analyzeSpaceUsage(Uptr& outTotalMemoryBytes,
Uptr& outMaxProbeCount,
F32& outOccupancy,
F32& outAverageProbeCount) const
{
outTotalMemoryBytes = sizeof(Bucket) * numBuckets() + sizeof(*this);
outOccupancy = size() / F32(numBuckets());
outMaxProbeCount = 0;
outAverageProbeCount = 0.0f;
for(Uptr idealBucketIndex = 0; idealBucketIndex < numBuckets(); ++idealBucketIndex)
{
Uptr probeCount = 0;
while(true)
{
const Uptr bucketIndex = (idealBucketIndex + probeCount) & hashToBucketIndexMask;
if(!buckets[bucketIndex].hashAndOccupancy || probeCount > calcProbeCount(bucketIndex))
{ break; }
++probeCount;
};
outMaxProbeCount = probeCount > outMaxProbeCount ? probeCount : outMaxProbeCount;
outAverageProbeCount += probeCount / F32(numBuckets());
}
}
template<HASHTABLE_PARAMETERS>
HashTable<HASHTABLE_ARGUMENTS>::HashTable(Uptr estimatedNumElements)
: buckets(nullptr), numElements(0), hashToBucketIndexMask(UINTPTR_MAX)
{
const Uptr numBuckets = AllocPolicy::getMinDesiredBuckets(estimatedNumElements);
if(numBuckets)
{
// Allocate the initial buckets.
hashToBucketIndexMask = numBuckets - 1;
WAVM_ASSERT((numBuckets & hashToBucketIndexMask) == 0);
buckets = new Bucket[numBuckets]();
}
}
template<HASHTABLE_PARAMETERS> HashTable<HASHTABLE_ARGUMENTS>::HashTable(const HashTable& copy)
{
copyFrom(copy);
}
template<HASHTABLE_PARAMETERS> HashTable<HASHTABLE_ARGUMENTS>::HashTable(HashTable&& movee) noexcept
{
moveFrom(std::move(movee));
}
template<HASHTABLE_PARAMETERS> HashTable<HASHTABLE_ARGUMENTS>::~HashTable() { destruct(); }
template<HASHTABLE_PARAMETERS>
HashTable<HASHTABLE_ARGUMENTS>& HashTable<HASHTABLE_ARGUMENTS>::operator=(
const HashTable<HASHTABLE_ARGUMENTS>& copyee)
{
// Do nothing if copying from this.
if(this != ©ee)
{
destruct();
copyFrom(copyee);
}
return *this;
}
template<HASHTABLE_PARAMETERS>
HashTable<HASHTABLE_ARGUMENTS>& HashTable<HASHTABLE_ARGUMENTS>::operator=(
HashTable<HASHTABLE_ARGUMENTS>&& movee) noexcept
{
// Do nothing if moving from this.
if(this != &movee)
{
destruct();
moveFrom(std::move(movee));
}
return *this;
}
template<HASHTABLE_PARAMETERS> void HashTable<HASHTABLE_ARGUMENTS>::destruct()
{
if(buckets)
{
for(Uptr bucketIndex = 0; bucketIndex < numBuckets(); ++bucketIndex)
{
if(buckets[bucketIndex].hashAndOccupancy) { buckets[bucketIndex].storage.destruct(); }
}
delete[] buckets;
buckets = nullptr;
}
}
template<HASHTABLE_PARAMETERS> void HashTable<HASHTABLE_ARGUMENTS>::copyFrom(const HashTable& copy)
{
numElements = copy.numElements;
hashToBucketIndexMask = copy.hashToBucketIndexMask;
if(!copy.buckets) { buckets = nullptr; }
else
{
buckets = new Bucket[copy.numBuckets()];
for(Uptr bucketIndex = 0; bucketIndex < numBuckets(); ++bucketIndex)
{
buckets[bucketIndex].hashAndOccupancy = copy.buckets[bucketIndex].hashAndOccupancy;
if(buckets[bucketIndex].hashAndOccupancy)
{ buckets[bucketIndex].storage.construct(copy.buckets[bucketIndex].storage.get()); }
}
}
}
template<HASHTABLE_PARAMETERS>
void HashTable<HASHTABLE_ARGUMENTS>::moveFrom(HashTable&& movee) noexcept
{
numElements = movee.numElements;
hashToBucketIndexMask = movee.hashToBucketIndexMask;
if(!movee.buckets) { buckets = nullptr; }
else
{
buckets = movee.buckets;
movee.numElements = 0;
movee.hashToBucketIndexMask = 0;
movee.buckets = nullptr;
}
}
#undef HASHTABLE_PARAMETERS
#undef HASHTABLE_ARGUMENTS
|