Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: runtime/lib/compact_hash.dart

Issue 3001763002: [VM-corelib] Improve performance for very big maps and sets.
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | runtime/vm/flow_graph_inliner.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import 'dart:typed_data' show Uint32List; 5 import 'dart:typed_data' show Uint32List;
6 import 'dart:_internal' as internal; 6 import 'dart:_internal' as internal;
7 7
8 // Hash table with open addressing that separates the index from keys/values. 8 // Hash table with open addressing that separates the index from keys/values.
9 9
10 abstract class _HashFieldBase { 10 abstract class _HashFieldBase {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 // are doubled when _data is full. Thus, _index will have a max load factor 64 // are doubled when _data is full. Thus, _index will have a max load factor
65 // of 1/2, which enables one more bit to be used for the hash. 65 // of 1/2, which enables one more bit to be used for the hash.
66 // TODO(koda): Consider growing _data by factor sqrt(2), twice as often. 66 // TODO(koda): Consider growing _data by factor sqrt(2), twice as often.
67 static const int _INITIAL_INDEX_BITS = 3; 67 static const int _INITIAL_INDEX_BITS = 3;
68 static const int _INITIAL_INDEX_SIZE = 1 << (_INITIAL_INDEX_BITS + 1); 68 static const int _INITIAL_INDEX_SIZE = 1 << (_INITIAL_INDEX_BITS + 1);
69 69
70 // Unused and deleted entries are marked by 0 and 1, respectively. 70 // Unused and deleted entries are marked by 0 and 1, respectively.
71 static const int _UNUSED_PAIR = 0; 71 static const int _UNUSED_PAIR = 0;
72 static const int _DELETED_PAIR = 1; 72 static const int _DELETED_PAIR = 1;
73 73
74 static const int _MAX_LINEAR_DATA = 4096;
75 static const int _MAX_LINEAR_DATA_LOG_2 = 12;
76
77 // For sizes up to _MAX_LINEAR_DATA the size of the _data array is just the
78 // size we ask for. Above that size we add enough elements onto the _data
79 // array to hold _MAX_LINEAR_DATA-sized sub-arrays for the rest of the
80 // entries.
sra1 2017/08/17 18:33:17 It might be better not to have the hybrid scheme w
81 static int _sizeToBaseListSize(int size) {
82 if (size <= _MAX_LINEAR_DATA) return size;
83 // Round up.
84 size = ((size - 1) | (_MAX_LINEAR_DATA - 1)) + 1;
85 // First few entries are in the linear area.
86 size -= _MAX_LINEAR_DATA;
87 // Enough entries for the sub-arrays.
88 size >>= _MAX_LINEAR_DATA_LOG_2;
89 return _MAX_LINEAR_DATA + size;
90 }
91
92 static int _baseListSizeToSize(int baseListSize) {
93 if (baseListSize <= _MAX_LINEAR_DATA) return baseListSize;
94 baseListSize -= _MAX_LINEAR_DATA;
95 baseListSize <<= _MAX_LINEAR_DATA_LOG_2;
96 return baseListSize + _MAX_LINEAR_DATA;
97 }
98
99 static List _indexToList(List base, int index) {
100 if (index < _MAX_LINEAR_DATA) return base;
101 index >>= _MAX_LINEAR_DATA_LOG_2;
102 return base[_MAX_LINEAR_DATA - 1 + index];
103 }
104
105 static List _setSublist(List base, int index, List sublist) {
106 assert(index >= _MAX_LINEAR_DATA);
107 index -= _MAX_LINEAR_DATA;
108 index >>= _MAX_LINEAR_DATA_LOG_2;
109 base[_MAX_LINEAR_DATA + index] = sublist;
110 }
111
74 // On 32-bit, the top bits are wasted to avoid Mint allocation. 112 // On 32-bit, the top bits are wasted to avoid Mint allocation.
75 // TODO(koda): Reclaim the bits by making the compiler treat hash patterns 113 // TODO(koda): Reclaim the bits by making the compiler treat hash patterns
76 // as unsigned words. 114 // as unsigned words.
77 static int _indexSizeToHashMask(int indexSize) { 115 static int _indexSizeToHashMask(int indexSize) {
78 int indexBits = indexSize.bitLength - 2; 116 int indexBits = indexSize.bitLength - 2;
79 return internal.is64Bit 117 return internal.is64Bit
80 ? (1 << (32 - indexBits)) - 1 118 ? (1 << (32 - indexBits)) - 1
81 : (1 << (30 - indexBits)) - 1; 119 : (1 << (30 - indexBits)) - 1;
82 } 120 }
83 121
84 static int _hashPattern(int fullHash, int hashMask, int size) { 122 static int _hashPattern(int fullHash, int hashMask, int size) {
85 final int maskedHash = fullHash & hashMask; 123 final int maskedHash = fullHash & hashMask;
86 // TODO(koda): Consider keeping bit length and use left shift. 124 // TODO(koda): Consider keeping bit length and use left shift.
87 return (maskedHash == 0) ? (size >> 1) : maskedHash * (size >> 1); 125 return (maskedHash == 0) ? (size >> 1) : maskedHash * (size >> 1);
88 } 126 }
89 127
90 // Linear probing. 128 // Linear probing.
91 static int _firstProbe(int fullHash, int sizeMask) { 129 static int _firstProbe(int fullHash, int sizeMask) {
92 final int i = fullHash & sizeMask; 130 final int i = fullHash & sizeMask;
93 // Light, fast shuffle to mitigate bad hashCode (e.g., sequential). 131 // Light, fast shuffle to mitigate bad hashCode (e.g., sequential).
94 return ((i << 1) + i) & sizeMask; 132 return ((i << 1) + i) & sizeMask;
95 } 133 }
96 134
97 static int _nextProbe(int i, int sizeMask) => (i + 1) & sizeMask; 135 static int _nextProbe(int i, int sizeMask) => (i + 1) & sizeMask;
98 136
99 // A self-loop is used to mark a deleted key or value. 137 // A self-loop is used to mark a deleted key or value.
100 static bool _isDeleted(List data, Object keyOrValue) => 138 static bool _isDeleted(List data, Object keyOrValue) =>
101 identical(keyOrValue, data); 139 identical(keyOrValue, data);
102 static void _setDeletedAt(List data, int d) { 140 static void _setDeletedAt(List data, List sublist, int modulus) {
103 data[d] = data; 141 sublist[modulus] = data;
104 } 142 }
105 143
106 // Concurrent modification detection relies on this checksum monotonically 144 // Concurrent modification detection relies on this checksum monotonically
107 // increasing between reallocations of _data. 145 // increasing between reallocations of _data.
108 int get _checkSum => _usedData + _deletedKeys; 146 int get _checkSum => _usedData + _deletedKeys;
109 bool _isModifiedSince(List oldData, int oldCheckSum) => 147 bool _isModifiedSince(List oldData, int oldCheckSum) =>
110 !identical(_data, oldData) || (_checkSum != oldCheckSum); 148 !identical(_data, oldData) || (_checkSum != oldCheckSum);
111 } 149 }
112 150
113 class _OperatorEqualsAndHashCode { 151 class _OperatorEqualsAndHashCode {
(...skipping 25 matching lines...) Expand all
139 177
140 class _LinkedHashMapMixin<K, V> { 178 class _LinkedHashMapMixin<K, V> {
141 int get length => (_usedData >> 1) - _deletedKeys; 179 int get length => (_usedData >> 1) - _deletedKeys;
142 bool get isEmpty => length == 0; 180 bool get isEmpty => length == 0;
143 bool get isNotEmpty => !isEmpty; 181 bool get isNotEmpty => !isEmpty;
144 182
145 void _rehash() { 183 void _rehash() {
146 if ((_deletedKeys << 2) > _usedData) { 184 if ((_deletedKeys << 2) > _usedData) {
147 // TODO(koda): Consider shrinking. 185 // TODO(koda): Consider shrinking.
148 // TODO(koda): Consider in-place compaction and more costly CME check. 186 // TODO(koda): Consider in-place compaction and more costly CME check.
149 _init(_index.length, _hashMask, _data, _usedData); 187 _init(_index.length, _hashMask, _data);
150 } else { 188 } else {
151 // TODO(koda): Support 32->64 bit transition (and adjust _hashMask). 189 // TODO(koda): Support 32->64 bit transition (and adjust _hashMask).
152 _init(_index.length << 1, _hashMask >> 1, _data, _usedData); 190 _init(_index.length << 1, _hashMask >> 1, _data);
153 } 191 }
154 } 192 }
155 193
156 void clear() { 194 void clear() {
157 if (!isEmpty) { 195 if (!isEmpty) {
158 // Use _data.length, since _index might be null. 196 int size = _HashBase._INITIAL_INDEX_SIZE;
159 _init(_data.length, _hashMask, null, 0); 197 _init(size, _HashBase._indexSizeToHashMask(size), null);
160 } 198 }
161 } 199 }
162 200
163 // Allocate new _index and _data, and optionally copy existing contents. 201 // Allocate new _index and _data, and optionally copy existing contents.
164 void _init(int size, int hashMask, List oldData, int oldUsed) { 202 void _init(int size, int hashMask, List oldData) {
165 assert(size & (size - 1) == 0); 203 assert(size & (size - 1) == 0);
166 assert(_HashBase._UNUSED_PAIR == 0); 204 assert(_HashBase._UNUSED_PAIR == 0);
167 _index = new Uint32List(size); 205 _index = new Uint32List(size);
168 _hashMask = hashMask; 206 _hashMask = hashMask;
169 _data = new List(size); 207 if (_deletedKeys == 0 && _data == oldData) {
208 _rebuildIndex(size, oldData);
209 return;
210 }
211 _data = new List(_HashBase._sizeToBaseListSize(size));
212 int oldUsed = _usedData;
170 _usedData = 0; 213 _usedData = 0;
171 _deletedKeys = 0; 214 _deletedKeys = 0;
172 if (oldData != null) { 215 if (oldData != null) {
173 for (int i = 0; i < oldUsed; i += 2) { 216 for (int i = 0; i < oldUsed; i += 2) {
174 var key = oldData[i]; 217 List sublist = _HashBase._indexToList(oldData, i);
218 int modulus = i & (_HashBase._MAX_LINEAR_DATA - 1);
219 var key = sublist[modulus];
175 if (!_HashBase._isDeleted(oldData, key)) { 220 if (!_HashBase._isDeleted(oldData, key)) {
176 // TODO(koda): While there are enough hash bits, avoid hashCode calls. 221 // TODO(koda): While there are enough hash bits, avoid hashCode calls.
177 this[key] = oldData[i + 1]; 222 this[key] = sublist[modulus + 1];
178 } 223 }
179 } 224 }
180 } 225 }
181 } 226 }
182 227
228 void _rebuildIndex(int size, List oldData) {
229 int dataSize = _HashBase._sizeToBaseListSize(size);
230 if (_data.length != dataSize) {
231 _data = new List(dataSize);
232 for (int i = 0; i < oldData.length; i++) {
233 _data[i] = oldData[i];
234 }
235 }
236 int i = 0;
237 int notYetAdded = _usedData;
238 _usedData = 0;
239 for (; i < notYetAdded && i < _HashBase._MAX_LINEAR_DATA; i += 2) {
240 _setAlreadyThere(oldData[i]);
241 }
242 for (; i < oldData.length; i++) {
243 notYetAdded -= _HashBase._MAX_LINEAR_DATA;
244 List sublist = oldData[i];
245 for (int j = 0; j < notYetAdded && j < sublist.length; j += 2) {
246 _setAlreadyThere(sublist[j]);
247 }
248 }
249 }
250
183 int _getIndexLength() { 251 int _getIndexLength() {
184 return (_index == null) ? _regenerateIndex() : _index.length; 252 return (_index == null) ? _regenerateIndex() : _index.length;
185 } 253 }
186 254
187 int _regenerateIndex() { 255 int _regenerateIndex() {
188 assert(_index == null); 256 assert(_index == null);
189 _index = new Uint32List(_data.length); 257 _index = new Uint32List(_HashBase._baseListSizeToSize(_data.length));
190 assert(_hashMask == 0); 258 assert(_hashMask == 0);
191 _hashMask = _HashBase._indexSizeToHashMask(_index.length); 259 _hashMask = _HashBase._indexSizeToHashMask(_index.length);
192 final int tmpUsed = _usedData; 260 _rebuildIndex(_data.length, _data);
193 _usedData = 0;
194 for (int i = 0; i < tmpUsed; i += 2) {
195 // TODO(koda): Avoid redundant equality tests and stores into _data.
196 this[_data[i]] = _data[i + 1];
197 }
198 return _index.length; 261 return _index.length;
199 } 262 }
200 263
201 void _insert(K key, V value, int hashPattern, int i) { 264 void _insert(K key, V value, int hashPattern, int i) {
202 if (_usedData == _data.length) { 265 if (_usedData == _getIndexLength()) {
203 _rehash(); 266 _rehash();
204 this[key] = value; 267 this[key] = value;
205 } else { 268 } else {
206 assert(1 <= hashPattern && hashPattern < (1 << 32)); 269 assert(1 <= hashPattern && hashPattern < (1 << 32));
207 final int index = _usedData >> 1; 270 final int index = _usedData >> 1;
208 assert((index & hashPattern) == 0); 271 assert((index & hashPattern) == 0);
209 _index[i] = hashPattern | index; 272 _index[i] = hashPattern | index;
210 _data[_usedData++] = key; 273 List sublist = _HashBase._indexToList(_data, _usedData);
211 _data[_usedData++] = value; 274 if (sublist == null) {
275 sublist = new List(_HashBase._MAX_LINEAR_DATA);
276 _HashBase._setSublist(_data, _usedData, sublist);
277 }
278 int modulus = _usedData & (_HashBase._MAX_LINEAR_DATA - 1);
279 sublist[modulus] = key;
280 sublist[modulus + 1] = value;
281 _usedData += 2;
212 } 282 }
213 } 283 }
214 284
215 // If key is present, returns the index of the value in _data, else returns 285 // If key is present, returns the index of the value in _data, else returns
216 // the negated insertion point in _index. 286 // the negated insertion point in _index.
217 int _findValueOrInsertPoint(K key, int fullHash, int hashPattern, int size) { 287 int _findValueOrInsertPoint(K key, int fullHash, int hashPattern, int size) {
218 final int sizeMask = size - 1; 288 final int sizeMask = size - 1;
219 final int maxEntries = size >> 1; 289 final int maxEntries = size >> 1;
220 int i = _HashBase._firstProbe(fullHash, sizeMask); 290 int i = _HashBase._firstProbe(fullHash, sizeMask);
221 int firstDeleted = -1; 291 int firstDeleted = -1;
222 int pair = _index[i]; 292 int pair = _index[i];
223 while (pair != _HashBase._UNUSED_PAIR) { 293 while (pair != _HashBase._UNUSED_PAIR) {
224 if (pair == _HashBase._DELETED_PAIR) { 294 if (pair == _HashBase._DELETED_PAIR) {
225 if (firstDeleted < 0) { 295 if (firstDeleted < 0) {
226 firstDeleted = i; 296 firstDeleted = i;
227 } 297 }
228 } else { 298 } else {
229 final int entry = hashPattern ^ pair; 299 final int entry = hashPattern ^ pair;
230 if (entry < maxEntries) { 300 if (entry < maxEntries) {
231 final int d = entry << 1; 301 final int d = entry << 1;
232 if (_equals(key, _data[d])) { 302 List sublist = _HashBase._indexToList(_data, d);
233 return d + 1; 303 if (sublist != null) {
304 int modulus = d & (_HashBase._MAX_LINEAR_DATA - 1);
305 if (_equals(key, sublist[modulus])) {
306 return d + 1;
307 }
234 } 308 }
235 } 309 }
236 } 310 }
237 i = _HashBase._nextProbe(i, sizeMask); 311 i = _HashBase._nextProbe(i, sizeMask);
238 pair = _index[i]; 312 pair = _index[i];
239 } 313 }
240 return firstDeleted >= 0 ? -firstDeleted : -i; 314 return firstDeleted >= 0 ? -firstDeleted : -i;
241 } 315 }
242 316
317 // Adds a key to the index where the (key, value) are already in the data.
318 void _setAlreadyThere(K key) {
319 final int size = _getIndexLength();
320 final int sizeMask = size - 1;
321 final int fullHash = _hashCode(key);
322 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size);
323
324 final int maxEntries = size >> 1;
325 int i = _HashBase._firstProbe(fullHash, sizeMask);
326 int pair = _index[i];
327 while (pair != _HashBase._UNUSED_PAIR) {
328 i = _HashBase._nextProbe(i, sizeMask);
329 pair = _index[i];
330 }
331
332 assert(1 <= hashPattern && hashPattern < (1 << 32));
333 final int index = _usedData >> 1;
334 assert((index & hashPattern) == 0);
335 _index[i] = hashPattern | index;
336 _usedData += 2;
337 }
338
243 void operator []=(K key, V value) { 339 void operator []=(K key, V value) {
244 final int size = _getIndexLength(); 340 final int size = _getIndexLength();
245 final int sizeMask = size - 1; 341 final int sizeMask = size - 1;
246 final int fullHash = _hashCode(key); 342 final int fullHash = _hashCode(key);
247 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); 343 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size);
248 final int d = _findValueOrInsertPoint(key, fullHash, hashPattern, size); 344 final int d = _findValueOrInsertPoint(key, fullHash, hashPattern, size);
249 if (d > 0) { 345 if (d > 0) {
250 _data[d] = value; 346 List sublist = _HashBase._indexToList(_data, d);
347 int modulus = d & (_HashBase._MAX_LINEAR_DATA - 1);
348 sublist[modulus] = value;
251 } else { 349 } else {
252 final int i = -d; 350 final int i = -d;
253 _insert(key, value, hashPattern, i); 351 _insert(key, value, hashPattern, i);
254 } 352 }
255 } 353 }
256 354
257 V putIfAbsent(K key, V ifAbsent()) { 355 V putIfAbsent(K key, V ifAbsent()) {
258 final int size = _getIndexLength(); 356 final int size = _getIndexLength();
259 final int sizeMask = size - 1; 357 final int sizeMask = size - 1;
260 final int maxEntries = size >> 1; 358 final int maxEntries = size >> 1;
261 final int fullHash = _hashCode(key); 359 final int fullHash = _hashCode(key);
262 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); 360 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size);
263 final int d = _findValueOrInsertPoint(key, fullHash, hashPattern, size); 361 final int d = _findValueOrInsertPoint(key, fullHash, hashPattern, size);
264 if (d > 0) { 362 if (d > 0) {
265 return _data[d]; 363 List sublist = _HashBase._indexToList(_data, d);
364 int modulus = d & (_HashBase._MAX_LINEAR_DATA - 1);
365 return sublist[modulus];
266 } 366 }
267 // 'ifAbsent' is allowed to modify the map. 367 // 'ifAbsent' is allowed to modify the map.
268 List oldData = _data; 368 List oldData = _data;
269 int oldCheckSum = _checkSum; 369 int oldCheckSum = _checkSum;
270 V value = ifAbsent(); 370 V value = ifAbsent();
271 if (_isModifiedSince(oldData, oldCheckSum)) { 371 if (_isModifiedSince(oldData, oldCheckSum)) {
272 this[key] = value; 372 this[key] = value;
273 } else { 373 } else {
274 final int i = -d; 374 final int i = -d;
275 _insert(key, value, hashPattern, i); 375 _insert(key, value, hashPattern, i);
276 } 376 }
277 return value; 377 return value;
278 } 378 }
279 379
280 V remove(Object key) { 380 V remove(Object key) {
281 final int size = _getIndexLength(); 381 final int size = _getIndexLength();
282 final int sizeMask = size - 1; 382 final int sizeMask = size - 1;
283 final int maxEntries = size >> 1; 383 final int maxEntries = size >> 1;
284 final int fullHash = _hashCode(key); 384 final int fullHash = _hashCode(key);
285 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); 385 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size);
286 int i = _HashBase._firstProbe(fullHash, sizeMask); 386 int i = _HashBase._firstProbe(fullHash, sizeMask);
287 int pair = _index[i]; 387 int pair = _index[i];
288 while (pair != _HashBase._UNUSED_PAIR) { 388 while (pair != _HashBase._UNUSED_PAIR) {
289 if (pair != _HashBase._DELETED_PAIR) { 389 if (pair != _HashBase._DELETED_PAIR) {
290 final int entry = hashPattern ^ pair; 390 final int entry = hashPattern ^ pair;
291 if (entry < maxEntries) { 391 if (entry < maxEntries) {
292 final int d = entry << 1; 392 final int d = entry << 1;
293 if (_equals(key, _data[d])) { 393 List sublist = _HashBase._indexToList(_data, d);
394 int modulus = d & (_HashBase._MAX_LINEAR_DATA - 1);
395 if (_equals(key, sublist[modulus])) {
294 _index[i] = _HashBase._DELETED_PAIR; 396 _index[i] = _HashBase._DELETED_PAIR;
295 _HashBase._setDeletedAt(_data, d); 397 _HashBase._setDeletedAt(_data, sublist, modulus);
296 V value = _data[d + 1]; 398 V value = sublist[modulus + 1];
297 _HashBase._setDeletedAt(_data, d + 1); 399 _HashBase._setDeletedAt(_data, sublist, modulus + 1);
298 ++_deletedKeys; 400 ++_deletedKeys;
299 return value; 401 return value;
300 } 402 }
301 } 403 }
302 } 404 }
303 i = _HashBase._nextProbe(i, sizeMask); 405 i = _HashBase._nextProbe(i, sizeMask);
304 pair = _index[i]; 406 pair = _index[i];
305 } 407 }
306 return null; 408 return null;
307 } 409 }
308 410
309 // If key is absent, return _data (which is never a value). 411 // If key is absent, return _data (which is never a value).
310 Object _getValueOrData(Object key) { 412 Object _getValueOrData(Object key) {
311 final int size = _getIndexLength(); 413 final int size = _getIndexLength();
312 final int sizeMask = size - 1; 414 final int sizeMask = size - 1;
313 final int maxEntries = size >> 1; 415 final int maxEntries = size >> 1;
314 final int fullHash = _hashCode(key); 416 final int fullHash = _hashCode(key);
315 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); 417 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size);
316 int i = _HashBase._firstProbe(fullHash, sizeMask); 418 int i = _HashBase._firstProbe(fullHash, sizeMask);
317 int pair = _index[i]; 419 int pair = _index[i];
318 while (pair != _HashBase._UNUSED_PAIR) { 420 while (pair != _HashBase._UNUSED_PAIR) {
319 if (pair != _HashBase._DELETED_PAIR) { 421 if (pair != _HashBase._DELETED_PAIR) {
320 final int entry = hashPattern ^ pair; 422 final int entry = hashPattern ^ pair;
321 if (entry < maxEntries) { 423 if (entry < maxEntries) {
322 final int d = entry << 1; 424 final int d = entry << 1;
323 if (_equals(key, _data[d])) { 425 List sublist = _HashBase._indexToList(_data, d);
324 return _data[d + 1]; 426 int modulus = d & (_HashBase._MAX_LINEAR_DATA - 1);
427 if (_equals(key, sublist[modulus])) {
428 return sublist[modulus + 1];
325 } 429 }
326 } 430 }
327 } 431 }
328 i = _HashBase._nextProbe(i, sizeMask); 432 i = _HashBase._nextProbe(i, sizeMask);
329 pair = _index[i]; 433 pair = _index[i];
330 } 434 }
331 return _data; 435 return _data;
332 } 436 }
333 437
334 bool containsKey(Object key) => !identical(_data, _getValueOrData(key)); 438 bool containsKey(Object key) => !identical(_data, _getValueOrData(key));
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 _CompactIterator(table, this._data, this._len, this._offset, this._step) 529 _CompactIterator(table, this._data, this._len, this._offset, this._step)
426 : _table = table, 530 : _table = table,
427 _checkSum = table._checkSum; 531 _checkSum = table._checkSum;
428 532
429 bool moveNext() { 533 bool moveNext() {
430 if (_table._isModifiedSince(_data, _checkSum)) { 534 if (_table._isModifiedSince(_data, _checkSum)) {
431 throw new ConcurrentModificationError(_table); 535 throw new ConcurrentModificationError(_table);
432 } 536 }
433 do { 537 do {
434 _offset += _step; 538 _offset += _step;
435 } while (_offset < _len && _HashBase._isDeleted(_data, _data[_offset])); 539 } while (_offset < _len &&
540 _offset < _HashBase._MAX_LINEAR_DATA &&
541 _HashBase._isDeleted(_data, _data[_offset]));
542 if (_offset < _len && _offset >= _HashBase._MAX_LINEAR_DATA) {
543 List sublist = _HashBase._indexToList(_data, _offset);
544 int modulus = _offset & (_HashBase._MAX_LINEAR_DATA - 1);
545 while (_HashBase._isDeleted(_data, sublist[modulus])) {
546 _offset += _step;
547 modulus += _step;
548 if (_offset >= _len) break;
549 if (modulus >= _HashBase._MAX_LINEAR_DATA) {
550 modulus = 0;
551 sublist = _HashBase._indexToList(_data, _offset);
552 }
553 }
554 }
436 if (_offset < _len) { 555 if (_offset < _len) {
437 current = _data[_offset]; 556 List sublist = _HashBase._indexToList(_data, _offset);
557 int modulus = _offset & (_HashBase._MAX_LINEAR_DATA - 1);
558 current = sublist[modulus];
438 return true; 559 return true;
439 } else { 560 } else {
440 current = null; 561 current = null;
sra1 2017/08/17 18:33:17 If the current sublist from _data is held in a fie
441 return false; 562 return false;
442 } 563 }
443 } 564 }
444 } 565 }
445 566
446 // Set implementation, analogous to _CompactLinkedHashMap. 567 // Set implementation, analogous to _CompactLinkedHashMap.
447 class _CompactLinkedHashSet<E> extends _HashFieldBase 568 class _CompactLinkedHashSet<E> extends _HashFieldBase
448 with _HashBase, _OperatorEqualsAndHashCode, SetMixin<E> 569 with _HashBase, _OperatorEqualsAndHashCode, SetMixin<E>
449 implements LinkedHashSet<E> { 570 implements LinkedHashSet<E> {
450 _CompactLinkedHashSet() : super(_HashBase._INITIAL_INDEX_SIZE >> 1) { 571 _CompactLinkedHashSet() : super(_HashBase._INITIAL_INDEX_SIZE >> 1) {
451 assert(_HashBase._UNUSED_PAIR == 0); 572 assert(_HashBase._UNUSED_PAIR == 0);
452 } 573 }
453 574
454 int get length => _usedData - _deletedKeys; 575 int get length => _usedData - _deletedKeys;
455 576
456 void _rehash() { 577 void _rehash() {
457 if ((_deletedKeys << 1) > _usedData) { 578 if ((_deletedKeys << 1) > _usedData) {
458 _init(_index.length, _hashMask, _data, _usedData); 579 _init(_index.length, _hashMask, _data);
459 } else { 580 } else {
460 _init(_index.length << 1, _hashMask >> 1, _data, _usedData); 581 _init(_index.length << 1, _hashMask >> 1, _data);
461 } 582 }
462 } 583 }
463 584
464 void clear() { 585 void clear() {
465 if (!isEmpty) { 586 if (!isEmpty) {
466 _init(_index.length, _hashMask, null, 0); 587 int size = _HashBase._INITIAL_INDEX_SIZE;
588 _init(size, _HashBase._indexSizeToHashMask(size), null);
467 } 589 }
468 } 590 }
469 591
470 void _init(int size, int hashMask, List oldData, int oldUsed) { 592 void _init(int size, int hashMask, List oldData) {
471 _index = new Uint32List(size); 593 _index = new Uint32List(size);
472 _hashMask = hashMask; 594 _hashMask = hashMask;
473 _data = new List(size >> 1); 595 if (_deletedKeys == 0 && _data == oldData) {
596 _rebuildIndex(size, oldData);
597 return;
598 }
599 _data = new List(_HashBase._sizeToBaseListSize(size >> 1));
600 int oldUsed = _usedData;
474 _usedData = 0; 601 _usedData = 0;
475 _deletedKeys = 0; 602 _deletedKeys = 0;
476 if (oldData != null) { 603 if (oldData != null) {
477 for (int i = 0; i < oldUsed; i += 1) { 604 for (int i = 0; i < oldUsed; i += 1) {
478 var key = oldData[i]; 605 List sublist = _HashBase._indexToList(oldData, i);
606 int modulus = i & (_HashBase._MAX_LINEAR_DATA - 1);
607 var key = sublist[modulus];
479 if (!_HashBase._isDeleted(oldData, key)) { 608 if (!_HashBase._isDeleted(oldData, key)) {
480 add(key); 609 add(key);
481 } 610 }
482 } 611 }
483 } 612 }
484 } 613 }
485 614
615 void _rebuildIndex(int size, List oldData) {
616 int dataSize = _HashBase._sizeToBaseListSize(size >> 1);
617 if (_data.length != dataSize) {
618 _data = new List(dataSize);
619 for (int i = 0; i < oldData.length; i++) {
620 _data[i] = oldData[i];
621 }
622 }
623 _usedData = 0;
624 // Unlike in the map case, this Set method is only called when the
625 // data array and sublists are full, so we don't need to keep track of
626 // the number of entries, we can just add everything from the data arrays
627 // to the new index.
628 for (int i = 0; i < oldData.length; i++) {
629 if (i < _HashBase._MAX_LINEAR_DATA) {
630 _addAlreadyThere(oldData[i]);
631 } else {
632 List sublist = oldData[i];
633 for (int j = 0; j < sublist.length; j++) {
634 _addAlreadyThere(sublist[j]);
635 }
636 }
637 }
638 }
639
486 bool add(E key) { 640 bool add(E key) {
487 final int size = _index.length; 641 final int size = _index.length;
488 final int sizeMask = size - 1; 642 final int sizeMask = size - 1;
489 final int maxEntries = size >> 1; 643 final int maxEntries = size >> 1;
490 final int fullHash = _hashCode(key); 644 final int fullHash = _hashCode(key);
491 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); 645 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size);
492 int i = _HashBase._firstProbe(fullHash, sizeMask); 646 int i = _HashBase._firstProbe(fullHash, sizeMask);
493 int firstDeleted = -1; 647 int firstDeleted = -1;
494 int pair = _index[i]; 648 int pair = _index[i];
495 while (pair != _HashBase._UNUSED_PAIR) { 649 while (pair != _HashBase._UNUSED_PAIR) {
496 if (pair == _HashBase._DELETED_PAIR) { 650 if (pair == _HashBase._DELETED_PAIR) {
497 if (firstDeleted < 0) { 651 if (firstDeleted < 0) {
498 firstDeleted = i; 652 firstDeleted = i;
499 } 653 }
500 } else { 654 } else {
501 final int d = hashPattern ^ pair; 655 final int d = hashPattern ^ pair;
502 if (d < maxEntries && _equals(key, _data[d])) { 656 if (d < maxEntries) {
503 return false; 657 List sublist = _HashBase._indexToList(_data, d);
658 if (sublist != null) {
659 int modulus = d & (_HashBase._MAX_LINEAR_DATA - 1);
660 if (_equals(key, sublist[modulus])) {
661 return false;
662 }
663 }
504 } 664 }
505 } 665 }
506 i = _HashBase._nextProbe(i, sizeMask); 666 i = _HashBase._nextProbe(i, sizeMask);
507 pair = _index[i]; 667 pair = _index[i];
508 } 668 }
509 if (_usedData == _data.length) { 669 if (_usedData == maxEntries) {
510 _rehash(); 670 _rehash();
511 add(key); 671 add(key);
512 } else { 672 } else {
513 final int insertionPoint = (firstDeleted >= 0) ? firstDeleted : i; 673 final int insertionPoint = (firstDeleted >= 0) ? firstDeleted : i;
514 assert(1 <= hashPattern && hashPattern < (1 << 32)); 674 assert(1 <= hashPattern && hashPattern < (1 << 32));
515 assert((hashPattern & _usedData) == 0); 675 assert((hashPattern & _usedData) == 0);
516 _index[insertionPoint] = hashPattern | _usedData; 676 _index[insertionPoint] = hashPattern | _usedData;
517 _data[_usedData++] = key; 677 List sublist = _HashBase._indexToList(_data, _usedData);
678 if (sublist == null) {
679 sublist = new List(_HashBase._MAX_LINEAR_DATA);
680 _HashBase._setSublist(_data, _usedData, sublist);
681 }
682 int modulus = _usedData & (_HashBase._MAX_LINEAR_DATA - 1);
683 sublist[modulus] = key;
684 _usedData++;
518 } 685 }
519 return true; 686 return true;
520 } 687 }
521 688
689 // Adds a key to the index which is already in the data.
690 void _addAlreadyThere(E key) {
691 final int size = _index.length;
692 final int sizeMask = size - 1;
693 final int maxEntries = size >> 1;
694 final int fullHash = _hashCode(key);
695 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size);
696 int i = _HashBase._firstProbe(fullHash, sizeMask);
697 int pair = _index[i];
698 while (pair != _HashBase._UNUSED_PAIR) {
699 i = _HashBase._nextProbe(i, sizeMask);
700 pair = _index[i];
701 }
702
703 assert(1 <= hashPattern && hashPattern < (1 << 32));
704 assert((hashPattern & _usedData) == 0);
705 _index[i] = hashPattern | _usedData;
706 _usedData++;
707 }
708
522 // If key is absent, return _data (which is never a value). 709 // If key is absent, return _data (which is never a value).
523 Object _getKeyOrData(Object key) { 710 Object _getKeyOrData(Object key) {
524 final int size = _index.length; 711 final int size = _index.length;
525 final int sizeMask = size - 1; 712 final int sizeMask = size - 1;
526 final int maxEntries = size >> 1; 713 final int maxEntries = size >> 1;
527 final int fullHash = _hashCode(key); 714 final int fullHash = _hashCode(key);
528 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); 715 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size);
529 int i = _HashBase._firstProbe(fullHash, sizeMask); 716 int i = _HashBase._firstProbe(fullHash, sizeMask);
530 int pair = _index[i]; 717 int pair = _index[i];
531 while (pair != _HashBase._UNUSED_PAIR) { 718 while (pair != _HashBase._UNUSED_PAIR) {
532 if (pair != _HashBase._DELETED_PAIR) { 719 if (pair != _HashBase._DELETED_PAIR) {
533 final int d = hashPattern ^ pair; 720 final int d = hashPattern ^ pair;
534 if (d < maxEntries && _equals(key, _data[d])) { 721 if (d < maxEntries) {
535 return _data[d]; // Note: Must return the existing key. 722 List sublist = _HashBase._indexToList(_data, d);
723 if (sublist != null) {
724 int modulus = d & (_HashBase._MAX_LINEAR_DATA - 1);
725 if (_equals(key, sublist[modulus])) {
726 return sublist[modulus]; // Note: Must return the existing key.
727 }
728 }
536 } 729 }
537 } 730 }
538 i = _HashBase._nextProbe(i, sizeMask); 731 i = _HashBase._nextProbe(i, sizeMask);
539 pair = _index[i]; 732 pair = _index[i];
540 } 733 }
541 return _data; 734 return _data;
542 } 735 }
543 736
544 E lookup(Object key) { 737 E lookup(Object key) {
545 var k = _getKeyOrData(key); 738 var k = _getKeyOrData(key);
546 return identical(_data, k) ? null : k; 739 return identical(_data, k) ? null : k;
547 } 740 }
548 741
549 bool contains(Object key) => !identical(_data, _getKeyOrData(key)); 742 bool contains(Object key) => !identical(_data, _getKeyOrData(key));
550 743
551 bool remove(Object key) { 744 bool remove(Object key) {
552 final int size = _index.length; 745 final int size = _index.length;
553 final int sizeMask = size - 1; 746 final int sizeMask = size - 1;
554 final int maxEntries = size >> 1; 747 final int maxEntries = size >> 1;
555 final int fullHash = _hashCode(key); 748 final int fullHash = _hashCode(key);
556 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); 749 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size);
557 int i = _HashBase._firstProbe(fullHash, sizeMask); 750 int i = _HashBase._firstProbe(fullHash, sizeMask);
558 int pair = _index[i]; 751 int pair = _index[i];
559 while (pair != _HashBase._UNUSED_PAIR) { 752 while (pair != _HashBase._UNUSED_PAIR) {
560 if (pair != _HashBase._DELETED_PAIR) { 753 if (pair != _HashBase._DELETED_PAIR) {
561 final int d = hashPattern ^ pair; 754 final int d = hashPattern ^ pair;
562 if (d < maxEntries && _equals(key, _data[d])) { 755 if (d < maxEntries) {
563 _index[i] = _HashBase._DELETED_PAIR; 756 List sublist = _HashBase._indexToList(_data, d);
564 _HashBase._setDeletedAt(_data, d); 757 if (sublist != null) {
565 ++_deletedKeys; 758 int modulus = d & (_HashBase._MAX_LINEAR_DATA - 1);
566 return true; 759 if (_equals(key, sublist[modulus])) {
760 _index[i] = _HashBase._DELETED_PAIR;
761 _HashBase._setDeletedAt(_data, sublist, modulus);
762 ++_deletedKeys;
763 return true;
764 }
765 }
567 } 766 }
568 } 767 }
569 i = _HashBase._nextProbe(i, sizeMask); 768 i = _HashBase._nextProbe(i, sizeMask);
570 pair = _index[i]; 769 pair = _index[i];
571 } 770 }
572 return false; 771 return false;
573 } 772 }
574 773
575 Iterator<E> get iterator => 774 Iterator<E> get iterator =>
576 new _CompactIterator<E>(this, _data, _usedData, -1, 1); 775 new _CompactIterator<E>(this, _data, _usedData, -1, 1);
(...skipping 21 matching lines...) Expand all
598 E lookup(Object o) => _validKey(o) ? super.lookup(o) : null; 797 E lookup(Object o) => _validKey(o) ? super.lookup(o) : null;
599 bool remove(Object o) => _validKey(o) ? super.remove(o) : false; 798 bool remove(Object o) => _validKey(o) ? super.remove(o) : false;
600 799
601 _CompactLinkedCustomHashSet(this._equality, this._hasher, validKey) 800 _CompactLinkedCustomHashSet(this._equality, this._hasher, validKey)
602 : _validKey = (validKey != null) ? validKey : new _TypeTest<E>().test; 801 : _validKey = (validKey != null) ? validKey : new _TypeTest<E>().test;
603 802
604 Set<E> toSet() => 803 Set<E> toSet() =>
605 new _CompactLinkedCustomHashSet<E>(_equality, _hasher, _validKey) 804 new _CompactLinkedCustomHashSet<E>(_equality, _hasher, _validKey)
606 ..addAll(this); 805 ..addAll(this);
607 } 806 }
OLDNEW
« no previous file with comments | « no previous file | runtime/vm/flow_graph_inliner.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698