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

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: 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..e106600d623d3c12bcb4bf97a2a331ba2719d29d 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));
}
@@ -1181,16 +1181,36 @@ class MathNatives {
}
builtin$hashCode$0(receiver) {
+ if (receiver is String) return stringHashCodeUnchecked(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());
}
+/**
+ * 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.
+ *
+ * Caveat: As long as String.hashCode isn't optimized, Frog will
+ * out-perform Leg because of the overhead of calling
+ * [builtin$hashCode$0].
+ *
+ * [1]: http://en.wikipedia.org/wiki/Jenkins_hash_function
+ */
+stringHashCodeUnchecked(string) {
+ var hash = 0;
kasperl 2012/02/28 09:37:08 int hash?
ahe 2012/02/28 10:09:35 Done.
+ var length = JS('int', @'$0.length', string);
+ for (var i = 0; i < length; i++) {
kasperl 2012/02/28 09:37:08 int i?
ahe 2012/02/28 10:09:35 Done.
+ hash ^= JS('int', @'$0.charCodeAt($1)', string, i);
+ hash ^= JS("int", @"$0 << $1", hash, 10);
ngeoffray 2012/02/28 10:13:25 Cobnsistency -> use ' instead of "
ahe 2012/02/28 17:26:10 Done.
+ hash ^= hash >> 6;
+ }
+ hash ^= JS("int", @"$0 << $1", hash, 3);
ngeoffray 2012/02/28 10:13:25 ditto
ahe 2012/02/28 17:26:10 Done.
+ hash ^= hash >> 11;
+ return 0x1fffffff & (hash ^ JS("int", @"$0 << $1", hash, 15));
+}
+
// TODO(ahe): Dynamic may be overridden.
builtin$get$dynamic(receiver) => receiver;
« 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