mirror of https://github.com/grpc/grpc.git
[slice] Remove murmur implementation (#31118)
* [slice] Remove _internal variants of APIs * Automated change: Fix sanity tests * fix * reduce bloat * fixes * Automated change: Fix sanity tests * murmurings * Automated change: Fix sanity tests * progress * Automated change: Fix sanity tests * fix * fix * Automated change: Fix sanity tests * fix * Automated change: Fix sanity tests * Automated change: Fix sanity tests Co-authored-by: ctiller <ctiller@users.noreply.github.com>pull/31207/head
parent
6fdbe67946
commit
66c8e098ee
38 changed files with 75 additions and 538 deletions
@ -1,82 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/gpr/murmur_hash.h" |
||||
|
||||
#include <string.h> |
||||
|
||||
#include "absl/base/attributes.h" |
||||
|
||||
#define ROTL32(x, r) (((x) << (r)) | ((x) >> (32 - (r)))) |
||||
|
||||
#define FMIX32(h) \ |
||||
(h) ^= (h) >> 16; \
|
||||
(h) *= 0x85ebca6b; \
|
||||
(h) ^= (h) >> 13; \
|
||||
(h) *= 0xc2b2ae35; \
|
||||
(h) ^= (h) >> 16; |
||||
|
||||
uint32_t gpr_murmur_hash3(const void* key, size_t len, uint32_t seed) { |
||||
uint32_t h1 = seed; |
||||
uint32_t k1; |
||||
|
||||
const uint32_t c1 = 0xcc9e2d51; |
||||
const uint32_t c2 = 0x1b873593; |
||||
|
||||
const uint8_t* keyptr = static_cast<const uint8_t*>(key); |
||||
const size_t bsize = sizeof(k1); |
||||
const size_t nblocks = len / bsize; |
||||
|
||||
/* body */ |
||||
for (size_t i = 0; i < nblocks; i++, keyptr += bsize) { |
||||
memcpy(&k1, keyptr, bsize); |
||||
|
||||
k1 *= c1; |
||||
k1 = ROTL32(k1, 15); |
||||
k1 *= c2; |
||||
|
||||
h1 ^= k1; |
||||
h1 = ROTL32(h1, 13); |
||||
h1 = h1 * 5 + 0xe6546b64; |
||||
} |
||||
|
||||
k1 = 0; |
||||
|
||||
/* tail */ |
||||
switch (len & 3) { |
||||
case 3: |
||||
k1 ^= (static_cast<uint32_t>(keyptr[2])) << 16; |
||||
ABSL_FALLTHROUGH_INTENDED; |
||||
case 2: |
||||
k1 ^= (static_cast<uint32_t>(keyptr[1])) << 8; |
||||
ABSL_FALLTHROUGH_INTENDED; |
||||
case 1: |
||||
k1 ^= keyptr[0]; |
||||
k1 *= c1; |
||||
k1 = ROTL32(k1, 15); |
||||
k1 *= c2; |
||||
h1 ^= k1; |
||||
}; |
||||
|
||||
/* finalization */ |
||||
h1 ^= static_cast<uint32_t>(len); |
||||
FMIX32(h1); |
||||
return h1; |
||||
} |
@ -1,30 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_CORE_LIB_GPR_MURMUR_HASH_H |
||||
#define GRPC_CORE_LIB_GPR_MURMUR_HASH_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <stddef.h> |
||||
#include <stdint.h> |
||||
|
||||
/* compute the hash of key (length len) */ |
||||
uint32_t gpr_murmur_hash3(const void* key, size_t len, uint32_t seed); |
||||
|
||||
#endif /* GRPC_CORE_LIB_GPR_MURMUR_HASH_H */ |
@ -1,35 +0,0 @@ |
||||
// Copyright 2021 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/slice/slice_refcount.h" |
||||
|
||||
#include <chrono> |
||||
|
||||
namespace grpc_core { |
||||
|
||||
uint32_t g_hash_seed = []() { |
||||
auto now = std::chrono::system_clock::now(); |
||||
auto now_since_epoch = now.time_since_epoch(); |
||||
auto now_ns = |
||||
std::chrono::duration_cast<std::chrono::nanoseconds>(now_since_epoch); |
||||
return static_cast<uint32_t>(now_ns.count()); |
||||
}(); |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
void grpc_test_only_set_slice_hash_seed(uint32_t seed) { |
||||
grpc_core::g_hash_seed = seed; |
||||
} |
@ -1,60 +0,0 @@ |
||||
// Copyright 2021 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef GRPC_CORE_LIB_SLICE_SLICE_REFCOUNT_BASE_H |
||||
#define GRPC_CORE_LIB_SLICE_SLICE_REFCOUNT_BASE_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <stddef.h> |
||||
|
||||
#include <atomic> |
||||
|
||||
// grpc_slice_refcount : A reference count for grpc_slice.
|
||||
struct grpc_slice_refcount { |
||||
public: |
||||
typedef void (*DestroyerFn)(grpc_slice_refcount*); |
||||
|
||||
static grpc_slice_refcount* NoopRefcount() { |
||||
return reinterpret_cast<grpc_slice_refcount*>(1); |
||||
} |
||||
|
||||
grpc_slice_refcount() = default; |
||||
|
||||
// Regular constructor for grpc_slice_refcount.
|
||||
//
|
||||
// Parameters:
|
||||
// 1. DestroyerFn destroyer_fn
|
||||
// Called when the refcount goes to 0, with 'this' as parameter.
|
||||
explicit grpc_slice_refcount(DestroyerFn destroyer_fn) |
||||
: destroyer_fn_(destroyer_fn) {} |
||||
|
||||
void Ref() { ref_.fetch_add(1, std::memory_order_relaxed); } |
||||
void Unref() { |
||||
if (ref_.fetch_sub(1, std::memory_order_acq_rel) == 1) { |
||||
destroyer_fn_(this); |
||||
} |
||||
} |
||||
|
||||
// Is this the only instance?
|
||||
// For this to be useful the caller needs to ensure that if this is the only
|
||||
// instance, no other instance could be created during this call.
|
||||
bool IsUnique() const { return ref_.load(std::memory_order_relaxed) == 1; } |
||||
|
||||
private: |
||||
std::atomic<size_t> ref_{1}; |
||||
DestroyerFn destroyer_fn_ = nullptr; |
||||
}; |
||||
|
||||
#endif // GRPC_CORE_LIB_SLICE_SLICE_REFCOUNT_BASE_H
|
@ -1,81 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#include "src/core/lib/gpr/murmur_hash.h" |
||||
|
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
|
||||
#include <gtest/gtest.h> |
||||
|
||||
#include <grpc/support/log.h> |
||||
|
||||
#include "test/core/util/test_config.h" |
||||
|
||||
typedef uint32_t (*hash_func)(const void* key, size_t len, uint32_t seed); |
||||
|
||||
/* From smhasher:
|
||||
This should hopefully be a thorough and uambiguous test of whether a hash |
||||
is correctly implemented on a given platform */ |
||||
|
||||
static void verification_test(hash_func hash, uint32_t expected) { |
||||
uint8_t key[256]; |
||||
uint32_t hashes[256]; |
||||
uint32_t final = 0; |
||||
size_t i; |
||||
|
||||
memset(key, 0, sizeof(key)); |
||||
memset(hashes, 0, sizeof(hashes)); |
||||
|
||||
/* Hash keys of the form {0}, {0,1}, {0,1,2}... up to N=255,using 256-N as
|
||||
the seed */ |
||||
|
||||
for (i = 0; i < 256; i++) { |
||||
key[i] = static_cast<uint8_t>(i); |
||||
hashes[i] = hash(key, i, static_cast<uint32_t>(256u - i)); |
||||
} |
||||
|
||||
/* Then hash the result array */ |
||||
|
||||
final = hash(hashes, sizeof(hashes), 0); |
||||
|
||||
/* The first four bytes of that hash, interpreted as a little-endian integer,
|
||||
is our |
||||
verification value */ |
||||
|
||||
if (expected != final) { |
||||
gpr_log(GPR_INFO, "Verification value 0x%08X : Failed! (Expected 0x%08x)", |
||||
final, expected); |
||||
abort(); |
||||
} else { |
||||
gpr_log(GPR_INFO, "Verification value 0x%08X : Passed!", final); |
||||
} |
||||
} |
||||
|
||||
TEST(MurmurHashTest, MainTest) { |
||||
/* basic tests to verify that things don't crash */ |
||||
gpr_murmur_hash3("", 0, 0); |
||||
gpr_murmur_hash3("xyz", 3, 0); |
||||
verification_test(gpr_murmur_hash3, 0xB0F57EE3); |
||||
} |
||||
|
||||
int main(int argc, char** argv) { |
||||
grpc::testing::TestEnvironment env(&argc, argv); |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
return RUN_ALL_TESTS(); |
||||
} |
Loading…
Reference in new issue