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