-
Notifications
You must be signed in to change notification settings - Fork 500
[SDK] custom hash and equality for attribute processor #3643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
marcalff
merged 19 commits into
open-telemetry:main
from
nikhilbhatia08:custom_hash_equality
Sep 23, 2025
Merged
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
faccac5
[fix]: custom hash and equality for attribute processor
nikhilbhatia08 844f1a2
StringViewHash Fix
nikhilbhatia08 7a05e7f
macos copy fix
nikhilbhatia08 f199a59
string_view fix
nikhilbhatia08 a38b2d7
more fixes
nikhilbhatia08 121bfcf
some more fix
nikhilbhatia08 53012d2
cppver fix
nikhilbhatia08 0a81688
hash fix
nikhilbhatia08 98543fc
fix
nikhilbhatia08 63b2338
code style fix
nikhilbhatia08 e5ad0c9
small fix
nikhilbhatia08 662b23f
Merge branch 'main' into custom_hash_equality
marcalff a0a5540
header fix
nikhilbhatia08 ae5eeb9
fix in test
nikhilbhatia08 4991c29
iwyu fix
nikhilbhatia08 3e8f5c7
name and typedef fix
nikhilbhatia08 5c5c3cc
Attribute map name
nikhilbhatia08 49e8152
format fix
nikhilbhatia08 7320c35
iwyu fix
nikhilbhatia08 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
sdk/include/opentelemetry/sdk/common/custom_hash_equality.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <cstring> | ||
#include <string> | ||
|
||
#include "opentelemetry/nostd/string_view.h" | ||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace common | ||
{ | ||
/** | ||
* | ||
* FNV Hashing Utilities | ||
* | ||
* Implements FNV-1a hashing algorithm for 32-bit and 64-bit types. | ||
* | ||
* - FNV (Fowler–Noll–Vo) is a simple, fast, and widely used non-cryptographic hash. | ||
* - FNV-1a is the recommended variant because it mixes input bits better. | ||
* - We parameterize by type size (4 = 32-bit, 8 = 64-bit). | ||
*/ | ||
|
||
// Forward declaration for FNV prime constants | ||
template <typename Ty, size_t Size> | ||
struct FnvPrime; | ||
|
||
// Specialization for 32-bit | ||
template <typename Ty> | ||
struct FnvPrime<Ty, 4> | ||
{ | ||
static constexpr Ty value = static_cast<Ty>(0x01000193U); | ||
}; | ||
|
||
// Specialization for 64-bit | ||
template <typename Ty> | ||
struct FnvPrime<Ty, 8> | ||
{ | ||
static constexpr Ty value = static_cast<Ty>(0x100000001b3ULL); | ||
}; | ||
|
||
// Forward declaration for FNV offset basis constants | ||
template <typename Ty, size_t Size> | ||
struct FnvOffset; | ||
|
||
// Specialization for 32-bit | ||
template <typename Ty> | ||
struct FnvOffset<Ty, 4> | ||
{ | ||
// 32-bit offset basis | ||
static constexpr Ty value = static_cast<Ty>(0x811C9DC5U); | ||
|
||
static constexpr Ty Fix(Ty hval) noexcept { return hval; } | ||
}; | ||
|
||
// Specialization for 64-bit | ||
template <typename Ty> | ||
struct FnvOffset<Ty, 8> | ||
{ | ||
// 64-bit offset basis | ||
static constexpr Ty value = static_cast<Ty>(0xCBF29CE484222325ULL); | ||
|
||
// Fix function: mix upper and lower bits for better distribution | ||
static constexpr Ty Fix(Ty hval) noexcept { return hval ^ (hval >> 32); } | ||
}; | ||
|
||
/** | ||
* FNV-1a hash function | ||
* | ||
* @tparam Ty Hash integer type (std::size_t, uint32_t, uint64_t, etc.) | ||
* @param buf Pointer to the input buffer | ||
* @param len Length of the buffer | ||
* @param hval Starting hash value (defaults to offset basis) | ||
* @return Computed hash | ||
*/ | ||
template <typename Ty> | ||
inline Ty Fnv1a(const void *buf, size_t len, Ty hval = FnvOffset<Ty, sizeof(Ty)>::value) noexcept | ||
{ | ||
const unsigned char *bp = reinterpret_cast<const unsigned char *>(buf); | ||
const unsigned char *be = bp + len; | ||
Ty prime = FnvPrime<Ty, sizeof(Ty)>::value; | ||
|
||
while (bp < be) | ||
{ | ||
hval ^= static_cast<Ty>(*bp++); | ||
hval *= prime; | ||
} | ||
return FnvOffset<Ty, sizeof(Ty)>::Fix(hval); | ||
} | ||
|
||
/** | ||
* Hash and equality for nostd::string_view, enabling safe use in unordered_map | ||
* without requiring null termination. | ||
*/ | ||
|
||
struct StringViewHash | ||
{ | ||
#if defined(OPENTELEMETRY_STL_VERSION) | ||
# if OPENTELEMETRY_STL_VERSION >= 2020 | ||
using is_transparent = void; | ||
# endif | ||
#endif | ||
|
||
std::size_t operator()(const std::string &s) const noexcept | ||
{ | ||
return Fnv1a<std::size_t>(s.data(), s.size()); | ||
} | ||
|
||
std::size_t operator()(opentelemetry::nostd::string_view sv) const noexcept | ||
{ | ||
return Fnv1a<std::size_t>(sv.data(), sv.size()); | ||
} | ||
}; | ||
|
||
struct StringViewEqual | ||
{ | ||
#if defined(OPENTELEMETRY_STL_VERSION) | ||
# if OPENTELEMETRY_STL_VERSION >= 2020 | ||
using is_transparent = void; | ||
# endif | ||
#endif | ||
|
||
template <typename Lhs, typename Rhs> | ||
bool operator()(const Lhs &lhs, const Rhs &rhs) const noexcept | ||
{ | ||
opentelemetry::nostd::string_view lsv(lhs); | ||
opentelemetry::nostd::string_view rsv(rhs); | ||
|
||
return lsv.size() == rsv.size() && std::memcmp(lsv.data(), rsv.data(), lsv.size()) == 0; | ||
} | ||
}; | ||
|
||
/** | ||
* Cross-platform heterogeneous lookup wrapper. | ||
* Falls back to std::string construction on libc++ (macOS) and pre-c++20, | ||
* but uses direct lookup on libstdc++ (Linux). | ||
*/ | ||
|
||
template <typename MapType> | ||
inline auto find_hetero(MapType &&map, opentelemetry::nostd::string_view key) | ||
{ | ||
#if defined(_LIBCPP_VERSION) || \ | ||
(!defined(OPENTELEMETRY_STL_VERSION) || OPENTELEMETRY_STL_VERSION < 2020) | ||
return map.find(std::string(key)); | ||
#else | ||
// libstdc++ + C++20: heterogeneous lookup works | ||
return map.find(key); | ||
#endif | ||
} | ||
} // namespace common | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.