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