Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var $getHash; | |
| 6 var $getExistingHash; | |
| 7 | |
| 5 (function(global, utils) { | 8 (function(global, utils) { |
| 6 | |
| 7 "use strict"; | 9 "use strict"; |
| 8 | 10 |
| 9 %CheckIsBootstrapping(); | 11 %CheckIsBootstrapping(); |
| 10 | 12 |
| 11 var GlobalMap = global.Map; | 13 var GlobalMap = global.Map; |
| 12 var GlobalObject = global.Object; | 14 var GlobalObject = global.Object; |
| 13 var GlobalSet = global.Set; | 15 var GlobalSet = global.Set; |
| 16 var IntRandom; | |
| 17 | |
| 18 utils.Import(function(from) { | |
| 19 IntRandom = from.IntRandom; | |
| 20 }); | |
| 14 | 21 |
| 15 // ------------------------------------------------------------------- | 22 // ------------------------------------------------------------------- |
| 16 | 23 |
| 17 function HashToEntry(table, hash, numBuckets) { | 24 function HashToEntry(table, hash, numBuckets) { |
| 18 var bucket = ORDERED_HASH_TABLE_HASH_TO_BUCKET(hash, numBuckets); | 25 var bucket = ORDERED_HASH_TABLE_HASH_TO_BUCKET(hash, numBuckets); |
| 19 return ORDERED_HASH_TABLE_BUCKET_AT(table, bucket); | 26 return ORDERED_HASH_TABLE_BUCKET_AT(table, bucket); |
| 20 } | 27 } |
| 21 %SetForceInlineFlag(HashToEntry); | 28 %SetForceInlineFlag(HashToEntry); |
| 22 | 29 |
| 23 | 30 |
| 24 function SetFindEntry(table, numBuckets, key, hash) { | 31 function SetFindEntry(table, numBuckets, key, hash) { |
| 32 var entry = HashToEntry(table, hash, numBuckets); | |
| 33 if (entry === NOT_FOUND) return entry; | |
| 34 var candidate = ORDERED_HASH_SET_KEY_AT(table, entry, numBuckets); | |
| 35 if (key === candidate) return entry; | |
| 25 var keyIsNaN = $numberIsNaN(key); | 36 var keyIsNaN = $numberIsNaN(key); |
| 26 for (var entry = HashToEntry(table, hash, numBuckets); | 37 while (true) { |
| 27 entry !== NOT_FOUND; | |
| 28 entry = ORDERED_HASH_SET_CHAIN_AT(table, entry, numBuckets)) { | |
| 29 var candidate = ORDERED_HASH_SET_KEY_AT(table, entry, numBuckets); | |
| 30 if (key === candidate) { | |
| 31 return entry; | |
| 32 } | |
| 33 if (keyIsNaN && $numberIsNaN(candidate)) { | 38 if (keyIsNaN && $numberIsNaN(candidate)) { |
| 34 return entry; | 39 return entry; |
| 35 } | 40 } |
| 41 entry = ORDERED_HASH_SET_CHAIN_AT(table, entry, numBuckets); | |
| 42 if (entry === NOT_FOUND) return entry; | |
| 43 candidate = ORDERED_HASH_SET_KEY_AT(table, entry, numBuckets); | |
| 44 if (key === candidate) return entry; | |
| 36 } | 45 } |
| 37 return NOT_FOUND; | 46 return NOT_FOUND; |
| 38 } | 47 } |
| 39 %SetForceInlineFlag(SetFindEntry); | 48 %SetForceInlineFlag(SetFindEntry); |
| 40 | 49 |
| 41 | 50 |
| 42 function MapFindEntry(table, numBuckets, key, hash) { | 51 function MapFindEntry(table, numBuckets, key, hash) { |
| 52 var entry = HashToEntry(table, hash, numBuckets); | |
| 53 if (entry === NOT_FOUND) return entry; | |
| 54 var candidate = ORDERED_HASH_MAP_KEY_AT(table, entry, numBuckets); | |
| 55 if (key === candidate) return entry; | |
| 43 var keyIsNaN = $numberIsNaN(key); | 56 var keyIsNaN = $numberIsNaN(key); |
| 44 for (var entry = HashToEntry(table, hash, numBuckets); | 57 while (true) { |
| 45 entry !== NOT_FOUND; | |
| 46 entry = ORDERED_HASH_MAP_CHAIN_AT(table, entry, numBuckets)) { | |
| 47 var candidate = ORDERED_HASH_MAP_KEY_AT(table, entry, numBuckets); | |
| 48 if (key === candidate) { | |
| 49 return entry; | |
| 50 } | |
| 51 if (keyIsNaN && $numberIsNaN(candidate)) { | 58 if (keyIsNaN && $numberIsNaN(candidate)) { |
| 52 return entry; | 59 return entry; |
| 53 } | 60 } |
| 61 entry = ORDERED_HASH_MAP_CHAIN_AT(table, entry, numBuckets); | |
| 62 if (entry === NOT_FOUND) return entry; | |
| 63 candidate = ORDERED_HASH_MAP_KEY_AT(table, entry, numBuckets); | |
| 64 if (key === candidate) return entry; | |
| 54 } | 65 } |
| 55 return NOT_FOUND; | 66 return NOT_FOUND; |
| 56 } | 67 } |
| 57 %SetForceInlineFlag(MapFindEntry); | 68 %SetForceInlineFlag(MapFindEntry); |
| 58 | 69 |
| 59 | 70 |
| 60 function ComputeIntegerHash(key, seed) { | 71 function ComputeIntegerHash(key, seed) { |
| 61 var hash = key; | 72 var hash = key; |
| 62 hash = hash ^ seed; | 73 hash = hash ^ seed; |
| 63 hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1; | 74 hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1; |
| 64 hash = hash ^ (hash >>> 12); | 75 hash = hash ^ (hash >>> 12); |
| 65 hash = hash + (hash << 2); | 76 hash = hash + (hash << 2); |
| 66 hash = hash ^ (hash >>> 4); | 77 hash = hash ^ (hash >>> 4); |
| 67 hash = (hash * 2057) | 0; // hash = (hash + (hash << 3)) + (hash << 11); | 78 hash = (hash * 2057) | 0; // hash = (hash + (hash << 3)) + (hash << 11); |
| 68 hash = hash ^ (hash >>> 16); | 79 hash = hash ^ (hash >>> 16); |
| 69 return hash; | 80 return hash & 0x3fffffff; |
| 70 } | 81 } |
| 71 %SetForceInlineFlag(ComputeIntegerHash); | 82 %SetForceInlineFlag(ComputeIntegerHash); |
| 72 | 83 |
| 84 var hashCodeSymbol = GLOBAL_PRIVATE("hash_code_symbol"); | |
| 73 | 85 |
| 74 function GetHash(key) { | 86 function GetExistingHash(key) { |
|
Toon Verwaest
2015/05/26 09:06:26
That's a pretty deceiving name since it does compu
Erik Corry Chromium.org
2015/05/26 09:51:32
Left as is after offline discussion.
| |
| 75 if (%_IsSmi(key)) { | 87 if (%_IsSmi(key)) { |
| 76 return ComputeIntegerHash(key, 0); | 88 return ComputeIntegerHash(key, 0); |
| 77 } | 89 } |
| 78 if (IS_STRING(key)) { | 90 if (IS_STRING(key)) { |
| 79 var field = %_StringGetRawHashField(key); | 91 var field = %_StringGetRawHashField(key); |
| 80 if ((field & 1 /* Name::kHashNotComputedMask */) === 0) { | 92 if ((field & 1 /* Name::kHashNotComputedMask */) === 0) { |
| 81 return field >>> 2 /* Name::kHashShift */; | 93 return field >>> 2 /* Name::kHashShift */; |
| 82 } | 94 } |
| 95 } else if (IS_SPEC_OBJECT(key) && !%_IsJSProxy(key) && !IS_GLOBAL(key)) { | |
| 96 var hash = GET_PRIVATE(key, hashCodeSymbol); | |
| 97 return hash; | |
| 83 } | 98 } |
| 84 return %GenericHash(key); | 99 return %GenericHash(key); |
| 85 } | 100 } |
| 101 %SetForceInlineFlag(GetExistingHash); | |
| 102 | |
| 103 | |
| 104 function GetHash(key) { | |
| 105 var hash = GetExistingHash(key); | |
| 106 if (IS_UNDEFINED(hash)) { | |
| 107 hash = IntRandom() | 0; | |
| 108 if (hash === 0) hash = 1; | |
| 109 SET_PRIVATE(key, hashCodeSymbol, hash); | |
| 110 } | |
| 111 return hash; | |
| 112 } | |
| 86 %SetForceInlineFlag(GetHash); | 113 %SetForceInlineFlag(GetHash); |
| 87 | 114 |
| 88 | 115 |
| 89 // ------------------------------------------------------------------- | 116 // ------------------------------------------------------------------- |
| 90 // Harmony Set | 117 // Harmony Set |
| 91 | 118 |
| 92 function SetConstructor(iterable) { | 119 function SetConstructor(iterable) { |
| 93 if (!%_IsConstructCall()) { | 120 if (!%_IsConstructCall()) { |
| 94 throw MakeTypeError(kConstructorNotFunction, "Set"); | 121 throw MakeTypeError(kConstructorNotFunction, "Set"); |
| 95 } | 122 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 return this; | 175 return this; |
| 149 } | 176 } |
| 150 | 177 |
| 151 | 178 |
| 152 function SetHas(key) { | 179 function SetHas(key) { |
| 153 if (!IS_SET(this)) { | 180 if (!IS_SET(this)) { |
| 154 throw MakeTypeError(kIncompatibleMethodReceiver, 'Set.prototype.has', this); | 181 throw MakeTypeError(kIncompatibleMethodReceiver, 'Set.prototype.has', this); |
| 155 } | 182 } |
| 156 var table = %_JSCollectionGetTable(this); | 183 var table = %_JSCollectionGetTable(this); |
| 157 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); | 184 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); |
| 158 var hash = GetHash(key); | 185 var hash = GetExistingHash(key); |
| 186 if (IS_UNDEFINED(hash)) return false; | |
| 159 return SetFindEntry(table, numBuckets, key, hash) !== NOT_FOUND; | 187 return SetFindEntry(table, numBuckets, key, hash) !== NOT_FOUND; |
| 160 } | 188 } |
| 161 | 189 |
| 162 | 190 |
| 163 function SetDelete(key) { | 191 function SetDelete(key) { |
| 164 if (!IS_SET(this)) { | 192 if (!IS_SET(this)) { |
| 165 throw MakeTypeError(kIncompatibleMethodReceiver, | 193 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 166 'Set.prototype.delete', this); | 194 'Set.prototype.delete', this); |
| 167 } | 195 } |
| 168 var table = %_JSCollectionGetTable(this); | 196 var table = %_JSCollectionGetTable(this); |
| 169 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); | 197 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); |
| 170 var hash = GetHash(key); | 198 var hash = GetExistingHash(key); |
| 199 if (IS_UNDEFINED(hash)) return false; | |
| 171 var entry = SetFindEntry(table, numBuckets, key, hash); | 200 var entry = SetFindEntry(table, numBuckets, key, hash); |
| 172 if (entry === NOT_FOUND) return false; | 201 if (entry === NOT_FOUND) return false; |
| 173 | 202 |
| 174 var nof = ORDERED_HASH_TABLE_ELEMENT_COUNT(table) - 1; | 203 var nof = ORDERED_HASH_TABLE_ELEMENT_COUNT(table) - 1; |
| 175 var nod = ORDERED_HASH_TABLE_DELETED_COUNT(table) + 1; | 204 var nod = ORDERED_HASH_TABLE_DELETED_COUNT(table) + 1; |
| 176 var index = ORDERED_HASH_SET_ENTRY_TO_INDEX(entry, numBuckets); | 205 var index = ORDERED_HASH_SET_ENTRY_TO_INDEX(entry, numBuckets); |
| 177 FIXED_ARRAY_SET(table, index, %_TheHole()); | 206 FIXED_ARRAY_SET(table, index, %_TheHole()); |
| 178 ORDERED_HASH_TABLE_SET_ELEMENT_COUNT(table, nof); | 207 ORDERED_HASH_TABLE_SET_ELEMENT_COUNT(table, nof); |
| 179 ORDERED_HASH_TABLE_SET_DELETED_COUNT(table, nod); | 208 ORDERED_HASH_TABLE_SET_DELETED_COUNT(table, nod); |
| 180 if (nof < (numBuckets >>> 1)) %SetShrink(this); | 209 if (nof < (numBuckets >>> 1)) %SetShrink(this); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 } | 304 } |
| 276 | 305 |
| 277 | 306 |
| 278 function MapGet(key) { | 307 function MapGet(key) { |
| 279 if (!IS_MAP(this)) { | 308 if (!IS_MAP(this)) { |
| 280 throw MakeTypeError(kIncompatibleMethodReceiver, | 309 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 281 'Map.prototype.get', this); | 310 'Map.prototype.get', this); |
| 282 } | 311 } |
| 283 var table = %_JSCollectionGetTable(this); | 312 var table = %_JSCollectionGetTable(this); |
| 284 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); | 313 var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); |
| 285 var hash = GetHash(key); | 314 var hash = GetExistingHash(key); |
| 315 if (IS_UNDEFINED(hash)) return UNDEFINED; | |
| 286 var entry = MapFindEntry(table, numBuckets, key, hash); | 316 var entry = MapFindEntry(table, numBuckets, key, hash); |
| 287 if (entry === NOT_FOUND) return UNDEFINED; | 317 if (entry === NOT_FOUND) return UNDEFINED; |
| 288 return ORDERED_HASH_MAP_VALUE_AT(table, entry, numBuckets); | 318 return ORDERED_HASH_MAP_VALUE_AT(table, entry, numBuckets); |
| 289 } | 319 } |
| 290 | 320 |
| 291 | 321 |
| 292 function MapSet(key, value) { | 322 function MapSet(key, value) { |
| 293 if (!IS_MAP(this)) { | 323 if (!IS_MAP(this)) { |
| 294 throw MakeTypeError(kIncompatibleMethodReceiver, | 324 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 295 'Map.prototype.set', this); | 325 'Map.prototype.set', this); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 430 $installGetter(GlobalMap.prototype, "size", MapGetSize); | 460 $installGetter(GlobalMap.prototype, "size", MapGetSize); |
| 431 $installFunctions(GlobalMap.prototype, DONT_ENUM, [ | 461 $installFunctions(GlobalMap.prototype, DONT_ENUM, [ |
| 432 "get", MapGet, | 462 "get", MapGet, |
| 433 "set", MapSet, | 463 "set", MapSet, |
| 434 "has", MapHas, | 464 "has", MapHas, |
| 435 "delete", MapDelete, | 465 "delete", MapDelete, |
| 436 "clear", MapClearJS, | 466 "clear", MapClearJS, |
| 437 "forEach", MapForEach | 467 "forEach", MapForEach |
| 438 ]); | 468 ]); |
| 439 | 469 |
| 470 // Expose to the global scope. | |
| 471 $getHash = GetHash; | |
| 472 $getExistingHash = GetExistingHash; | |
| 473 | |
| 440 }) | 474 }) |
| OLD | NEW |