blob: c2fc261df3a66a3d771b7a8b9588aa9109f5b62f [file] [log] [blame]
Chris Lattnerbb28a812007-02-08 19:20:57 +00001//===--- StringMap.cpp - String Hash table map implementation -------------===//
Chris Lattner23d7b362006-10-29 23:42:03 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner23d7b362006-10-29 23:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerbb28a812007-02-08 19:20:57 +000010// This file implements the StringMap class.
Chris Lattner23d7b362006-10-29 23:42:03 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbb28a812007-02-08 19:20:57 +000014#include "llvm/ADT/StringMap.h"
Daniel Dunbar4dee7fd2009-10-17 18:21:06 +000015#include "llvm/ADT/StringExtras.h"
Chris Lattner23d7b362006-10-29 23:42:03 +000016#include <cassert>
17using namespace llvm;
18
Chris Lattnerbb28a812007-02-08 19:20:57 +000019StringMapImpl::StringMapImpl(unsigned InitSize, unsigned itemSize) {
Chris Lattner794a0142007-04-04 00:29:37 +000020 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
35void StringMapImpl::init(unsigned InitSize) {
Chris Lattner23d7b362006-10-29 23:42:03 +000036 assert((InitSize & (InitSize-1)) == 0 &&
37 "Init Size must be a power of 2 or zero!");
Chris Lattneref4c9162007-04-03 22:15:38 +000038 NumBuckets = InitSize ? InitSize : 16;
Chris Lattner23d7b362006-10-29 23:42:03 +000039 NumItems = 0;
Chris Lattner44dcd012007-02-11 20:58:00 +000040 NumTombstones = 0;
Chris Lattner23d7b362006-10-29 23:42:03 +000041
Benjamin Kramerc894b022011-12-27 20:35:07 +000042 TheTable = (StringMapEntryBase **)calloc(NumBuckets+1,
43 sizeof(StringMapEntryBase **) +
44 sizeof(unsigned));
45
Chris Lattnera86559e2007-02-11 08:20:35 +000046 // Allocate one extra bucket, set it to look filled so the iterators stop at
47 // end.
Benjamin Kramerc894b022011-12-27 20:35:07 +000048 TheTable[NumBuckets] = (StringMapEntryBase*)2;
Chris Lattner23d7b362006-10-29 23:42:03 +000049}
50
51
Chris Lattner23d7b362006-10-29 23:42:03 +000052/// 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 Dunbar2928c832009-11-06 10:58:06 +000057unsigned StringMapImpl::LookupBucketFor(StringRef Name) {
Chris Lattner23d7b362006-10-29 23:42:03 +000058 unsigned HTSize = NumBuckets;
Chris Lattner794a0142007-04-04 00:29:37 +000059 if (HTSize == 0) { // Hash table unallocated so far?
60 init(16);
61 HTSize = NumBuckets;
62 }
Daniel Dunbar4dee7fd2009-10-17 18:21:06 +000063 unsigned FullHashValue = HashString(Name);
Chris Lattner23d7b362006-10-29 23:42:03 +000064 unsigned BucketNo = FullHashValue & (HTSize-1);
Benjamin Kramerc894b022011-12-27 20:35:07 +000065 unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
66
Chris Lattner23d7b362006-10-29 23:42:03 +000067 unsigned ProbeAmt = 1;
Chris Lattner44dcd012007-02-11 20:58:00 +000068 int FirstTombstone = -1;
Chris Lattner23d7b362006-10-29 23:42:03 +000069 while (1) {
Benjamin Kramerc894b022011-12-27 20:35:07 +000070 StringMapEntryBase *BucketItem = TheTable[BucketNo];
Chris Lattner23d7b362006-10-29 23:42:03 +000071 // If we found an empty bucket, this key isn't in the table yet, return it.
72 if (BucketItem == 0) {
Chris Lattner44dcd012007-02-11 20:58:00 +000073 // 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 Kramerc894b022011-12-27 20:35:07 +000076 HashTable[FirstTombstone] = FullHashValue;
Chris Lattner44dcd012007-02-11 20:58:00 +000077 return FirstTombstone;
78 }
79
Benjamin Kramerc894b022011-12-27 20:35:07 +000080 HashTable[BucketNo] = FullHashValue;
Chris Lattner23d7b362006-10-29 23:42:03 +000081 return BucketNo;
82 }
83
Chris Lattner44dcd012007-02-11 20:58:00 +000084 if (BucketItem == getTombstoneVal()) {
85 // Skip over tombstones. However, remember the first one we see.
86 if (FirstTombstone == -1) FirstTombstone = BucketNo;
Benjamin Kramerc894b022011-12-27 20:35:07 +000087 } else if (HashTable[BucketNo] == FullHashValue) {
Chris Lattner44dcd012007-02-11 20:58:00 +000088 // 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 Dunbar6316fbc2009-07-23 18:17:34 +000093 // Do the comparison like this because Name isn't necessarily
Chris Lattner23d7b362006-10-29 23:42:03 +000094 // null-terminated!
95 char *ItemStr = (char*)BucketItem+ItemSize;
Daniel Dunbar6316fbc2009-07-23 18:17:34 +000096 if (Name == StringRef(ItemStr, BucketItem->getKeyLength())) {
Chris Lattner23d7b362006-10-29 23:42:03 +000097 // 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 Lattnerb5bb9f52007-02-11 19:49:41 +0000111
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 Dunbar2928c832009-11-06 10:58:06 +0000115int StringMapImpl::FindKey(StringRef Key) const {
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000116 unsigned HTSize = NumBuckets;
Chris Lattner794a0142007-04-04 00:29:37 +0000117 if (HTSize == 0) return -1; // Really empty table?
Daniel Dunbar4dee7fd2009-10-17 18:21:06 +0000118 unsigned FullHashValue = HashString(Key);
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000119 unsigned BucketNo = FullHashValue & (HTSize-1);
Benjamin Kramerc894b022011-12-27 20:35:07 +0000120 unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
121
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000122 unsigned ProbeAmt = 1;
123 while (1) {
Benjamin Kramerc894b022011-12-27 20:35:07 +0000124 StringMapEntryBase *BucketItem = TheTable[BucketNo];
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000125 // If we found an empty bucket, this key isn't in the table yet, return.
126 if (BucketItem == 0)
127 return -1;
128
Chris Lattner44dcd012007-02-11 20:58:00 +0000129 if (BucketItem == getTombstoneVal()) {
130 // Ignore tombstones.
Benjamin Kramerc894b022011-12-27 20:35:07 +0000131 } else if (HashTable[BucketNo] == FullHashValue) {
Chris Lattner44dcd012007-02-11 20:58:00 +0000132 // 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 Lattnerb5bb9f52007-02-11 19:49:41 +0000137 // Do the comparison like this because NameStart isn't necessarily
138 // null-terminated!
139 char *ItemStr = (char*)BucketItem+ItemSize;
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000140 if (Key == StringRef(ItemStr, BucketItem->getKeyLength())) {
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000141 // 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 Lattner44dcd012007-02-11 20:58:00 +0000155/// RemoveKey - Remove the specified StringMapEntry from the table, but do not
156/// delete it. This aborts if the value isn't in the table.
157void StringMapImpl::RemoveKey(StringMapEntryBase *V) {
158 const char *VStr = (char*)V + ItemSize;
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000159 StringMapEntryBase *V2 = RemoveKey(StringRef(VStr, V->getKeyLength()));
Jeffrey Yasskin8e68c382010-12-23 00:58:24 +0000160 (void)V2;
Chris Lattner44dcd012007-02-11 20:58:00 +0000161 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 Dunbar2928c832009-11-06 10:58:06 +0000166StringMapEntryBase *StringMapImpl::RemoveKey(StringRef Key) {
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000167 int Bucket = FindKey(Key);
Chris Lattner44dcd012007-02-11 20:58:00 +0000168 if (Bucket == -1) return 0;
169
Benjamin Kramerc894b022011-12-27 20:35:07 +0000170 StringMapEntryBase *Result = TheTable[Bucket];
171 TheTable[Bucket] = getTombstoneVal();
Chris Lattner44dcd012007-02-11 20:58:00 +0000172 --NumItems;
173 ++NumTombstones;
Jakob Stoklund Olesen03ef4492011-03-30 18:32:51 +0000174 assert(NumItems + NumTombstones <= NumBuckets);
175
Chris Lattner44dcd012007-02-11 20:58:00 +0000176 return Result;
177}
178
179
Chris Lattnerb5bb9f52007-02-11 19:49:41 +0000180
Chris Lattner23d7b362006-10-29 23:42:03 +0000181/// RehashTable - Grow the table, redistributing values into the buckets with
182/// the appropriate mod-of-hashtable-size.
Chris Lattnerbb28a812007-02-08 19:20:57 +0000183void StringMapImpl::RehashTable() {
Jakob Stoklund Olesenaea4fe22011-03-30 18:32:44 +0000184 unsigned NewSize;
Benjamin Kramerc894b022011-12-27 20:35:07 +0000185 unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1);
Jakob Stoklund Olesenaea4fe22011-03-30 18:32:44 +0000186
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 Carruth2a791162012-06-19 17:40:35 +0000192 } else if (NumBuckets-(NumItems+NumTombstones) <= NumBuckets/8) {
Jakob Stoklund Olesenaea4fe22011-03-30 18:32:44 +0000193 NewSize = NumBuckets;
194 } else {
195 return;
196 }
197
Chris Lattnera86559e2007-02-11 08:20:35 +0000198 // Allocate one extra bucket which will always be non-empty. This allows the
199 // iterators to stop at end.
Benjamin Kramerc894b022011-12-27 20:35:07 +0000200 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 Lattner23d7b362006-10-29 23:42:03 +0000206 // 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 Kramerc894b022011-12-27 20:35:07 +0000208 for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
209 StringMapEntryBase *Bucket = TheTable[I];
210 if (Bucket && Bucket != getTombstoneVal()) {
Chris Lattner23d7b362006-10-29 23:42:03 +0000211 // Fast case, bucket available.
Benjamin Kramerc894b022011-12-27 20:35:07 +0000212 unsigned FullHash = HashTable[I];
Chris Lattner23d7b362006-10-29 23:42:03 +0000213 unsigned NewBucket = FullHash & (NewSize-1);
Benjamin Kramerc894b022011-12-27 20:35:07 +0000214 if (NewTableArray[NewBucket] == 0) {
215 NewTableArray[FullHash & (NewSize-1)] = Bucket;
216 NewHashArray[FullHash & (NewSize-1)] = FullHash;
Chris Lattner23d7b362006-10-29 23:42:03 +0000217 continue;
218 }
219
Chris Lattner44dcd012007-02-11 20:58:00 +0000220 // Otherwise probe for a spot.
Chris Lattner23d7b362006-10-29 23:42:03 +0000221 unsigned ProbeSize = 1;
222 do {
223 NewBucket = (NewBucket + ProbeSize++) & (NewSize-1);
Benjamin Kramerc894b022011-12-27 20:35:07 +0000224 } while (NewTableArray[NewBucket]);
Chris Lattner23d7b362006-10-29 23:42:03 +0000225
226 // Finally found a slot. Fill it in.
Benjamin Kramerc894b022011-12-27 20:35:07 +0000227 NewTableArray[NewBucket] = Bucket;
228 NewHashArray[NewBucket] = FullHash;
Chris Lattner23d7b362006-10-29 23:42:03 +0000229 }
230 }
231
Chris Lattner12ba8062007-04-04 17:24:28 +0000232 free(TheTable);
Chris Lattner23d7b362006-10-29 23:42:03 +0000233
234 TheTable = NewTableArray;
235 NumBuckets = NewSize;
Jakob Stoklund Olesen03ef4492011-03-30 18:32:51 +0000236 NumTombstones = 0;
Chris Lattner23d7b362006-10-29 23:42:03 +0000237}