/// Returns the number of buckets to allocate to ensure that the DenseMap can
/// accommodate \p NumEntries without need to grow().
-static unsigned getMinBucketToReserveForEntries(unsigned NumEntries) {
+static inline unsigned getMinBucketToReserveForEntries(unsigned NumEntries) {
// Ensure that "NumEntries * 4 < NumBuckets * 3"
if (NumEntries == 0)
return 0;
return NextPowerOf2(NumEntries * 4 / 3 + 1);
}
+static inline StringMapEntryBase **createTable(unsigned NewNumBuckets) {
+ auto **Table = static_cast<StringMapEntryBase **>(safe_calloc(
+ NewNumBuckets + 1, sizeof(StringMapEntryBase **) + sizeof(unsigned)));
+
+ // Allocate one extra bucket, set it to look filled so the iterators stop at
+ // end.
+ Table[NewNumBuckets] = (StringMapEntryBase *)2;
+ return Table;
+}
+
+static inline unsigned *getHashTable(StringMapEntryBase **TheTable,
+ unsigned NumBuckets) {
+ return reinterpret_cast<unsigned *>(TheTable + NumBuckets + 1);
+}
+
StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) {
ItemSize = itemSize;
NumItems = 0;
NumTombstones = 0;
- TheTable = static_cast<StringMapEntryBase **>(safe_calloc(
- NewNumBuckets + 1, sizeof(StringMapEntryBase **) + sizeof(unsigned)));
+ TheTable = createTable(NewNumBuckets);
// Set the member only if TheTable was successfully allocated
NumBuckets = NewNumBuckets;
-
- // Allocate one extra bucket, set it to look filled so the iterators stop at
- // end.
- TheTable[NumBuckets] = (StringMapEntryBase *)2;
}
/// LookupBucketFor - Look up the bucket that the specified string should end
/// case, the FullHashValue field of the bucket will be set to the hash value
/// of the string.
unsigned StringMapImpl::LookupBucketFor(StringRef Name) {
- unsigned HTSize = NumBuckets;
- if (HTSize == 0) { // Hash table unallocated so far?
+ // Hash table unallocated so far?
+ if (NumBuckets == 0)
init(16);
- HTSize = NumBuckets;
- }
unsigned FullHashValue = djbHash(Name, 0);
- unsigned BucketNo = FullHashValue & (HTSize - 1);
- unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
+ unsigned BucketNo = FullHashValue & (NumBuckets - 1);
+ unsigned *HashTable = getHashTable(TheTable, NumBuckets);
unsigned ProbeAmt = 1;
int FirstTombstone = -1;
}
// Okay, we didn't find the item. Probe to the next bucket.
- BucketNo = (BucketNo + ProbeAmt) & (HTSize - 1);
+ BucketNo = (BucketNo + ProbeAmt) & (NumBuckets - 1);
// Use quadratic probing, it has fewer clumping artifacts than linear
// probing and has good cache behavior in the common case.
/// in the map, return the bucket number of the key. Otherwise return -1.
/// This does not modify the map.
int StringMapImpl::FindKey(StringRef Key) const {
- unsigned HTSize = NumBuckets;
- if (HTSize == 0)
+ if (NumBuckets == 0)
return -1; // Really empty table?
unsigned FullHashValue = djbHash(Key, 0);
- unsigned BucketNo = FullHashValue & (HTSize - 1);
- unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
+ unsigned BucketNo = FullHashValue & (NumBuckets - 1);
+ unsigned *HashTable = getHashTable(TheTable, NumBuckets);
unsigned ProbeAmt = 1;
while (true) {
}
// Okay, we didn't find the item. Probe to the next bucket.
- BucketNo = (BucketNo + ProbeAmt) & (HTSize - 1);
+ BucketNo = (BucketNo + ProbeAmt) & (NumBuckets - 1);
// Use quadratic probing, it has fewer clumping artifacts than linear
// probing and has good cache behavior in the common case.
/// the appropriate mod-of-hashtable-size.
unsigned StringMapImpl::RehashTable(unsigned BucketNo) {
unsigned NewSize;
- unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
-
// If the hash table is now more than 3/4 full, or if fewer than 1/8 of
// the buckets are empty (meaning that many are filled with tombstones),
// grow/rehash the table.
}
unsigned NewBucketNo = BucketNo;
- // Allocate one extra bucket which will always be non-empty. This allows the
- // iterators to stop at end.
- auto NewTableArray = static_cast<StringMapEntryBase **>(safe_calloc(
- NewSize + 1, sizeof(StringMapEntryBase *) + sizeof(unsigned)));
-
- unsigned *NewHashArray = (unsigned *)(NewTableArray + NewSize + 1);
- NewTableArray[NewSize] = (StringMapEntryBase *)2;
+ auto **NewTableArray = createTable(NewSize);
+ unsigned *NewHashArray = getHashTable(NewTableArray, NewSize);
+ unsigned *HashTable = getHashTable(TheTable, NumBuckets);
// Rehash all the items into their new buckets. Luckily :) we already have
// the hash values available, so we don't have to rehash any strings.