Skip to content

Commit

Permalink
Specialize Symbol.hashCode
Browse files Browse the repository at this point in the history
Patch internal.Symbol.hashCode and specialize JS version to avoid
recomputing String.hashCode

R=sigmund@google.com

Review URL: https://codereview.chromium.org/2038853003 .
  • Loading branch information
rakudrama committed Jun 3, 2016
1 parent 5e9f7a8 commit b377479
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
5 changes: 5 additions & 0 deletions runtime/lib/symbol_patch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ patch class Symbol {
}
return result.toString();
}

/* patch */ int get hashCode {
const arbitraryPrime = 664597;
return 0x1fffffff & (arbitraryPrime * _name.hashCode);
}
}
11 changes: 11 additions & 0 deletions sdk/lib/_internal/js_runtime/lib/internal_patch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@
import 'dart:_js_primitives' show printString;
import 'dart:_js_helper' show patch;
import 'dart:_interceptors' show JSArray;
import 'dart:_foreign_helper' show JS;

@patch
class Symbol implements core.Symbol {
@patch
const Symbol(String name)
: this._name = name;

@patch
int get hashCode {
int hash = JS('int|Null', '#._hashCode', this);
if (hash != null) return hash;
const arbitraryPrime = 664597;
hash = 0x1fffffff & (arbitraryPrime * _name.hashCode);
JS('', '#._hashCode = #', this, hash);
return hash;
}
}

@patch
Expand Down
5 changes: 1 addition & 4 deletions sdk/lib/internal/symbol.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ class Symbol implements core.Symbol {

bool operator ==(other) => other is Symbol && _name == other._name;

int get hashCode {
const arbitraryPrime = 664597;
return 0x1fffffff & (arbitraryPrime * _name.hashCode);
}
external int get hashCode;

toString() => 'Symbol("$_name")';

Expand Down

0 comments on commit b377479

Please sign in to comment.