You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by ja...@apache.org on 2020/01/13 06:28:23 UTC

[incubator-brpc] branch master updated: bugfix to the hash function HashInts32.

This is an automated email from the ASF dual-hosted git repository.

jamesge pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 660b9b3  bugfix to the hash function HashInts32.
     new 9fb0ba2  Merge pull request #1009 from pexeer/chenzj
660b9b3 is described below

commit 660b9b321378460fec6283fb8e50e5f71d5c0490
Author: pexeer <pe...@gmail.com>
AuthorDate: Mon Dec 30 11:46:06 2019 +0800

    bugfix to the hash function HashInts32.
---
 src/butil/containers/hash_tables.h |  3 +++
 test/endpoint_unittest.cpp         | 27 +++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/src/butil/containers/hash_tables.h b/src/butil/containers/hash_tables.h
index 7b2986b..88766cc 100644
--- a/src/butil/containers/hash_tables.h
+++ b/src/butil/containers/hash_tables.h
@@ -26,6 +26,7 @@
 #include "butil/basictypes.h"
 #include "butil/strings/string16.h"
 #include "butil/build_config.h"
+#include "butil/third_party/murmurhash3/murmurhash3.h"   // fmix64
 
 #if defined(COMPILER_MSVC)
 #include <hash_map>
@@ -132,6 +133,8 @@ inline std::size_t HashInts32(uint32_t value1, uint32_t value2) {
   uint64_t value1_64 = value1;
   uint64_t hash64 = (value1_64 << 32) | value2;
 
+  hash64 = fmix64(hash64);
+
   if (sizeof(std::size_t) >= sizeof(uint64_t))
     return static_cast<std::size_t>(hash64);
 
diff --git a/test/endpoint_unittest.cpp b/test/endpoint_unittest.cpp
index 503fb66..ed24d12 100644
--- a/test/endpoint_unittest.cpp
+++ b/test/endpoint_unittest.cpp
@@ -19,6 +19,7 @@
 #include "butil/errno.h"
 #include "butil/endpoint.h"
 #include "butil/logging.h"
+#include "butil/containers/flat_map.h"
 
 namespace {
 
@@ -121,4 +122,30 @@ TEST(EndPointTest, hash_table) {
     ASSERT_EQ(2u, m.size());
 }
 
+TEST(EndPointTest, flat_map) {
+    butil::FlatMap<butil::EndPoint, int> m;
+    ASSERT_EQ(0, m.init(1024));
+    uint32_t port = 8088;
+
+    butil::EndPoint ep1(butil::IP_ANY, port);
+    butil::EndPoint ep2(butil::IP_ANY, port);
+    ++m[ep1];
+    ++m[ep2];
+    ASSERT_EQ(1u, m.size());
+
+    butil::ip_t ip_addr;
+    butil::str2ip("10.10.10.10", &ip_addr);
+    int ip = butil::ip2int(ip_addr);
+
+    for (int i = 0; i < 1023; ++i) {
+        butil::EndPoint ep(butil::int2ip(++ip), port);
+        ++m[ep];
+    }
+
+    butil::BucketInfo info = m.bucket_info();
+    LOG(INFO) << "bucket info max long=" << info.longest_length
+        << " avg=" << info.average_length << std::endl;
+    ASSERT_LT(info.longest_length, 32) << "detect hash collision and it's too large.";
 }
+
+} // end of namespace


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org