StringMap.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //===--- StringMap.cpp - String Hash table map implementation -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the StringMap class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/ADT/StringMap.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. #include "llvm/Support/Compiler.h"
  16. #include <cassert>
  17. using namespace llvm;
  18. StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) {
  19. ItemSize = itemSize;
  20. // If a size is specified, initialize the table with that many buckets.
  21. if (InitSize) {
  22. init(InitSize);
  23. return;
  24. }
  25. // Otherwise, initialize it with zero buckets to avoid the allocation.
  26. TheTable = nullptr;
  27. NumBuckets = 0;
  28. NumItems = 0;
  29. NumTombstones = 0;
  30. }
  31. void StringMapImpl::init(unsigned InitSize) {
  32. assert((InitSize & (InitSize-1)) == 0 &&
  33. "Init Size must be a power of 2 or zero!");
  34. NumBuckets = InitSize ? InitSize : 16;
  35. NumItems = 0;
  36. NumTombstones = 0;
  37. // HLSL Change Begin: Use overridable operator new
  38. unsigned NewNumBuckets = NumBuckets;
  39. NumBuckets = 0; // If allocation fails, we should have zero buckets.
  40. size_t TableSize = (NewNumBuckets + 1) * (sizeof(StringMapEntryBase *) + sizeof(unsigned));
  41. TheTable = (StringMapEntryBase **)::operator new(TableSize);
  42. std::memset(TheTable, 0, TableSize);
  43. NumBuckets = NewNumBuckets;
  44. // HLSL Change End
  45. // Allocate one extra bucket, set it to look filled so the iterators stop at
  46. // end.
  47. TheTable[NumBuckets] = (StringMapEntryBase*)2;
  48. }
  49. /// LookupBucketFor - Look up the bucket that the specified string should end
  50. /// up in. If it already exists as a key in the map, the Item pointer for the
  51. /// specified bucket will be non-null. Otherwise, it will be null. In either
  52. /// case, the FullHashValue field of the bucket will be set to the hash value
  53. /// of the string.
  54. unsigned StringMapImpl::LookupBucketFor(StringRef Name) {
  55. unsigned HTSize = NumBuckets;
  56. if (HTSize == 0) { // Hash table unallocated so far?
  57. init(16);
  58. HTSize = NumBuckets;
  59. }
  60. unsigned FullHashValue = HashString(Name);
  61. unsigned BucketNo = FullHashValue & (HTSize-1);
  62. unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
  63. unsigned ProbeAmt = 1;
  64. int FirstTombstone = -1;
  65. while (1) {
  66. StringMapEntryBase *BucketItem = TheTable[BucketNo];
  67. // If we found an empty bucket, this key isn't in the table yet, return it.
  68. if (LLVM_LIKELY(!BucketItem)) {
  69. // If we found a tombstone, we want to reuse the tombstone instead of an
  70. // empty bucket. This reduces probing.
  71. if (FirstTombstone != -1) {
  72. HashTable[FirstTombstone] = FullHashValue;
  73. return FirstTombstone;
  74. }
  75. HashTable[BucketNo] = FullHashValue;
  76. return BucketNo;
  77. }
  78. if (BucketItem == getTombstoneVal()) {
  79. // Skip over tombstones. However, remember the first one we see.
  80. if (FirstTombstone == -1) FirstTombstone = BucketNo;
  81. } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
  82. // If the full hash value matches, check deeply for a match. The common
  83. // case here is that we are only looking at the buckets (for item info
  84. // being non-null and for the full hash value) not at the items. This
  85. // is important for cache locality.
  86. // Do the comparison like this because Name isn't necessarily
  87. // null-terminated!
  88. char *ItemStr = (char*)BucketItem+ItemSize;
  89. if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) {
  90. // We found a match!
  91. return BucketNo;
  92. }
  93. }
  94. // Okay, we didn't find the item. Probe to the next bucket.
  95. BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
  96. // Use quadratic probing, it has fewer clumping artifacts than linear
  97. // probing and has good cache behavior in the common case.
  98. ++ProbeAmt;
  99. }
  100. }
  101. /// FindKey - Look up the bucket that contains the specified key. If it exists
  102. /// in the map, return the bucket number of the key. Otherwise return -1.
  103. /// This does not modify the map.
  104. int StringMapImpl::FindKey(StringRef Key) const {
  105. unsigned HTSize = NumBuckets;
  106. if (HTSize == 0) return -1; // Really empty table?
  107. unsigned FullHashValue = HashString(Key);
  108. unsigned BucketNo = FullHashValue & (HTSize-1);
  109. unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
  110. unsigned ProbeAmt = 1;
  111. while (1) {
  112. StringMapEntryBase *BucketItem = TheTable[BucketNo];
  113. // If we found an empty bucket, this key isn't in the table yet, return.
  114. if (LLVM_LIKELY(!BucketItem))
  115. return -1;
  116. if (BucketItem == getTombstoneVal()) {
  117. // Ignore tombstones.
  118. } else if (LLVM_LIKELY(HashTable[BucketNo] == FullHashValue)) {
  119. // If the full hash value matches, check deeply for a match. The common
  120. // case here is that we are only looking at the buckets (for item info
  121. // being non-null and for the full hash value) not at the items. This
  122. // is important for cache locality.
  123. // Do the comparison like this because NameStart isn't necessarily
  124. // null-terminated!
  125. char *ItemStr = (char*)BucketItem+ItemSize;
  126. if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) {
  127. // We found a match!
  128. return BucketNo;
  129. }
  130. }
  131. // Okay, we didn't find the item. Probe to the next bucket.
  132. BucketNo = (BucketNo+ProbeAmt) & (HTSize-1);
  133. // Use quadratic probing, it has fewer clumping artifacts than linear
  134. // probing and has good cache behavior in the common case.
  135. ++ProbeAmt;
  136. }
  137. }
  138. /// RemoveKey - Remove the specified StringMapEntry from the table, but do not
  139. /// delete it. This aborts if the value isn't in the table.
  140. void StringMapImpl::RemoveKey(StringMapEntryBase *V) {
  141. const char *VStr = (char*)V + ItemSize;
  142. StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength()));
  143. (void)V2;
  144. assert(V == V2 && "Didn't find key?");
  145. }
  146. /// RemoveKey - Remove the StringMapEntry for the specified key from the
  147. /// table, returning it. If the key is not in the table, this returns null.
  148. StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) {
  149. int Bucket = FindKey(Key);
  150. if (Bucket == -1) return nullptr;
  151. StringMapEntryBase *Result = TheTable[Bucket];
  152. TheTable[Bucket] = getTombstoneVal();
  153. --NumItems;
  154. ++NumTombstones;
  155. assert(NumItems + NumTombstones <= NumBuckets);
  156. return Result;
  157. }
  158. /// RehashTable - Grow the table, redistributing values into the buckets with
  159. /// the appropriate mod-of-hashtable-size.
  160. unsigned StringMapImpl::RehashTable(unsigned BucketNo) {
  161. unsigned NewSize;
  162. unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
  163. // If the hash table is now more than 3/4 full, or if fewer than 1/8 of
  164. // the buckets are empty (meaning that many are filled with tombstones),
  165. // grow/rehash the table.
  166. if (LLVM_UNLIKELY(NumItems * 4 > NumBuckets * 3)) {
  167. NewSize = NumBuckets*2;
  168. } else if (LLVM_UNLIKELY(NumBuckets - (NumItems + NumTombstones) <=
  169. NumBuckets / 8)) {
  170. NewSize = NumBuckets;
  171. } else {
  172. return BucketNo;
  173. }
  174. unsigned NewBucketNo = BucketNo;
  175. // Allocate one extra bucket which will always be non-empty. This allows the
  176. // iterators to stop at end.
  177. // HLSL Change Begin: Use overridable operator new
  178. size_t NewTableSize = (NewSize + 1) * (sizeof(StringMapEntryBase*) + sizeof(unsigned));
  179. StringMapEntryBase **NewTableArray = (StringMapEntryBase **)::operator new(NewTableSize);
  180. std::memset(NewTableArray, 0, NewTableSize);
  181. // HLSL Change End
  182. unsigned *NewHashArray = (unsigned *)(NewTableArray + NewSize + 1);
  183. NewTableArray[NewSize] = (StringMapEntryBase*)2;
  184. // Rehash all the items into their new buckets. Luckily :) we already have
  185. // the hash values available, so we don't have to rehash any strings.
  186. for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
  187. StringMapEntryBase *Bucket = TheTable[I];
  188. if (Bucket && Bucket != getTombstoneVal()) {
  189. // Fast case, bucket available.
  190. unsigned FullHash = HashTable[I];
  191. unsigned NewBucket = FullHash & (NewSize-1);
  192. if (!NewTableArray[NewBucket]) {
  193. NewTableArray[FullHash & (NewSize-1)] = Bucket;
  194. NewHashArray[FullHash & (NewSize-1)] = FullHash;
  195. if (I == BucketNo)
  196. NewBucketNo = NewBucket;
  197. continue;
  198. }
  199. // Otherwise probe for a spot.
  200. unsigned ProbeSize = 1;
  201. do {
  202. NewBucket = (NewBucket + ProbeSize++) & (NewSize-1);
  203. } while (NewTableArray[NewBucket]);
  204. // Finally found a slot. Fill it in.
  205. NewTableArray[NewBucket] = Bucket;
  206. NewHashArray[NewBucket] = FullHash;
  207. if (I == BucketNo)
  208. NewBucketNo = NewBucket;
  209. }
  210. }
  211. ::operator delete(TheTable); // HLSL Change: Use overridable operator delete
  212. TheTable = NewTableArray;
  213. NumBuckets = NewSize;
  214. NumTombstones = 0;
  215. return NewBucketNo;
  216. }