From 762d1367b382692af54a570a850d5bb5f5efbe54 Mon Sep 17 00:00:00 2001 From: Sigurd Meldgaard Date: Wed, 16 Nov 2016 21:28:25 +0100 Subject: [PATCH] Faster hash codes. (flutter/engine#3214) * Faster hash codes. Mask the intermediate values when computing composite hash codes. * WIP * WIP * WIP --- engine/src/flutter/lib/ui/hash_codes.dart | 92 +++++++++++------------ 1 file changed, 44 insertions(+), 48 deletions(-) diff --git a/engine/src/flutter/lib/ui/hash_codes.dart b/engine/src/flutter/lib/ui/hash_codes.dart index c09cf82c34..74e338180e 100644 --- a/engine/src/flutter/lib/ui/hash_codes.dart +++ b/engine/src/flutter/lib/ui/hash_codes.dart @@ -7,6 +7,24 @@ part of dart_ui; class _HashEnd { const _HashEnd(); } const _HashEnd _hashEnd = const _HashEnd(); +/// Jenkins hash function, optimized for small integers. +/// +/// Borrowed from the dart sdk: sdk/lib/math/jenkins_smi_hash.dart. +class _Jenkins { + static int combine(int hash, Object o) { + assert(o is! Iterable); + hash = 0x1fffffff & (hash + o.hashCode); + hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); + return hash ^ (hash >> 6); + } + + static int finish(int hash) { + hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); + hash = hash ^ (hash >> 11); + return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); + } +} + /// Combine up to twenty values' hashCodes into one value. /// /// If you only need to handle one value's hashCode, then just refer to its @@ -27,65 +45,45 @@ int hashValues( Object arg13 = _hashEnd, Object arg14 = _hashEnd, Object arg15 = _hashEnd, Object arg16 = _hashEnd, Object arg17 = _hashEnd, Object arg18 = _hashEnd, Object arg19 = _hashEnd, Object arg20 = _hashEnd ]) { - int result = 373; - assert(arg01 is! Iterable); - result = 37 * result + arg01.hashCode; - assert(arg02 is! Iterable); - result = 37 * result + arg02.hashCode; + int result = 0; + result = _Jenkins.combine(result, arg01); + result = _Jenkins.combine(result, arg02); if (arg03 != _hashEnd) { - assert(arg03 is! Iterable); - result = 37 * result + arg03.hashCode; + result = _Jenkins.combine(result, arg03); if (arg04 != _hashEnd) { - assert(arg04 is! Iterable); - result = 37 * result + arg04.hashCode; + result = _Jenkins.combine(result, arg04); if (arg05 != _hashEnd) { - assert(arg05 is! Iterable); - result = 37 * result + arg05.hashCode; + result = _Jenkins.combine(result, arg05); if (arg06 != _hashEnd) { - assert(arg06 is! Iterable); - result = 37 * result + arg06.hashCode; + result = _Jenkins.combine(result, arg06); if (arg07 != _hashEnd) { - assert(arg07 is! Iterable); - result = 37 * result + arg07.hashCode; + result = _Jenkins.combine(result, arg07); if (arg08 != _hashEnd) { - assert(arg08 is! Iterable); - result = 37 * result + arg08.hashCode; + result = _Jenkins.combine(result, arg08); if (arg09 != _hashEnd) { - assert(arg09 is! Iterable); - result = 37 * result + arg09.hashCode; + result = _Jenkins.combine(result, arg09); if (arg10 != _hashEnd) { - assert(arg10 is! Iterable); - result = 37 * result + arg10.hashCode; + result = _Jenkins.combine(result, arg10); if (arg11 != _hashEnd) { - assert(arg11 is! Iterable); - result = 37 * result + arg11.hashCode; + result = _Jenkins.combine(result, arg11); if (arg12 != _hashEnd) { - assert(arg12 is! Iterable); - result = 37 * result + arg12.hashCode; + result = _Jenkins.combine(result, arg12); if (arg13 != _hashEnd) { - assert(arg13 is! Iterable); - result = 37 * result + arg13.hashCode; + result = _Jenkins.combine(result, arg13); if (arg14 != _hashEnd) { - assert(arg14 is! Iterable); - result = 37 * result + arg14.hashCode; + result = _Jenkins.combine(result, arg14); if (arg15 != _hashEnd) { - assert(arg15 is! Iterable); - result = 37 * result + arg15.hashCode; + result = _Jenkins.combine(result, arg15); if (arg16 != _hashEnd) { - assert(arg16 is! Iterable); - result = 37 * result + arg16.hashCode; + result = _Jenkins.combine(result, arg16); if (arg17 != _hashEnd) { - assert(arg17 is! Iterable); - result = 37 * result + arg17.hashCode; + result = _Jenkins.combine(result, arg17); if (arg18 != _hashEnd) { - assert(arg18 is! Iterable); - result = 37 * result + arg18.hashCode; + result = _Jenkins.combine(result, arg18); if (arg19 != _hashEnd) { - assert(arg19 is! Iterable); - result = 37 * result + arg19.hashCode; + result = _Jenkins.combine(result, arg19); if (arg20 != _hashEnd) { - assert(arg20 is! Iterable); - result = 37 * result + arg20.hashCode; + result = _Jenkins.combine(result, arg20); // I can see my house from here! } } @@ -105,19 +103,17 @@ int hashValues( } } } - return result; + return _Jenkins.finish(result); } /// Combine the hashCodes of an arbitrary number of values from an Iterable into /// one value. This function will return the same value if given "null" as if /// given an empty list. int hashList(Iterable args) { - int result = 373; + int result = 0; if (args != null) { - for (Object arg in args) { - assert(arg is! Iterable); - result = 37 * result + arg.hashCode; - } + for (Object arg in args) + result = _Jenkins.combine(result, arg); } - return result; + return _Jenkins.finish(result); }