Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 36d8b0d

Browse files
authored
Switch from custom hash_mix to absl::Hash. (#370)
The hash_mix code pre-dates absl::Hash.
1 parent 7f2d4a2 commit 36d8b0d

9 files changed

Lines changed: 21 additions & 78 deletions

File tree

opencensus/common/internal/BUILD

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ licenses(["notice"]) # Apache 2.0
2020

2121
package(default_visibility = ["//opencensus:__subpackages__"])
2222

23-
cc_library(
24-
name = "hash_mix",
25-
hdrs = ["hash_mix.h"],
26-
copts = DEFAULT_COPTS,
27-
)
28-
2923
cc_library(
3024
name = "hostname",
3125
srcs = ["hostname.cc"],
@@ -62,7 +56,7 @@ cc_library(
6256
name = "string_vector_hash",
6357
hdrs = ["string_vector_hash.h"],
6458
copts = DEFAULT_COPTS,
65-
deps = [":hash_mix"],
59+
deps = ["@com_google_absl//absl/hash"],
6660
)
6761

6862
# Tests

opencensus/common/internal/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
opencensus_lib(common_hash_mix)
16-
1715
opencensus_lib(common_hostname
1816
SRCS
1917
hostname.cc
@@ -34,7 +32,7 @@ opencensus_lib(common_stats_object DEPS absl::time)
3432
target_compile_definitions(opencensus_common_stats_object INTERFACE
3533
$<$<CXX_COMPILER_ID:MSVC>:NOMINMAX>)
3634

37-
opencensus_lib(common_string_vector_hash)
35+
opencensus_lib(common_string_vector_hash DEPS absl::hash)
3836

3937
opencensus_test(common_hostname_test hostname_test.cc common_hostname)
4038

opencensus/common/internal/hash_mix.h

Lines changed: 0 additions & 49 deletions
This file was deleted.

opencensus/common/internal/string_vector_hash.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,14 @@
1919
#include <string>
2020
#include <vector>
2121

22-
#include "opencensus/common/internal/hash_mix.h"
22+
#include "absl/hash/hash.h"
2323

2424
namespace opencensus {
2525
namespace common {
2626

2727
struct StringVectorHash {
2828
std::size_t operator()(const std::vector<std::string>& container) const {
29-
std::hash<std::string> hasher;
30-
HashMix mixer;
31-
for (const auto& elem : container) {
32-
mixer.Mix(hasher(elem));
33-
}
34-
return mixer.get();
29+
return absl::Hash<std::vector<std::string>>()(container);
3530
}
3631
};
3732

opencensus/tags/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ cc_library(
3434
copts = DEFAULT_COPTS,
3535
visibility = ["//visibility:public"],
3636
deps = [
37-
"//opencensus/common/internal:hash_mix",
3837
"@com_google_absl//absl/base:core_headers",
38+
"@com_google_absl//absl/hash",
3939
"@com_google_absl//absl/strings",
4040
"@com_google_absl//absl/synchronization",
4141
],

opencensus/tags/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ opencensus_lib(tags
1919
internal/tag_map.cc
2020
DEPS
2121
absl::strings
22-
common_hash_mix
2322
absl::base
23+
absl::hash
2424
absl::synchronization)
2525

2626
opencensus_lib(tags_context_util

opencensus/tags/internal/tag_key_test.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "opencensus/tags/tag_key.h"
1616

17+
#include "absl/hash/hash.h"
1718
#include "gtest/gtest.h"
1819

1920
namespace opencensus {
@@ -29,14 +30,14 @@ TEST(TagKeyTest, DuplicateRegistrationsEqual) {
2930
TagKey k1 = TagKey::Register("key");
3031
TagKey k2 = TagKey::Register("key");
3132
EXPECT_EQ(k1, k2);
32-
EXPECT_EQ(k1.hash(), k2.hash());
33+
EXPECT_EQ(absl::Hash<TagKey>()(k1), absl::Hash<TagKey>()(k2));
3334
}
3435

3536
TEST(TagKeyTest, Inequality) {
3637
TagKey k1 = TagKey::Register("k1");
3738
TagKey k2 = TagKey::Register("k2");
3839
EXPECT_NE(k1, k2);
39-
EXPECT_NE(k1.hash(), k2.hash());
40+
EXPECT_NE(absl::Hash<TagKey>()(k1), absl::Hash<TagKey>()(k2));
4041
}
4142

4243
} // namespace

opencensus/tags/internal/tag_map.cc

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
#include <utility>
2222
#include <vector>
2323

24+
#include "absl/hash/hash.h"
2425
#include "absl/strings/str_cat.h"
2526
#include "absl/strings/str_join.h"
2627
#include "absl/strings/string_view.h"
27-
#include "opencensus/common/internal/hash_mix.h"
2828
#include "opencensus/tags/tag_key.h"
2929

3030
namespace opencensus {
@@ -57,13 +57,7 @@ void TagMap::Initialize() {
5757
"Duplicate keys are not allowed in TagMap.");
5858
#endif
5959

60-
std::hash<std::string> hasher;
61-
common::HashMix mixer;
62-
for (const auto& tag : tags_) {
63-
mixer.Mix(tag.first.hash());
64-
mixer.Mix(hasher(tag.second));
65-
}
66-
hash_ = mixer.get();
60+
hash_ = absl::Hash<std::vector<std::pair<TagKey, std::string>>>()(tags_);
6761
}
6862

6963
std::size_t TagMap::Hash::operator()(const TagMap& tags) const {

opencensus/tags/tag_key.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <cstdint>
2020
#include <string>
2121

22+
#include "absl/base/macros.h"
23+
#include "absl/hash/hash.h"
2224
#include "absl/strings/string_view.h"
2325

2426
namespace opencensus {
@@ -40,8 +42,16 @@ class TagKey final {
4042
bool operator<(TagKey other) const { return id_ < other.id_; }
4143

4244
// Returns a suitable hash of the TagKey. The implementation may change.
45+
ABSL_DEPRECATED(
46+
"Use absl::Hash<TagKey>()(...) instead. "
47+
"This method will be removed on or after 2020-02-06")
4348
std::size_t hash() const { return id_; }
4449

50+
template <typename H>
51+
friend H AbslHashValue(H state, const TagKey& k) {
52+
return H::combine(std::move(state), k.id_);
53+
}
54+
4555
private:
4656
friend class TagKeyRegistry;
4757
explicit TagKey(uint64_t id) : id_(id) {}

0 commit comments

Comments
 (0)