Skip to content

Commit 74eee2a

Browse files
Abseil Teamcopybara-github
authored andcommitted
Replace absl::Hash for inputs from 9 to 16 bytes according to AlphaZero findings
PiperOrigin-RevId: 500401844 Change-Id: I6d0909a8e395c914861dd034824a34737a52d71f
1 parent e160c5f commit 74eee2a

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

absl/hash/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ cc_library(
4343
"//absl/container:fixed_array",
4444
"//absl/functional:function_ref",
4545
"//absl/meta:type_traits",
46+
"//absl/numeric:bits",
4647
"//absl/numeric:int128",
4748
"//absl/strings",
4849
"//absl/types:optional",

absl/hash/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ absl_cc_library(
2525
COPTS
2626
${ABSL_DEFAULT_COPTS}
2727
DEPS
28+
absl::bits
2829
absl::city
2930
absl::config
3031
absl::core_headers

absl/hash/internal/hash.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "absl/hash/internal/city.h"
5050
#include "absl/hash/internal/low_level_hash.h"
5151
#include "absl/meta/type_traits.h"
52+
#include "absl/numeric/bits.h"
5253
#include "absl/numeric/int128.h"
5354
#include "absl/strings/string_view.h"
5455
#include "absl/types/optional.h"
@@ -1052,7 +1053,7 @@ class ABSL_DLL MixingHashState : public HashStateBase<MixingHashState> {
10521053
uint64_t most_significant = low_mem;
10531054
uint64_t least_significant = high_mem;
10541055
#endif
1055-
return {least_significant, most_significant >> (128 - len * 8)};
1056+
return {least_significant, most_significant};
10561057
}
10571058

10581059
// Reads 4 to 8 bytes from p. Zero pads to fill uint64_t.
@@ -1183,9 +1184,22 @@ inline uint64_t MixingHashState::CombineContiguousImpl(
11831184
}
11841185
v = Hash64(first, len);
11851186
} else if (len > 8) {
1187+
// This hash function was constructed by the ML-driven algorithm discovery
1188+
// using reinforcement learning. We fed the agent lots of inputs from
1189+
// microbenchmarks, SMHasher, low hamming distance from generated inputs and
1190+
// picked up the one that was good on micro and macrobenchmarks.
11861191
auto p = Read9To16(first, len);
1187-
state = Mix(state, p.first);
1188-
v = p.second;
1192+
uint64_t lo = p.first;
1193+
uint64_t hi = p.second;
1194+
// Rotation by 53 was found to be most often useful when discovering these
1195+
// hashing algorithms with ML techniques.
1196+
lo = absl::rotr(lo, 53);
1197+
state += kMul;
1198+
lo += state;
1199+
state ^= hi;
1200+
uint128 m = state;
1201+
m *= lo;
1202+
return static_cast<uint64_t>(m ^ (m >> 64));
11891203
} else if (len >= 4) {
11901204
v = Read4To8(first, len);
11911205
} else if (len > 0) {

0 commit comments

Comments
 (0)