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

Unified Diff: dart/frog/leg/lib/js_helper.dart

Issue 9490003: Implement String.hashCode. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Improve performance by avoiding bailout. Created 8 years, 10 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | dart/tests/co19/co19-leg.status » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: dart/frog/leg/lib/js_helper.dart
diff --git a/dart/frog/leg/lib/js_helper.dart b/dart/frog/leg/lib/js_helper.dart
index d730fa097f52c06284dd16c8f6629a26c3af305f..86c85b539a8636b8d82a8a9e672226c357e180ab 100644
--- a/dart/frog/leg/lib/js_helper.dart
+++ b/dart/frog/leg/lib/js_helper.dart
@@ -359,7 +359,7 @@ builtin$charCodeAt$1(var receiver, int index) {
if (index is !num) throw new IllegalArgumentException(index);
if (index < 0) throw new IndexOutOfRangeException(index);
if (index >= receiver.length) throw new IndexOutOfRangeException(index);
- return JS("string", @"$0.charCodeAt($1)", receiver, index);
+ return JS("int", @"$0.charCodeAt($1)", receiver, index);
} else {
return UNINTERCEPTED(receiver.charCodeAt(index));
}
@@ -1180,15 +1180,27 @@ class MathNatives {
static double random() => JS("double", @"Math.random()");
}
+/**
+ * This is the [Jenkins hash function][1], but always using XOR
+ * instead of addition to keep the hash value in 32bit. This was
+ * inspired by jmesserly's work in Frog. However, using XOR is 40%
+ * faster than the original version in Frog.
+ *
+ * [1]: http://en.wikipedia.org/wiki/Jenkins_hash_function
+ */
builtin$hashCode$0(receiver) {
- if (receiver is num) return receiver & 0x1FFFFFFF;
- if (receiver is String) {
- throw 'String.hashCode is not implemented';
- }
- if (isJsArray(receiver)) {
- throw 'List.hashCode is not implemented';
- }
- return UNINTERCEPTED(receiver.hashCode());
+ if (receiver is num) return JS('int', @'$0 & 0x1FFFFFFF', receiver);
ngeoffray 2012/02/28 10:13:25 Please add a TODO to make sure we change the code
ahe 2012/02/28 17:26:10 Done.
+ if (receiver is !String) return UNINTERCEPTED(receiver.hashCode());
+ int hash = 0;
+ int length = JS('int', @'$0.length', receiver);
+ for (int i = 0; i < length; i++) {
+ hash ^= JS('int', @'$0.charCodeAt($1)', receiver, i);
+ hash ^= JS("int", @"$0 << $1", hash, 10);
+ hash ^= hash >> 6;
+ }
+ hash ^= JS("int", @"$0 << $1", hash, 3);
+ hash ^= hash >> 11;
+ return 0x1fffffff & (hash ^ JS("int", @"$0 << $1", hash, 15));
}
// TODO(ahe): Dynamic may be overridden.
« no previous file with comments | « no previous file | dart/tests/co19/co19-leg.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698