Chromium Code Reviews| Index: src/collection.js |
| diff --git a/src/collection.js b/src/collection.js |
| index db30546165119e53f0c4e03a1b5e856259927f10..a4d5204ce3e3dd0ff025103d4c4ad3ab65401842 100644 |
| --- a/src/collection.js |
| +++ b/src/collection.js |
| @@ -2,6 +2,9 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +var $gethash; |
|
adamk
2015/05/21 19:11:32
Rename to $getHash
Erik Corry
2015/05/22 06:20:24
Done.
|
| +var $getexistinghash; |
|
adamk
2015/05/21 19:11:32
and $getExistingHash
Erik Corry
2015/05/22 06:20:24
Done.
|
| + |
| (function(global, shared, exports) { |
| "use strict"; |
| @@ -22,17 +25,19 @@ function HashToEntry(table, hash, numBuckets) { |
| function SetFindEntry(table, numBuckets, key, hash) { |
| + var entry = HashToEntry(table, hash, numBuckets); |
| + if (entry === NOT_FOUND) return entry; |
| + var candidate = ORDERED_HASH_SET_KEY_AT(table, entry, numBuckets); |
| + if (key === candidate) return entry; |
| var keyIsNaN = $numberIsNaN(key); |
| - for (var entry = HashToEntry(table, hash, numBuckets); |
|
adamk
2015/05/21 19:11:32
Why did this get rewritten as a while loop? From m
Erik Corry
2015/05/21 20:19:01
The idea is to avoid testing the key for Nan-ness
|
| - entry !== NOT_FOUND; |
| - entry = ORDERED_HASH_SET_CHAIN_AT(table, entry, numBuckets)) { |
| - var candidate = ORDERED_HASH_SET_KEY_AT(table, entry, numBuckets); |
| - if (key === candidate) { |
| - return entry; |
| - } |
| + while (true) { |
| if (keyIsNaN && $numberIsNaN(candidate)) { |
| return entry; |
| } |
| + entry = ORDERED_HASH_SET_CHAIN_AT(table, entry, numBuckets); |
| + if (entry === NOT_FOUND) return entry; |
| + candidate = ORDERED_HASH_SET_KEY_AT(table, entry, numBuckets); |
| + if (key === candidate) return entry; |
| } |
| return NOT_FOUND; |
| } |
| @@ -40,17 +45,19 @@ function SetFindEntry(table, numBuckets, key, hash) { |
| function MapFindEntry(table, numBuckets, key, hash) { |
| + var entry = HashToEntry(table, hash, numBuckets); |
| + if (entry === NOT_FOUND) return entry; |
| + var candidate = ORDERED_HASH_MAP_KEY_AT(table, entry, numBuckets); |
| + if (key === candidate) return entry; |
| var keyIsNaN = $numberIsNaN(key); |
| - for (var entry = HashToEntry(table, hash, numBuckets); |
|
adamk
2015/05/21 19:11:32
Same question down here.
Erik Corry
2015/05/21 20:19:01
Same answer :-)
|
| - entry !== NOT_FOUND; |
| - entry = ORDERED_HASH_MAP_CHAIN_AT(table, entry, numBuckets)) { |
| - var candidate = ORDERED_HASH_MAP_KEY_AT(table, entry, numBuckets); |
| - if (key === candidate) { |
| - return entry; |
| - } |
| + while (true) { |
| if (keyIsNaN && $numberIsNaN(candidate)) { |
| return entry; |
| } |
| + entry = ORDERED_HASH_MAP_CHAIN_AT(table, entry, numBuckets); |
| + if (entry === NOT_FOUND) return entry; |
| + candidate = ORDERED_HASH_MAP_KEY_AT(table, entry, numBuckets); |
| + if (key === candidate) return entry; |
| } |
| return NOT_FOUND; |
| } |
| @@ -66,12 +73,13 @@ function ComputeIntegerHash(key, seed) { |
| hash = hash ^ (hash >>> 4); |
| hash = (hash * 2057) | 0; // hash = (hash + (hash << 3)) + (hash << 11); |
| hash = hash ^ (hash >>> 16); |
| - return hash; |
| + return hash & 0x3fffffff; |
|
adamk
2015/05/21 19:11:32
Why the extra &? This used to be equivalent to Com
Erik Corry
2015/05/22 06:20:24
To make sure it's always a Smi. I changed Compute
|
| } |
| %SetInlineBuiltinFlag(ComputeIntegerHash); |
| +var hash_code_symbol = GLOBAL_PRIVATE("hash_code_symbol"); |
|
adamk
2015/05/21 19:11:32
Please use JS style here ("hashCodeSymbol").
Erik Corry
2015/05/22 06:20:24
Done.
|
| -function GetHash(key) { |
| +function GetExistingHash(key) { |
| if (%_IsSmi(key)) { |
| return ComputeIntegerHash(key, 0); |
| } |
| @@ -80,9 +88,27 @@ function GetHash(key) { |
| if ((field & 1 /* Name::kHashNotComputedMask */) === 0) { |
| return field >>> 2 /* Name::kHashShift */; |
| } |
| + } else if (IS_SPEC_OBJECT(key) && !%_IsJSProxy(key) && !IS_GLOBAL(key)) { |
| + var hash = GET_PRIVATE(key, hash_code_symbol); |
| + return hash; |
| } |
| return %GenericHash(key); |
|
adamk
2015/05/21 19:11:32
%GenericHash calls GetOrCreateHash, which seems wr
Erik Corry
2015/05/21 20:19:02
I think we don't care much about this case, it's o
|
| } |
| +%SetInlineBuiltinFlag(GetExistingHash); |
| + |
| + |
| +function GetHash(key) { |
| + var hash = GetExistingHash(key); |
| + if (IS_UNDEFINED(hash)) { |
| + if (IS_GLOBAL(key)) { |
|
adamk
2015/05/21 19:11:32
I think this branch is currently unreachable due t
Erik Corry
2015/05/21 20:19:02
Good point.
I think a better solution would be fo
|
| + return %GenericHash(key); |
| + } |
| + hash = $intrandom() | 0; |
| + if (hash === 0) hash = 1; |
| + SET_PRIVATE(key, hash_code_symbol, hash); |
| + } |
| + return hash; |
| +} |
| %SetInlineBuiltinFlag(GetHash); |
| @@ -155,7 +181,8 @@ function SetHas(key) { |
| } |
| var table = %_JSCollectionGetTable(this); |
| var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); |
| - var hash = GetHash(key); |
| + var hash = GetExistingHash(key); |
| + if (IS_UNDEFINED(hash)) return false; |
| return SetFindEntry(table, numBuckets, key, hash) !== NOT_FOUND; |
| } |
| @@ -167,7 +194,8 @@ function SetDelete(key) { |
| } |
| var table = %_JSCollectionGetTable(this); |
| var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); |
| - var hash = GetHash(key); |
| + var hash = GetExistingHash(key); |
| + if (IS_UNDEFINED(hash)) return false; |
| var entry = SetFindEntry(table, numBuckets, key, hash); |
| if (entry === NOT_FOUND) return false; |
| @@ -282,7 +310,8 @@ function MapGet(key) { |
| } |
| var table = %_JSCollectionGetTable(this); |
| var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table); |
| - var hash = GetHash(key); |
| + var hash = GetExistingHash(key); |
| + if (IS_UNDEFINED(hash)) return UNDEFINED; |
| var entry = MapFindEntry(table, numBuckets, key, hash); |
| if (entry === NOT_FOUND) return UNDEFINED; |
| return ORDERED_HASH_MAP_VALUE_AT(table, entry, numBuckets); |
| @@ -437,4 +466,8 @@ $installFunctions(GlobalMap.prototype, DONT_ENUM, [ |
| "forEach", MapForEach |
| ]); |
| +// Expose to the global scope. |
| +$gethash = GetHash; |
| +$getexistinghash = GetExistingHash; |
| + |
| }) |