11/*
2- * Copyright (c) 2022-2024 , NVIDIA CORPORATION.
2+ * Copyright (c) 2022-2025 , NVIDIA CORPORATION.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1717
1818#include < cuco/detail/bitwise_compare.cuh>
1919
20- #include < cstddef >
20+ #include < cuda/std/cstdint >
2121
22- namespace cuco {
23- namespace detail {
22+ namespace cuco ::detail {
2423
2524/* *
2625 * @brief Enum of equality comparison results
2726 */
2827// ENUM VALUE MATTERS, DO NOT CHANGE
29- enum class equal_result : int32_t { UNEQUAL = 0 , EQUAL = 1 , EMPTY = 2 , AVAILABLE = 3 };
28+ enum class equal_result : cuda::std:: int8_t { UNEQUAL = 0 , EQUAL = 1 , EMPTY = 2 , AVAILABLE = 3 };
3029
31- enum class is_insert : bool { YES, NO };
30+ enum class is_insert : cuda::std:: int8_t { YES, NO };
3231
3332/* *
3433 * @brief Key equality wrapper.
@@ -37,8 +36,9 @@ enum class is_insert : bool { YES, NO };
3736 *
3837 * @tparam T Right-hand side Element type
3938 * @tparam Equal Type of user-provided equality binary callable
39+ * @tparam AllowsDuplicates Flag indicating whether duplicate keys are allowed
4040 */
41- template <typename T, typename Equal>
41+ template <typename T, typename Equal, bool AllowsDuplicates >
4242struct equal_wrapper {
4343 // TODO: Clean up the sentinel handling since it's duplicated in ref and equal wrapper
4444 T empty_sentinel_; // /< Empty sentinel value
@@ -97,16 +97,22 @@ struct equal_wrapper {
9797 __device__ constexpr equal_result operator ()(LHS const & lhs, RHS const & rhs) const noexcept
9898 {
9999 if constexpr (IsInsert == is_insert::YES) {
100- return (cuco::detail::bitwise_compare (rhs, empty_sentinel_) or
101- cuco::detail::bitwise_compare (rhs, erased_sentinel_))
102- ? equal_result::AVAILABLE
103- : this ->equal_to (lhs, rhs);
100+ if (cuco::detail::bitwise_compare (rhs, empty_sentinel_) or
101+ cuco::detail::bitwise_compare (rhs, erased_sentinel_)) {
102+ return equal_result::AVAILABLE;
103+ }
104+ // Optimization: For containers that allow duplicates, skip expensive key equality check
105+ // during insertion since we always insert regardless of whether the key already exists
106+ if constexpr (AllowsDuplicates) {
107+ return equal_result::UNEQUAL;
108+ } else {
109+ return this ->equal_to (lhs, rhs);
110+ }
104111 } else {
105112 return cuco::detail::bitwise_compare (rhs, empty_sentinel_) ? equal_result::EMPTY
106113 : this ->equal_to (lhs, rhs);
107114 }
108115 }
109116};
110117
111- } // namespace detail
112- } // namespace cuco
118+ } // namespace cuco::detail
0 commit comments