From caf53ce03a390ba2ca7f51971b674a631aaf9ba7 Mon Sep 17 00:00:00 2001 From: Arek Sredzki Date: Wed, 9 Jul 2025 12:41:40 -0700 Subject: [PATCH] Use the next power of 2 for default_bucket_count This allows the compiler to optimize the modulo operation into a bitwise and. --- include/fixed_containers/fixed_robinhood_hashtable.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/fixed_containers/fixed_robinhood_hashtable.hpp b/include/fixed_containers/fixed_robinhood_hashtable.hpp index 69094d9b..51fcf8ac 100644 --- a/include/fixed_containers/fixed_robinhood_hashtable.hpp +++ b/include/fixed_containers/fixed_robinhood_hashtable.hpp @@ -393,10 +393,10 @@ class FixedRobinhoodHashtable constexpr std::size_t default_bucket_count(std::size_t value_count) { - // oversize the bucket array by 30% + // Oversize the bucket array by 30% to reduce collisions as the container becomes full. // TODO: think about the oversize percentage - // TODO: round to a nearby power of 2 to improve modulus performance - return (value_count * 130) / 100; + // Additionally, use the next power of 2 to improve modulo runtime efficiency. + return std::bit_ceil((value_count * 130) / 100); } } // namespace fixed_containers::fixed_robinhood_hashtable_detail