Skip to content

Commit 95d8710

Browse files
osa1Commit Queue
authored andcommitted
[dart2wasm] Cache hash codes in JSStringImpl
Change-Id: I41980461f9e6a4818458aa17471bd868b70ad354 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/362781 Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Ömer Ağacan <[email protected]>
1 parent 8e15937 commit 95d8710

File tree

6 files changed

+18
-10
lines changed

6 files changed

+18
-10
lines changed

pkg/dart2wasm/lib/intrinsics.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,14 +732,14 @@ class Intrinsifier {
732732
// dart:_object_helper static functions.
733733
if (node.target.enclosingLibrary.name == 'dart._object_helper') {
734734
switch (name) {
735-
case "getHash":
735+
case "getIdentityHashField":
736736
Expression arg = node.arguments.positional[0];
737737
w.ValueType objectType = translator.objectInfo.nonNullableType;
738738
codeGen.wrap(arg, objectType);
739739
b.struct_get(translator.objectInfo.struct, FieldIndex.identityHash);
740740
b.i64_extend_i32_u();
741741
return w.NumType.i64;
742-
case "setHash":
742+
case "setIdentityHashField":
743743
Expression arg = node.arguments.positional[0];
744744
Expression hash = node.arguments.positional[1];
745745
w.ValueType objectType = translator.objectInfo.nonNullableType;

sdk/lib/_internal/wasm/lib/js_string.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,17 @@ final class JSStringImpl implements String {
606606
}
607607
}
608608

609-
/// This must be kept in sync with `StringBase.hashCode` in string_patch.dart.
610-
/// TODO(joshualitt): Find some way to cache the hash code.
611609
@override
612610
int get hashCode {
611+
int hash = getIdentityHashField(this);
612+
if (hash != 0) return hash;
613+
hash = _computeHashCode();
614+
setIdentityHashField(this, hash);
615+
return hash;
616+
}
617+
618+
/// This must be kept in sync with `StringBase.hashCode` in string_patch.dart.
619+
int _computeHashCode() {
613620
int hash = 0;
614621
final length = this.length;
615622
for (int i = 0; i < length; i++) {

sdk/lib/_internal/wasm/lib/js_types.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ library dart._js_types;
1616
import 'dart:_error_utils';
1717
import 'dart:_internal';
1818
import 'dart:_js_helper' as js;
19+
import 'dart:_object_helper';
1920
import 'dart:_string_helper';
2021
import 'dart:_wasm';
2122
import 'dart:collection';

sdk/lib/_internal/wasm/lib/object_helper.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
library dart._object_helper;
66

77
// Access hidden identity hash code field.
8-
external int getHash(Object obj);
9-
external void setHash(Object obj, int hash);
8+
external int getIdentityHashField(Object obj);
9+
external void setIdentityHashField(Object obj, int hash);

sdk/lib/_internal/wasm/lib/object_patch.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ class Object {
1313
static final _hashCodeRnd = new Random();
1414

1515
static int _objectHashCode(Object obj) {
16-
var result = getHash(obj);
16+
var result = getIdentityHashField(obj);
1717
if (result == 0) {
1818
// We want the hash to be a Smi value greater than 0.
1919
do {
2020
result = _hashCodeRnd.nextInt(0x40000000);
2121
} while (result == 0);
2222

23-
setHash(obj, result);
23+
setIdentityHashField(obj, result);
2424
return result;
2525
}
2626
return result;

sdk/lib/_internal/wasm/lib/string.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ abstract final class StringBase extends WasmStringBase {
9696
static const int _maxUnsignedSmiBits = 63;
9797

9898
int get hashCode {
99-
int hash = getHash(this);
99+
int hash = getIdentityHashField(this);
100100
if (hash != 0) return hash;
101101
hash = _computeHashCode();
102-
setHash(this, hash);
102+
setIdentityHashField(this, hash);
103103
return hash;
104104
}
105105

0 commit comments

Comments
 (0)