Skip to content

Commit 4f24849

Browse files
Improved hash funcs
1 parent d2a429b commit 4f24849

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

libcxx/include/__stacktrace/basic_stacktrace.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,11 @@ _LIBCPP_HIDE_FROM_ABI inline ostream& operator<<(ostream& __os, const basic_stac
285285
template <class _Allocator>
286286
struct hash<basic_stacktrace<_Allocator>> {
287287
_LIBCPP_HIDE_FROM_ABI size_t operator()(basic_stacktrace<_Allocator> const& __trace) const noexcept {
288-
return hash(__trace.__entries_)();
288+
size_t __ret = 0xc3a5c85c97cb3127ull; // just a big prime number; taken from __functional/hash.h
289+
for (stacktrace_entry const& __e : __trace.__entries_) {
290+
__ret = (__ret << 1) ^ __e.__base_.hash();
291+
}
292+
return __ret;
289293
}
290294
};
291295

libcxx/include/__stacktrace/stacktrace_entry.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ struct _Entry {
8585
_LIBCPP_EXPORTED_FROM_ABI string to_string() const;
8686
# endif // _LIBCPP_HAS_LOCALIZATION
8787

88+
_LIBCPP_HIDE_FROM_ABI size_t hash() const { return std::__hash_memory(&__addr_, sizeof(uintptr_t)); }
89+
8890
_LIBCPP_HIDE_FROM_ABI uintptr_t adjusted_addr() const;
8991

9092
_LIBCPP_HIDE_FROM_ABI ~_Entry() = default;
@@ -100,6 +102,7 @@ struct _Entry {
100102
class stacktrace_entry {
101103
friend _LIBCPP_HIDE_FROM_ABI inline ostream& operator<<(ostream& __os, std::stacktrace_entry const& __entry);
102104
friend _LIBCPP_HIDE_FROM_ABI inline string to_string(std::stacktrace_entry const& __entry);
105+
friend _LIBCPP_HIDE_FROM_ABI struct hash<stacktrace_entry>;
103106

104107
__stacktrace::_Entry __base_{};
105108

@@ -159,8 +162,7 @@ _LIBCPP_HIDE_FROM_ABI inline ostream& operator<<(ostream& __os, const std::stack
159162
template <>
160163
struct hash<stacktrace_entry> {
161164
_LIBCPP_HIDE_FROM_ABI size_t operator()(stacktrace_entry const& __entry) const noexcept {
162-
auto __addr = __entry.native_handle();
163-
return hash<uintptr_t>()(__addr);
165+
return __entry.__base_.hash();
164166
}
165167
};
166168

0 commit comments

Comments
 (0)