diff --git a/cel-c/internal/BUILD b/cel-c/internal/BUILD index 363efd6..494dd9e 100644 --- a/cel-c/internal/BUILD +++ b/cel-c/internal/BUILD @@ -699,8 +699,8 @@ cc_test( deps = [ ":config", ":flat_hash_map", - ":hash", "//cel-c:alloc", + "@abseil-cpp//absl/hash", "@googletest//:gtest_main", ], ) @@ -721,8 +721,8 @@ cc_test( deps = [ ":config", ":flat_hash_set", - ":hash", "//cel-c:alloc", + "@abseil-cpp//absl/hash", "@googletest//:gtest_main", ], ) @@ -795,39 +795,6 @@ cc_test( ], ) -cc_library( - name = "hash", - srcs = [ - "hash.cc", - ], - hdrs = [ - "hash.h", - ], - deps = [ - ":bit", - ":config", - ":unaligned", - "//cel-c:assert", - "//cel-c:config", - "//cel-c:cstring_view", - "//cel-c:string_view", - "@abseil-cpp//absl/numeric:int128", - "@protobuf//upb/base", - ], -) - -cc_test( - name = "hash_test", - srcs = ["hash_test.cc"], - deps = [ - ":hash", - "//cel-c:cstring_view", - "//cel-c:string_view", - "@googletest//:gtest_main", - "@protobuf//upb/base", - ], -) - cc_library( name = "malloc", srcs = ["malloc.cc"], @@ -1351,7 +1318,6 @@ cc_library( ":bit", ":ckdint", ":config", - ":hash", ":message_equality", ":sort", "//cel-c:alloc", @@ -1367,6 +1333,7 @@ cc_library( "//cel-c:type", "//cel-c:value_headers", "//cel-c:value_kind", + "@abseil-cpp//absl/hash", "@protobuf//upb/base", "@protobuf//upb/message", "@protobuf//upb/reflection", @@ -1392,7 +1359,6 @@ cc_test( ], deps = [ ":config", - ":hash", ":value", ":value_testing", "//cel-c:duration", @@ -1402,9 +1368,9 @@ cc_test( "//cel-c:string_view", "//cel-c:string_view_absl", "//cel-c:timestamp", - "//cel-c:type", "//cel-c:value", "//cel-c:value_kind", + "@abseil-cpp//absl/hash", "@abseil-cpp//absl/strings:string_view", "@googletest//:gtest_main", "@protobuf//upb/base", diff --git a/cel-c/internal/flat_hash_map_test.cc b/cel-c/internal/flat_hash_map_test.cc index 50ae05d..cffde31 100644 --- a/cel-c/internal/flat_hash_map_test.cc +++ b/cel-c/internal/flat_hash_map_test.cc @@ -18,19 +18,15 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "absl/hash/hash.h" #include "cel-c/alloc.h" #include "cel-c/internal/config.h" -#include "cel-c/internal/hash.h" namespace { using ::testing::Test; -size_t IntHasher(CEL_NONNULL(const int*) ent) { - _cel_HashState state = _cel_HashState_Initialize(); - state = _cel_HashState_Combine(state, *ent); - return _cel_HashState_Finalize(state); -} +size_t IntHasher(CEL_NONNULL(const int*) ent) { return absl::HashOf(*ent); } bool IntEqualer(CEL_NONNULL(const int*) lhs, CEL_NONNULL(const int*) rhs) { return *lhs == *rhs; diff --git a/cel-c/internal/flat_hash_set_test.cc b/cel-c/internal/flat_hash_set_test.cc index dd3e3f2..90e6438 100644 --- a/cel-c/internal/flat_hash_set_test.cc +++ b/cel-c/internal/flat_hash_set_test.cc @@ -18,19 +18,15 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "absl/hash/hash.h" #include "cel-c/alloc.h" #include "cel-c/internal/config.h" -#include "cel-c/internal/hash.h" namespace { using ::testing::Test; -size_t IntHasher(CEL_NONNULL(const int*) ent) { - _cel_HashState state = _cel_HashState_Initialize(); - state = _cel_HashState_Combine(state, *ent); - return _cel_HashState_Finalize(state); -} +size_t IntHasher(CEL_NONNULL(const int*) ent) { return absl::HashOf(*ent); } bool IntEqualer(CEL_NONNULL(const int*) lhs, CEL_NONNULL(const int*) rhs) { return *lhs == *rhs; diff --git a/cel-c/internal/hash.cc b/cel-c/internal/hash.cc deleted file mode 100644 index 39934f3..0000000 --- a/cel-c/internal/hash.cc +++ /dev/null @@ -1,252 +0,0 @@ -// Copyright 2025 Google LLC -// -// 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 -// -// https://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 "cel-c/internal/hash.h" - -#include -#include -#include - -#include "absl/numeric/int128.h" -#include "cel-c/assert.h" -#include "cel-c/internal/bit.h" -#include "cel-c/internal/config.h" -#include "cel-c/internal/unaligned.h" - -static CEL_NONNULL(const void*) const _cel_kHashSeed = &_cel_kHashSeed; - -CEL_ATTRIBUTE_NODISCARD -static uint64_t _cel_HashSeed() { -#if (!defined(__clang__) || __clang_major__ > 11) && \ - (!defined(__apple_build_version__) || __apple_build_version__ >= 19558921) - return (uint64_t)((uintptr_t)&_cel_kHashSeed); -#else - return (uint64_t)((uintptr_t)_cel_kHashSeed); -#endif -} - -extern "C" _cel_HashState _cel_HashState_Initialize() { - return ((_cel_HashState){_cel_HashSeed()}); -} - -static void _cel_HashRead9To16(const uint8_t* cel_nonnull p, size_t n, - uint64_t* cel_nonnull low, - uint64_t* cel_nonnull high) { - const uint64_t lo = _cel_UnalignedLoad64(p); - const uint64_t hi = _cel_UnalignedLoad64(p + n - 8); -#ifdef _CEL_IS_LITTLE_ENDIAN - const uint64_t most_significant = hi; - const uint64_t least_significant = lo; -#else - const uint64_t most_significant = lo; - const uint64_t least_significant = hi; -#endif - *low = least_significant; - *high = most_significant; -} - -CEL_ATTRIBUTE_NODISCARD -static uint64_t _cel_HashRead4To8(const uint8_t* cel_nonnull p, size_t n) { - const uint32_t low = _cel_UnalignedLoad32(p); - const uint32_t hi = _cel_UnalignedLoad32(p + n - 4); -#ifdef _CEL_IS_LITTLE_ENDIAN - const uint32_t most_significant = hi; - const uint32_t least_significant = low; -#else - const uint32_t most_significant = low; - const uint32_t least_significant = hi; -#endif - return (((uint64_t)most_significant) << (n - 4) * 8) | least_significant; -} - -CEL_ATTRIBUTE_NODISCARD -static uint32_t _cel_HashRead1To3(const uint8_t* cel_nonnull p, size_t n) { - const uint8_t mem0 = p[0]; - const uint8_t mem1 = p[n / 2]; - const uint8_t mem2 = p[n - 1]; -#ifdef _CEL_IS_LITTLE_ENDIAN - const uint8_t significant2 = mem2; - const uint8_t significant1 = mem1; - const uint8_t significant0 = mem0; -#else - const uint8_t significant2 = mem0; - const uint8_t significant1 = n == 2 ? mem0 : mem1; - const uint8_t significant0 = mem2; -#endif - return (uint32_t)(significant0 | (significant1 << (n / 2 * 8)) | - (significant2 << ((n - 1) * 8))); -} - -CEL_ATTRIBUTE_NODISCARD -static uint64_t _cel_HashMix(uint64_t a, uint64_t b) { - absl::uint128 m = a + b; - m *= UINT64_C(0x9ddfea08eb382d69); - m ^= m >> 64; - return static_cast(m); -} - -static const uint64_t _cel_kHashSalt[5] = { - UINT64_C(0x243F6A8885A308D3), UINT64_C(0x13198A2E03707344), - UINT64_C(0xA4093822299F31D0), UINT64_C(0x082EFA98EC4E6C89), - UINT64_C(0x452821E638D01377), -}; - -CEL_ATTRIBUTE_NODISCARD -static uint64_t _cel_LowLevelHashMix(uint64_t a, uint64_t b) { - absl::uint128 m = a; - m *= b; - return static_cast(m) ^ static_cast(m >> 64); -} - -CEL_ATTRIBUTE_NODISCARD -static uint64_t _cel_LowLevelHashN(const uint8_t* cel_nonnull p, size_t n) { - CEL_ASSERT_GT(n, 16); - const size_t starting_n = n; - const uint8_t* const last_16_p = p + starting_n - 16; - uint64_t state = _cel_HashSeed() ^ _cel_kHashSalt[0]; - - if (n > 64) { - uint64_t dup_state[3]; - dup_state[0] = state; - dup_state[1] = state; - dup_state[2] = state; - - do { - const uint64_t a = _cel_UnalignedLoad64(p); - const uint64_t b = _cel_UnalignedLoad64(p + 8); - const uint64_t c = _cel_UnalignedLoad64(p + 16); - const uint64_t d = _cel_UnalignedLoad64(p + 24); - const uint64_t e = _cel_UnalignedLoad64(p + 32); - const uint64_t f = _cel_UnalignedLoad64(p + 40); - const uint64_t g = _cel_UnalignedLoad64(p + 48); - const uint64_t h = _cel_UnalignedLoad64(p + 56); - - state = _cel_LowLevelHashMix(a ^ _cel_kHashSalt[1], b ^ state); - dup_state[0] = - _cel_LowLevelHashMix(c ^ _cel_kHashSalt[2], d ^ dup_state[0]); - dup_state[1] = - _cel_LowLevelHashMix(e ^ _cel_kHashSalt[3], f ^ dup_state[1]); - dup_state[2] = - _cel_LowLevelHashMix(g ^ _cel_kHashSalt[4], h ^ dup_state[2]); - - p += 64; - n -= 64; - } while (n > 64); - - state = (state ^ dup_state[0]) ^ (dup_state[1] + dup_state[2]); - } - - if (n > 32) { - uint64_t dup_state[2]; - - const uint64_t a = _cel_UnalignedLoad64(p); - const uint64_t b = _cel_UnalignedLoad64(p + 8); - const uint64_t c = _cel_UnalignedLoad64(p + 16); - const uint64_t d = _cel_UnalignedLoad64(p + 24); - - dup_state[0] = _cel_LowLevelHashMix(a ^ _cel_kHashSalt[1], b ^ state); - dup_state[1] = _cel_LowLevelHashMix(c ^ _cel_kHashSalt[2], d ^ state); - state = dup_state[0] ^ dup_state[1]; - - p += 32; - n -= 32; - } - - if (n > 16) { - const uint64_t a = _cel_UnalignedLoad64(p); - const uint64_t b = _cel_UnalignedLoad64(p + 8); - - state = _cel_LowLevelHashMix(a ^ _cel_kHashSalt[1], b ^ state); - } - - const uint64_t a = _cel_UnalignedLoad64(last_16_p); - const uint64_t b = _cel_UnalignedLoad64(last_16_p + 8); - - return _cel_LowLevelHashMix(a ^ _cel_kHashSalt[1] ^ starting_n, b ^ state); -} - -extern "C" _cel_HashState _cel_HashState_CombineN(_cel_HashState state, - const void* cel_nullable data, - size_t size) { - uint64_t v; - if (size > 16) { - if (CEL_UNLIKELY(size > 1024)) { - do { - state.val = _cel_HashMix( - state.val, - _cel_LowLevelHashN(reinterpret_cast(data), 1024)); - size -= 1024; - data = ((const uint8_t*)data) + 1024; - } while (size > 1024); - return _cel_HashState_CombineN(state, data, size); - } - v = _cel_LowLevelHashN((const uint8_t*)data, size); - } else if (size > 8) { - uint64_t lo; - uint64_t hi; - _cel_HashRead9To16(reinterpret_cast(data), size, &lo, &hi); - lo = _cel_rotr(lo, 53); - state.val *= UINT64_C(0x9ddfea08eb382d69); - lo += state.val; - state.val ^= hi; - absl::uint128 m = state.val; - m *= lo; - m ^= m >> 64; - return ((_cel_HashState){static_cast(m)}); - } else if (size > 3) { - v = _cel_HashRead4To8(reinterpret_cast(data), size); - } else if (size > 0) { - v = _cel_HashRead1To3(reinterpret_cast(data), size); - } else { - return state; - } - return ((_cel_HashState){_cel_HashMix(state.val, v)}); -} - -extern "C" _cel_HashState _cel_HashState_Combine64(_cel_HashState state, - uint64_t data) { - return ((_cel_HashState){_cel_HashMix(state.val, data)}); -} - -extern "C" _cel_HashState _cel_HashState_CombineLD(_cel_HashState state, - long double value) { - const int cat = std::fpclassify(value); - switch (cat) { - case FP_INFINITE: - // Add the sign bit to differentiate between +Inf and -Inf - state = _cel_HashState_CombineB(state, std::signbit(value)); - break; - case FP_NAN: - CEL_ATTRIBUTE_FALLTHROUGH; - case FP_ZERO: - CEL_ATTRIBUTE_FALLTHROUGH; - default: - // Category is enough for these. - break; - case FP_NORMAL: - CEL_ATTRIBUTE_FALLTHROUGH; - case FP_SUBNORMAL: { - // We can't convert `value` directly to double because this would have - // undefined behavior if the value is out of range. - // std::frexp gives us a value in the range (-1, -.5] or [.5, 1) that is - // guaranteed to be in range for `double`. The truncation is - // implementation defined, but that works as long as it is deterministic. - int exp; - double mant = (double)frexpl(value, &exp); - state = _cel_HashState_CombineD(state, mant); - state = _cel_HashState_CombineI(state, exp); - } break; - } - return _cel_HashState_CombineI(state, cat); -} diff --git a/cel-c/internal/hash.h b/cel-c/internal/hash.h deleted file mode 100644 index bf4f68a..0000000 --- a/cel-c/internal/hash.h +++ /dev/null @@ -1,447 +0,0 @@ -// Copyright 2025 Google LLC -// -// 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 -// -// https://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 THIRD_PARTY_CEL_C_HASH_H_ -#define THIRD_PARTY_CEL_C_HASH_H_ - -#include -#include // IWYU pragma: keep -#include -#include -#include - -#include "cel-c/config.h" -#include "cel-c/cstring_view.h" -#include "cel-c/string_view.h" -#include "upb/base/string_view.h" - -// NOLINTBEGIN(runtime/int) -// NOLINTBEGIN(google-runtime-int) - -CEL_BEGIN_DECLS - -typedef struct { - uint64_t val; -} _cel_HashState; - -// _cel_HashState_Initialize -// -// Returns an initial `_cel_HashState`. -CEL_ATTRIBUTE_NODISCARD -CEL_ATTRIBUTE_NOTHROW -CEL_ATTRIBUTE_PURE -_cel_HashState _cel_HashState_Initialize(); - -// _cel_HashState_Finalize -// -// Computes the final hash code and returns it. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE size_t _cel_HashState_Finalize(_cel_HashState state) { - return (size_t)state.val; -} - -// _cel_HashState_CombineN -// -// Combines the contiguous block of memory starting at `data` and extending -// `size` bytes into the hash state, returning the updated state. -CEL_ATTRIBUTE_NODISCARD -CEL_ATTRIBUTE_NOTHROW -_cel_HashState _cel_HashState_CombineN(_cel_HashState state, - const void* cel_nullable data, - size_t size); - -// _cel_HashState_Combine64 -// -// Combines the 64-bit value into the hash state, returning the updated state. -CEL_ATTRIBUTE_NODISCARD -CEL_ATTRIBUTE_NOTHROW -_cel_HashState _cel_HashState_Combine64(_cel_HashState state, uint64_t data); - -// _cel_HashState_Combine8 -// -// Combines the 8-bit value into the hash state, returning the updated state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_Combine8(_cel_HashState state, - uint8_t data) { - return _cel_HashState_Combine64(state, data); -} - -// _cel_HashState_Combine16 -// -// Combines the 16-bit value into the hash state, returning the updated state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_Combine16(_cel_HashState state, - uint16_t data) { - return _cel_HashState_Combine64(state, data); -} - -// _cel_HashState_Combine32 -// -// Combines the 32-bit value into the hash state, returning the updated state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_Combine32(_cel_HashState state, - uint32_t data) { - return _cel_HashState_Combine64(state, data); -} - -// _cel_HashState_CombineB -// -// Combines the bool value into the hash state, returning the updated state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_CombineB(_cel_HashState state, - bool value) { - CEL_STATIC_ASSERT(sizeof(value) == 1); - return _cel_HashState_Combine8(state, (uint8_t)(value ? 1 : 0)); -} - -// _cel_HashState_CombineUC -// -// Combines the unsigned char value into the hash state, returning the updated -// state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_CombineUC(_cel_HashState state, - unsigned char value) { - CEL_STATIC_ASSERT(sizeof(value) == 1); - return _cel_HashState_Combine8(state, (uint8_t)value); -} - -// _cel_HashState_CombineC -// -// Combines the char value into the hash state, returning the updated state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_CombineC(_cel_HashState state, - char value) { - return _cel_HashState_CombineUC(state, (unsigned char)value); -} - -// _cel_HashState_CombineSC -// -// Combines the signed char value into the hash state, returning the updated -// state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_CombineSC(_cel_HashState state, - signed char value) { - return _cel_HashState_CombineUC(state, (unsigned char)value); -} - -// _cel_HashState_CombineUS -// -// Combines the unsigned short value into the hash state, returning the updated -// state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState -_cel_HashState_CombineUS(_cel_HashState state, unsigned short value) { - CEL_STATIC_ASSERT(sizeof(value) == 2); - return _cel_HashState_Combine16(state, (uint16_t)value); -} - -// _cel_HashState_CombineS -// -// Combines the short value into the hash state, returning the updated state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_CombineS(_cel_HashState state, - short value) { - return _cel_HashState_CombineUS(state, (unsigned short)value); -} - -// _cel_HashState_CombineUI -// -// Combines the unsigned int value into the hash state, returning the updated -// state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_CombineUI(_cel_HashState state, - unsigned int value) { - CEL_STATIC_ASSERT(sizeof(value) == 4); - return _cel_HashState_Combine32(state, (uint32_t)value); -} - -// _cel_HashState_CombineI -// -// Combines the int value into the hash state, returning the updated state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_CombineI(_cel_HashState state, - int value) { - return _cel_HashState_CombineUI(state, (unsigned int)value); -} - -// _cel_HashState_CombineUL -// -// Combines the unsigned long value into the hash state, returning the updated -// state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_CombineUL(_cel_HashState state, - unsigned long value) { - CEL_STATIC_ASSERT(sizeof(value) == 4 || sizeof(value) == 8); -#if LONG_MAX == INT32_MAX - return _cel_HashState_Combine32(state, (uint32_t)value); -#elif LONG_MAX == INT64_MAX - return _cel_HashState_Combine64(state, (uint64_t)value); -#else -#error Unreachable. -#endif -} - -// _cel_HashState_CombineL -// -// Combines the long value into the hash state, returning the updated state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_CombineL(_cel_HashState state, - long value) { - return _cel_HashState_CombineUL(state, (unsigned long)value); -} - -// _cel_HashState_CombineULL -// -// Combines the unsigned long long value into the hash state, returning the -// updated state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState -_cel_HashState_CombineULL(_cel_HashState state, unsigned long long value) { - CEL_STATIC_ASSERT(sizeof(value) == 8); - return _cel_HashState_Combine64(state, (uint64_t)value); -} - -// _cel_HashState_CombineLL -// -// Combines the long long value into the hash state, returning the updated -// state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_CombineLL(_cel_HashState state, - long long value) { - return _cel_HashState_CombineULL(state, (unsigned long long)value); -} - -// _cel_HashState_CombineF -// -// Combines the float value into the hash state, returning the updated state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_CombineF(_cel_HashState state, - float value) { - CEL_STATIC_ASSERT(sizeof(value) == sizeof(uint32_t)); - union { - float f; - uint32_t u; - } data = {.f = (value != 0.0f ? value : 0.0f)}; - return _cel_HashState_Combine32(state, data.u); -} - -// _cel_HashState_CombineD -// -// Combines the double value into the hash state, returning the updated state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_CombineD(_cel_HashState state, - double value) { - CEL_STATIC_ASSERT(sizeof(value) == sizeof(uint64_t)); - union { - double d; - uint64_t u; - } data = {.d = (value != 0.0 ? value : 0.0)}; - return _cel_HashState_Combine64(state, data.u); -} - -// _cel_HashState_CombineLD -// -// Combines the long double value into the hash state, returning the updated -// state. -CEL_ATTRIBUTE_NODISCARD -CEL_ATTRIBUTE_NOTHROW -_cel_HashState _cel_HashState_CombineLD(_cel_HashState state, - long double value); - -// _cel_HashState_CombineStr -// -// Combines the C string into the hash state, returning the updated state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState -_cel_HashState_CombineStr(_cel_HashState state, const char* cel_nullable str) { - return _cel_HashState_CombineN(state, str, - str != cel_nullptr ? strlen(str) : 0); -} - -// _cel_HashState_CombineMem -// -// Combines the upb string into the hash state, returning the updated state. -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState _cel_HashState_CombineMem(_cel_HashState state, - upb_StringView mem) { - return _cel_HashState_CombineN(state, mem.data, mem.size); -} - -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState cel_StringView_Hash(cel_StringView str, - _cel_HashState state) { - return _cel_HashState_CombineN(state, cel_StringView_Data(str), - cel_StringView_Size(str)); -} - -CEL_ATTRIBUTE_NODISCARD -static CEL_INLINE _cel_HashState cel_CStringView_Hash(cel_CStringView str, - _cel_HashState state) { - return _cel_HashState_CombineN(state, cel_CStringView_Data(str), - cel_CStringView_Size(str)); -} - -CEL_END_DECLS - -// _cel_HashState_Combine -// -// Combines the value into the hash state, returning the updated state. -#ifndef __cplusplus -#define _cel_HashState_Combine(state, x) \ - (_Generic((x), \ - bool: _cel_HashState_CombineB, \ - char: _cel_HashState_CombineC, \ - signed char: _cel_HashState_CombineSC, \ - unsigned char: _cel_HashState_CombineUC, \ - short: _cel_HashState_CombineS, \ - unsigned short: _cel_HashState_CombineUS, \ - int: _cel_HashState_CombineI, \ - unsigned int: _cel_HashState_CombineUI, \ - long: _cel_HashState_CombineL, \ - unsigned long: _cel_HashState_CombineUL, \ - long long: _cel_HashState_CombineLL, \ - unsigned long long: _cel_HashState_CombineULL, \ - float: _cel_HashState_CombineF, \ - double: _cel_HashState_CombineD, \ - long double: _cel_HashState_CombineLD, \ - char*: _cel_HashState_CombineStr, \ - const char*: _cel_HashState_CombineStr, \ - upb_StringView: _cel_HashState_CombineMem)((state), (x))) -#else -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - bool val) { - return _cel_HashState_CombineB(state, val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - char val) { - return _cel_HashState_CombineC(state, val); -} -#if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - char8_t val) { - CEL_STATIC_ASSERT(sizeof(val) == sizeof(unsigned char)); - return _cel_HashState_CombineUC(state, (unsigned char)val); -} -#endif -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - char16_t val) { - CEL_STATIC_ASSERT(sizeof(val) == sizeof(unsigned short)); - return _cel_HashState_CombineUS(state, (unsigned short)val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - char32_t val) { - CEL_STATIC_ASSERT(sizeof(val) == sizeof(unsigned int)); - return _cel_HashState_CombineUI(state, (unsigned int)val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - signed char val) { - return _cel_HashState_CombineSC(state, val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - unsigned char val) { - return _cel_HashState_CombineUC(state, val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - short val) { - return _cel_HashState_CombineS(state, val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - unsigned short val) { - return _cel_HashState_CombineUS(state, val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - int val) { - return _cel_HashState_CombineI(state, val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - unsigned int val) { - return _cel_HashState_CombineUI(state, val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - long val) { - return _cel_HashState_CombineL(state, val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - unsigned long val) { - return _cel_HashState_CombineUL(state, val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - long long val) { - return _cel_HashState_CombineLL(state, val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - unsigned long long val) { - return _cel_HashState_CombineULL(state, val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - float val) { - return _cel_HashState_CombineF(state, val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - double val) { - return _cel_HashState_CombineD(state, val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - long double val) { - return _cel_HashState_CombineLD(state, val); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - const char* cel_nullable str) { - return _cel_HashState_CombineStr(state, str); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - upb_StringView mem) { - return _cel_HashState_CombineMem(state, mem); -} -CEL_ATTRIBUTE_NODISCARD -CEL_INLINE _cel_HashState _cel_HashState_Combine(_cel_HashState state, - wchar_t val) { - CEL_STATIC_ASSERT(sizeof(wchar_t) == 1 || sizeof(wchar_t) == 2 || - sizeof(wchar_t) == 4 || sizeof(wchar_t) == 8); - if constexpr (sizeof(wchar_t) == 1) { - return _cel_HashState_Combine(state, (uint8_t)val); - } else if constexpr (sizeof(wchar_t) == 2) { - return _cel_HashState_Combine(state, (uint16_t)val); - } else if constexpr (sizeof(wchar_t) == 4) { - return _cel_HashState_Combine(state, (uint32_t)val); - } else { - return _cel_HashState_Combine(state, (uint64_t)val); - } -} -#endif - -// NOLINTEND(google-runtime-int) -// NOLINTEND(runtime/int) - -#endif // THIRD_PARTY_CEL_C_HASH_H_ diff --git a/cel-c/internal/hash_test.cc b/cel-c/internal/hash_test.cc deleted file mode 100644 index 6e78f6b..0000000 --- a/cel-c/internal/hash_test.cc +++ /dev/null @@ -1,183 +0,0 @@ -// Copyright 2025 Google LLC -// -// 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 -// -// https://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 "cel-c/internal/hash.h" - -#include - -#include "gtest/gtest.h" -#include "cel-c/cstring_view.h" -#include "cel-c/string_view.h" -#include "upb/base/string_view.h" - -// NOLINTBEGIN(runtime/int) -// NOLINTBEGIN(google-runtime-int) - -inline bool operator==(_cel_HashState lhs, _cel_HashState rhs) { - return lhs.val == rhs.val; -} - -inline bool operator!=(_cel_HashState lhs, _cel_HashState rhs) { - return !operator==(lhs, rhs); -} - -namespace { - -TEST(Hash, Empty) { - EXPECT_EQ(_cel_HashState_Initialize(), _cel_HashState_Initialize()); - EXPECT_EQ(_cel_HashState_Finalize(_cel_HashState_Initialize()), - _cel_HashState_Finalize(_cel_HashState_Initialize())); -} - -TEST(Hash, Bool) { - EXPECT_EQ(_cel_HashState_Combine(_cel_HashState_Initialize(), false), - _cel_HashState_Combine(_cel_HashState_Initialize(), false)); - EXPECT_EQ(_cel_HashState_Combine(_cel_HashState_Initialize(), true), - _cel_HashState_Combine(_cel_HashState_Initialize(), true)); -} - -template -class HashIntTest : public ::testing::Test {}; - -using HashIntTestTypes = - ::testing::Types; - -TYPED_TEST_SUITE(HashIntTest, HashIntTestTypes); - -TYPED_TEST(HashIntTest, Hash) { - using TypeParamLimits = std::numeric_limits; - EXPECT_EQ(_cel_HashState_Combine(_cel_HashState_Initialize(), TypeParam{0}), - _cel_HashState_Combine(_cel_HashState_Initialize(), TypeParam{0})); - EXPECT_EQ(_cel_HashState_Combine(_cel_HashState_Initialize(), TypeParam{1}), - _cel_HashState_Combine(_cel_HashState_Initialize(), TypeParam{1})); - EXPECT_EQ(_cel_HashState_Combine(_cel_HashState_Initialize(), - TypeParamLimits::min()), - _cel_HashState_Combine(_cel_HashState_Initialize(), - TypeParamLimits::min())); - EXPECT_EQ(_cel_HashState_Combine(_cel_HashState_Initialize(), - TypeParamLimits::max()), - _cel_HashState_Combine(_cel_HashState_Initialize(), - TypeParamLimits::max())); -} - -template -class HashFloatTest : public ::testing::Test {}; - -using HashFloatTestTypes = ::testing::Types; - -TYPED_TEST_SUITE(HashFloatTest, HashFloatTestTypes); - -TYPED_TEST(HashFloatTest, Hash) { - using Limits = std::numeric_limits; - EXPECT_EQ( - _cel_HashState_Combine(_cel_HashState_Initialize(), TypeParam{0.0}), - _cel_HashState_Combine(_cel_HashState_Initialize(), TypeParam{0.0})); - EXPECT_EQ( - _cel_HashState_Combine(_cel_HashState_Initialize(), Limits::infinity()), - _cel_HashState_Combine(_cel_HashState_Initialize(), Limits::infinity())); - EXPECT_EQ( - _cel_HashState_Combine(_cel_HashState_Initialize(), -Limits::infinity()), - _cel_HashState_Combine(_cel_HashState_Initialize(), -Limits::infinity())); - EXPECT_EQ( - _cel_HashState_Combine(_cel_HashState_Initialize(), Limits::quiet_NaN()), - _cel_HashState_Combine(_cel_HashState_Initialize(), Limits::quiet_NaN())); - EXPECT_EQ( - _cel_HashState_Combine(_cel_HashState_Initialize(), TypeParam{-0.0}), - _cel_HashState_Combine(_cel_HashState_Initialize(), TypeParam{0.0})); - EXPECT_EQ( - _cel_HashState_Combine(_cel_HashState_Initialize(), TypeParam{1.0}), - _cel_HashState_Combine(_cel_HashState_Initialize(), TypeParam{1.0})); -} - -TEST(Hash, Str) { - EXPECT_EQ( - _cel_HashState_Combine(_cel_HashState_Initialize(), "Hello World!"), - _cel_HashState_Combine(_cel_HashState_Initialize(), "Hello World!")); - EXPECT_EQ(_cel_HashState_Combine(_cel_HashState_Initialize(), "Hello World!"), - _cel_HashState_Combine(_cel_HashState_Initialize(), - upb_StringView_FromString("Hello World!"))); - EXPECT_EQ( - _cel_HashState_Combine(_cel_HashState_Initialize(), (const char*)nullptr), - _cel_HashState_Combine(_cel_HashState_Initialize(), "")); -} - -TEST(Hash, Mem) { - upb_StringView small = upb_StringView_FromString("Hello World!"); - EXPECT_EQ(_cel_HashState_Combine(_cel_HashState_Initialize(), small), - _cel_HashState_Combine(_cel_HashState_Initialize(), small)); - EXPECT_EQ(_cel_HashState_Combine(_cel_HashState_Initialize(), small), - _cel_HashState_Combine(_cel_HashState_Initialize(), small)); - upb_StringView large = upb_StringView_FromString( - "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus " - "condimentum rhoncus est volutpat venenatis. Fusce semper, sapien ut " - "venenatis pellentesque, lorem dui aliquam sapien, non pharetra diam " - "neque id mi. Suspendisse sollicitudin, metus ut gravida semper, nunc " - "ipsum ullamcorper nunc, ut maximus nulla nunc dignissim justo. Duis nec " - "nisi leo. Proin tristique massa mi, imperdiet tempus nibh vulputate " - "quis. Morbi sagittis eget neque sagittis egestas. Quisque viverra arcu " - "a cursus dignissim. In aliquam, mi ut laoreet varius, ex ante posuere " - "justo, eget aliquam magna metus id purus. Aenean convallis sem ac purus " - "bibendum, sit amet mattis augue fermentum. Quisque cursus posuere mi, " - "vitae vestibulum purus egestas eget. Nunc eu sagittis est, at elementum " - "leo. Pellentesque habitant morbi tristique senectus et netus et " - "malesuada fames ac turpis egestas. Nulla interdum lacus a turpis " - "maximus, scelerisque aliquam magna ultricies. Aliquam erat volutpat. " - "Mauris eget tellus sed velit aliquet venenatis ut nec eros. Nulla " - "facilisi. Sed posuere nisi quis felis varius, ut maximus elit placerat. " - "Sed vulputate quam augue, ac lacinia diam ullamcorper at. Fusce " - "efficitur fermentum mi, sed imperdiet ipsum dignissim eu. Nam semper " - "quis ex eget iaculis. In a diam dolor. Integer fermentum in arcu " - "commodo tempus. Phasellus ultricies gravida ante ac tristique. " - "Vestibulum volutpat id neque a porttitor. Donec ullamcorper sed augue " - "eget efficitur. Ut vitae aliquet lorem, quis ultrices neque. Fusce " - "blandit ac mauris vel porttitor. Pellentesque eget ultrices augue. " - "Pellentesque tempor maximus nunc sit amet ullamcorper. Nulla non " - "vehicula magna. In eget condimentum nisi, sit amet auctor purus. Proin " - "venenatis lorem non mattis interdum. Aenean metus leo, lobortis non " - "auctor eget, bibendum at mi. Aenean mauris est, convallis efficitur " - "molestie et, suscipit in dolor. Duis eget volutpat est. Fusce id " - "maximus arcu, et tincidunt purus. In efficitur volutpat est nec " - "eleifend. Proin malesuada pharetra mattis. Donec sed arcu sit amet nisi " - "vulputate ornare. Integer accumsan ullamcorper mauris, eget semper " - "ipsum. Suspendisse sagittis pellentesque molestie. Duis at venenatis " - "erat. Quisque congue turpis id erat egestas pellentesque. Nulla " - "fermentum ac massa nec sollicitudin. Fusce sed ultrices tellus."); - EXPECT_EQ(_cel_HashState_Combine(_cel_HashState_Initialize(), large), - _cel_HashState_Combine(_cel_HashState_Initialize(), large)); - EXPECT_EQ(_cel_HashState_Combine(_cel_HashState_Initialize(), large), - _cel_HashState_Combine(_cel_HashState_Initialize(), large)); -} - -TEST(StringView, Hash) { - EXPECT_EQ(_cel_HashState_Finalize( - cel_StringView_Hash(cel_StringView_FromString("Hello World!"), - _cel_HashState_Initialize())), - _cel_HashState_Finalize(_cel_HashState_Combine( - _cel_HashState_Initialize(), "Hello World!"))); -} - -TEST(CStringView, Hash) { - EXPECT_EQ(_cel_HashState_Finalize( - cel_CStringView_Hash(cel_CStringView_FromString("Hello World!"), - _cel_HashState_Initialize())), - _cel_HashState_Finalize(_cel_HashState_Combine( - _cel_HashState_Initialize(), "Hello World!"))); -} - -} // namespace - -// NOLINTEND(google-runtime-int) -// NOLINTEND(runtime/int) diff --git a/cel-c/internal/map_value.h b/cel-c/internal/map_value.h index a3ef907..a3142c2 100644 --- a/cel-c/internal/map_value.h +++ b/cel-c/internal/map_value.h @@ -15,10 +15,10 @@ #ifndef THIRD_PARTY_CEL_C_INTERNAL_MAP_VALUE_H_ #define THIRD_PARTY_CEL_C_INTERNAL_MAP_VALUE_H_ -#include +#include +#include #include "cel-c/internal/config.h" -#include "cel-c/internal/hash.h" #include "cel-c/string_view.h" #include "cel-c/value.h" #include "upb/base/descriptor_constants.h" @@ -192,41 +192,6 @@ static inline bool _cel_MapValueKey_Equals( } } -CEL_ATTRIBUTE_NODISCARD -static inline _cel_HashState _cel_MapValueKey_Hash( - const cel_MapValueKey* cel_nonnull map_value_key, _cel_HashState state) { - switch (cel_MapValueKey_Kind(map_value_key)) { - case cel_MapValueKeyKind_kBool: - state = _cel_HashState_Combine(state, cel_MapValueKeyKind_kBool); - state = - _cel_HashState_Combine(state, cel_MapValueKey_GetBool(map_value_key)); - break; - case cel_MapValueKeyKind_kInt: { - const int64_t value = cel_MapValueKey_GetInt(map_value_key); - if (value < 0) { - state = _cel_HashState_Combine(state, cel_MapValueKeyKind_kInt); - state = _cel_HashState_Combine(state, value); - } else { - state = _cel_HashState_Combine(state, cel_MapValueKeyKind_kUint); - state = _cel_HashState_Combine(state, (uint64_t)value); - } - } break; - case cel_MapValueKeyKind_kUint: - state = _cel_HashState_Combine(state, cel_MapValueKeyKind_kUint); - state = - _cel_HashState_Combine(state, cel_MapValueKey_GetUint(map_value_key)); - break; - case cel_MapValueKeyKind_kString: - state = _cel_HashState_Combine(state, cel_MapValueKeyKind_kString); - state = - cel_StringView_Hash(cel_MapValueKey_GetString(map_value_key), state); - break; - default: - CEL_UNREACHABLE(); - } - return state; -} - static inline void _cel_Value_SetMapValueKey(cel_Value* cel_nonnull value, const cel_MapValueKey* cel_nonnull map_value_key) { @@ -378,4 +343,40 @@ static inline void _cel_MapValueKey_FromMessageValue( CEL_END_DECLS +template +H AbslHashValue(H state, const cel_MapValueKey& map_value_key) { + switch (cel_MapValueKey_Kind(&map_value_key)) { + case cel_MapValueKeyKind_kBool: + state = H::combine(std::move(state), cel_MapValueKeyKind_kBool); + state = + H::combine(std::move(state), cel_MapValueKey_GetBool(&map_value_key)); + break; + case cel_MapValueKeyKind_kInt: { + const int64_t value = cel_MapValueKey_GetInt(&map_value_key); + if (value < 0) { + state = H::combine(std::move(state), cel_MapValueKeyKind_kInt); + state = H::combine(std::move(state), value); + } else { + state = H::combine(std::move(state), cel_MapValueKeyKind_kUint); + state = H::combine(std::move(state), (uint64_t)value); + } + } break; + case cel_MapValueKeyKind_kUint: + state = H::combine(std::move(state), cel_MapValueKeyKind_kUint); + state = + H::combine(std::move(state), cel_MapValueKey_GetUint(&map_value_key)); + break; + case cel_MapValueKeyKind_kString: + state = H::combine(std::move(state), cel_MapValueKeyKind_kString); + state = H::combine_contiguous( + std::move(state), + cel_StringView_Data(cel_MapValueKey_GetString(&map_value_key)), + cel_StringView_Size(cel_MapValueKey_GetString(&map_value_key))); + break; + default: + CEL_UNREACHABLE(); + } + return state; +} + #endif // THIRD_PARTY_CEL_C_INTERNAL_MAP_VALUE_H_ diff --git a/cel-c/internal/map_value_test.cc b/cel-c/internal/map_value_test.cc index 38af295..701e80c 100644 --- a/cel-c/internal/map_value_test.cc +++ b/cel-c/internal/map_value_test.cc @@ -19,11 +19,11 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "absl/hash/hash.h" #include "cel-c/error.h" #include "cel-c/error_code.h" #include "cel-c/internal/config.h" #include "cel-c/internal/empty_map_value.h" -#include "cel-c/internal/hash.h" #include "cel-c/internal/value_testing.h" #include "cel-c/status.h" #include "cel-c/string_view.h" @@ -310,10 +310,7 @@ TEST_F(MapValueKeyTest, Hash_Bool) { cel_MapValueKey key; cel_MapValueKey_SetBool(&key, true); - EXPECT_EQ(_cel_HashState_Finalize( - _cel_MapValueKey_Hash(&key, _cel_HashState_Initialize())), - _cel_HashState_Finalize( - _cel_MapValueKey_Hash(&key, _cel_HashState_Initialize()))); + EXPECT_EQ(absl::HashOf(key), absl::HashOf(key)); } TEST_F(MapValueKeyTest, Hash_Int) { @@ -322,20 +319,14 @@ TEST_F(MapValueKeyTest, Hash_Int) { cel_MapValueKey_SetInt(&int_key, 1); cel_MapValueKey_SetUint(&uint_key, 1); - EXPECT_EQ(_cel_HashState_Finalize( - _cel_MapValueKey_Hash(&int_key, _cel_HashState_Initialize())), - _cel_HashState_Finalize( - _cel_MapValueKey_Hash(&uint_key, _cel_HashState_Initialize()))); + EXPECT_EQ(absl::HashOf(int_key), absl::HashOf(uint_key)); } TEST_F(MapValueKeyTest, Hash_String) { cel_MapValueKey key; cel_MapValueKey_SetString(&key, cel_StringView_FromString("foo")); - EXPECT_EQ(_cel_HashState_Finalize( - _cel_MapValueKey_Hash(&key, _cel_HashState_Initialize())), - _cel_HashState_Finalize( - _cel_MapValueKey_Hash(&key, _cel_HashState_Initialize()))); + EXPECT_EQ(absl::HashOf(key), absl::HashOf(key)); } TEST_F(MapValueKeyTest, ToMessageValue_Bool) { diff --git a/cel-c/internal/mutable_map_value.cc b/cel-c/internal/mutable_map_value.cc index fe9c9c4..4664fff 100644 --- a/cel-c/internal/mutable_map_value.cc +++ b/cel-c/internal/mutable_map_value.cc @@ -20,6 +20,7 @@ #include #include +#include "absl/hash/hash.h" #include "cel-c/alloc.h" #include "cel-c/arena.h" #include "cel-c/assert.h" @@ -28,7 +29,6 @@ #include "cel-c/internal/bit.h" #include "cel-c/internal/config.h" #include "cel-c/internal/empty_map_value.h" -#include "cel-c/internal/hash.h" #include "cel-c/internal/map_value.h" #include "cel-c/status.h" #include "cel-c/string_view.h" @@ -140,9 +140,7 @@ static bool _cel_MutableMapValue_Get( content.ptr[_cel_MutableMapValueContent_kPtrOffset]; const size_t mask = (size_t)content.u32[_cel_MutableMapValueContent_kCapOffset] - 1; - size_t bit = _cel_HashState_Finalize( - _cel_MapValueKey_Hash(key, _cel_HashState_Initialize())) & - mask; + size_t bit = absl::HashOf(*key) & mask; _cel_MutableMapValueEntry* ent; for (ent = ents[bit]; ent != cel_nullptr; ent = ents[(++bit) & mask]) { if (_cel_MapValueKey_Equals(&ent->key, key)) { @@ -185,9 +183,7 @@ static bool _cel_MutableMapValue_Find( content.ptr[_cel_MutableMapValueContent_kPtrOffset]; const size_t mask = (size_t)content.u32[_cel_MutableMapValueContent_kCapOffset] - 1; - size_t bit = _cel_HashState_Finalize( - _cel_MapValueKey_Hash(key, _cel_HashState_Initialize())) & - mask; + size_t bit = absl::HashOf(*key) & mask; _cel_MutableMapValueEntry* ent; for (ent = ents[bit]; ent != cel_nullptr; ent = ents[(++bit) & mask]) { if (_cel_MapValueKey_Equals(&ent->key, key)) { @@ -222,9 +218,7 @@ static bool _cel_MutableMapValue_Has( content.ptr[_cel_MutableMapValueContent_kPtrOffset]; const size_t mask = (size_t)content.u32[_cel_MutableMapValueContent_kCapOffset] - 1; - size_t bit = _cel_HashState_Finalize( - _cel_MapValueKey_Hash(key, _cel_HashState_Initialize())) & - mask; + size_t bit = absl::HashOf(*key) & mask; _cel_MutableMapValueEntry* ent; for (ent = ents[bit]; ent != cel_nullptr; ent = ents[(++bit) & mask]) { if (_cel_MapValueKey_Equals(&ent->key, key)) { @@ -243,16 +237,13 @@ static void _cel_MutableMapValue_Rehash(_cel_MutableMapValueEntry** new_ents, _cel_MutableMapValueEntry** old_ents, size_t old_cap, size_t len) { const size_t new_mask = new_cap - 1; - const _cel_HashState state = _cel_HashState_Initialize(); size_t old_bit; for (old_bit = 0; len > 0; ++old_bit) { _cel_MutableMapValueEntry* ent = old_ents[old_bit]; if (ent == cel_nullptr) { continue; } - size_t new_bit = - _cel_HashState_Finalize(_cel_MapValueKey_Hash(&ent->key, state)) & - new_mask; + size_t new_bit = absl::HashOf(ent->key) & new_mask; for (; new_ents[new_bit] != cel_nullptr; new_bit = (new_bit + 1) & new_mask) { } @@ -341,9 +332,7 @@ extern "C" _cel_MutableMapValueInsertResult _cel_MutableMapValue_Insert( const size_t mask = (size_t)map_value->content.u32[_cel_MutableMapValueContent_kCapOffset] - 1; - size_t bit = _cel_HashState_Finalize( - _cel_MapValueKey_Hash(key, _cel_HashState_Initialize())) & - mask; + size_t bit = absl::HashOf(*key) & mask; _cel_MutableMapValueEntry* ent; for (ent = ents[bit]; ent != cel_nullptr; ent = ents[(++bit) & mask]) { if (_cel_MapValueKey_Equals(&ent->key, key)) { diff --git a/cel-c/internal/runtime/BUILD b/cel-c/internal/runtime/BUILD index 3aed1c8..0d9a509 100644 --- a/cel-c/internal/runtime/BUILD +++ b/cel-c/internal/runtime/BUILD @@ -48,6 +48,7 @@ cc_library( "//cel-c:runtime_headers", "//cel-c:status", "//cel-c:string_view", + "//cel-c:string_view_absl", "//cel-c:timestamp", "//cel-c:trilean", "//cel-c:type", @@ -68,7 +69,6 @@ cc_library( "//cel-c/internal:deque", "//cel-c/internal:durationconv", "//cel-c/internal:flat_hash_map", - "//cel-c/internal:hash", "//cel-c/internal:number", "//cel-c/internal:ref", "//cel-c/internal:regexp", @@ -78,6 +78,7 @@ cc_library( "//cel-c/internal:trivial_relocation", "//cel-c/internal:utf8", "//cel-c/internal:value", + "@abseil-cpp//absl/hash", "@protobuf//upb/reflection", ], ) diff --git a/cel-c/internal/runtime/interpreter.cc b/cel-c/internal/runtime/interpreter.cc index 809e184..4234263 100644 --- a/cel-c/internal/runtime/interpreter.cc +++ b/cel-c/internal/runtime/interpreter.cc @@ -20,6 +20,7 @@ #include +#include "absl/hash/hash.h" #include "cel-c/alloc.h" #include "cel-c/assert.h" #include "cel-c/internal/array.h" @@ -31,7 +32,6 @@ #include "cel-c/internal/container.h" #include "cel-c/internal/deque.h" #include "cel-c/internal/flat_hash_map.h" -#include "cel-c/internal/hash.h" #include "cel-c/internal/ref.h" #include "cel-c/internal/runtime/instr.h" #include "cel-c/internal/runtime/program.h" @@ -42,6 +42,7 @@ #include "cel-c/operators.h" #include "cel-c/status.h" #include "cel-c/string_view.h" +#include "cel-c/string_view_absl.h" #include "cel-c/type.h" #include "cel-c/well_known_types.h" #include "upb/reflection/def.h" @@ -1890,8 +1891,7 @@ static const cel_AstVisitorVTable _cel_InterpreterVTable = { }; static size_t _cel_Interpreter_StringViewHasher(const void* cel_nonnull key) { - return _cel_HashState_Finalize(_cel_HashState_Combine( - _cel_HashState_Initialize(), *(const cel_StringView*)key)); + return absl::HashOf(cel_StringView_ToAbsl(*(const cel_StringView*)key)); } static bool _cel_Interpreter_StringViewEqualer(const void* cel_nonnull lhs,