Skip to content

Commit a36a1ec

Browse files
[ADT] Simplify hash_combine (NFC) (#159938)
We just started using a C++17 fold expression inside combine (#159901). We still carry the style that was suitable for recursive calls of combine_data. Specifically, we keep passing several state variables as parameters of combine_data. Now that we no longer use recursion, this patch simplifies hash_combine_recursive_helper by making buffer_ptr, buffer_end, and length member variables while dropping the return value from hash_combine. This patch also names hash_combine_recursive_helper to hash_combine_helper. I will follow up to update comments that still mention "recursion".
1 parent d263150 commit a36a1ec

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

llvm/include/llvm/ADT/Hashing.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,11 @@ namespace detail {
483483
/// recursive combining of arguments used in hash_combine. It is particularly
484484
/// useful at minimizing the code in the recursive calls to ease the pain
485485
/// caused by a lack of variadic functions.
486-
struct hash_combine_recursive_helper {
486+
struct hash_combine_helper {
487487
char buffer[64] = {};
488+
char *buffer_ptr;
489+
char *const buffer_end;
490+
size_t length = 0;
488491
hash_state state;
489492
const uint64_t seed;
490493

@@ -493,18 +496,17 @@ struct hash_combine_recursive_helper {
493496
///
494497
/// This sets up the state for a recursive hash combine, including getting
495498
/// the seed and buffer setup.
496-
hash_combine_recursive_helper()
497-
: seed(get_execution_seed()) {}
499+
hash_combine_helper()
500+
: buffer_ptr(buffer), buffer_end(buffer + 64),
501+
seed(get_execution_seed()) {}
498502

499503
/// Combine one chunk of data into the current in-flight hash.
500504
///
501505
/// This merges one chunk of data into the hash. First it tries to buffer
502506
/// the data. If the buffer is full, it hashes the buffer into its
503507
/// hash_state, empties it, and then merges the new chunk in. This also
504508
/// handles cases where the data straddles the end of the buffer.
505-
template <typename T>
506-
char *combine_data(size_t &length, char *&buffer_ptr, char *buffer_end,
507-
T data) {
509+
template <typename T> void combine_data(T data) {
508510
if (!store_and_advance(buffer_ptr, buffer_end, data)) {
509511
// Check for skew which prevents the buffer from being packed, and do
510512
// a partial store into the buffer to fill it. This is only a concern
@@ -535,19 +537,14 @@ struct hash_combine_recursive_helper {
535537
partial_store_size))
536538
llvm_unreachable("buffer smaller than stored type");
537539
}
538-
return buffer_ptr;
539540
}
540541

541542
/// Recursive, variadic combining method.
542543
///
543544
/// This function recurses through each argument, combining that argument
544545
/// into a single hash.
545-
template <typename... Ts>
546-
hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
547-
const Ts &...args) {
548-
((void)combine_data(length, buffer_ptr, buffer_end,
549-
get_hashable_data(args)),
550-
...);
546+
template <typename... Ts> hash_code combine(const Ts &...args) {
547+
(combine_data(get_hashable_data(args)), ...);
551548

552549
// Finalize the hash by flushing any remaining data in the buffer.
553550
//
@@ -584,10 +581,10 @@ struct hash_combine_recursive_helper {
584581
/// The result is suitable for returning from a user's hash_value
585582
/// *implementation* for their user-defined type. Consumers of a type should
586583
/// *not* call this routine, they should instead call 'hash_value'.
587-
template <typename ...Ts> hash_code hash_combine(const Ts &...args) {
584+
template <typename... Ts> hash_code hash_combine(const Ts &...args) {
588585
// Recursively hash each argument using a helper class.
589-
::llvm::hashing::detail::hash_combine_recursive_helper helper;
590-
return helper.combine(0, helper.buffer, helper.buffer + 64, args...);
586+
::llvm::hashing::detail::hash_combine_helper helper;
587+
return helper.combine(args...);
591588
}
592589

593590
// Implementation details for implementations of hash_value overloads provided

0 commit comments

Comments
 (0)