[libcxx-commits] [libcxx] [libc++] Refactor __next_prime to be const (PR #157421)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 8 03:40:52 PDT 2025


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/157421

This can improve the codegen a bit, since the compiler can now know that `__next_prime` never modifies memory. If the argument to `__rehash` is known it can even eliminate the overflow check entirely, possibly allowing the compiler to fold away more code.


>From 7209c84d9a861d3317d76639348197120a8ab158 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Mon, 8 Sep 2025 12:39:18 +0200
Subject: [PATCH] [libc++] Refactor __next_prime to be const

---
 libcxx/include/__hash_table | 22 +++++++++++++++++++---
 libcxx/src/hash.cpp         | 14 +++-----------
 2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/libcxx/include/__hash_table b/libcxx/include/__hash_table
index 91f660d3491e8..006947768b3c1 100644
--- a/libcxx/include/__hash_table
+++ b/libcxx/include/__hash_table
@@ -47,6 +47,7 @@
 #include <__utility/swap.h>
 #include <__utility/try_key_extraction.h>
 #include <limits>
+#include <stdexcept>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -72,7 +73,22 @@ struct __is_hash_value_type : false_type {};
 template <class _One>
 struct __is_hash_value_type<_One> : __is_hash_value_type_imp<__remove_cvref_t<_One> > {};
 
-_LIBCPP_EXPORTED_FROM_ABI size_t __next_prime(size_t __n);
+_LIBCPP_HIDE_FROM_ABI inline void __check_for_overflow(size_t __n) {
+  if _LIBCPP_CONSTEXPR (sizeof(size_t) == 4) {
+    if (__n > 0xFFFFFFFB)
+      std::__throw_overflow_error("__next_prime overflow");
+  } else {
+    if (__n > 0xFFFFFFFFFFFFFFC5ull)
+      std::__throw_overflow_error("__next_prime overflow");
+  }
+}
+
+[[__gnu__::__const__]] _LIBCPP_EXPORTED_FROM_ABI size_t __next_prime_impl(size_t) _NOEXCEPT;
+
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t __get_next_prime(size_t __n) {
+  __check_for_overflow(__n);
+  return __next_prime_impl(__n);
+}
 
 template <class _NodePtr>
 struct __hash_node_base {
@@ -1764,7 +1780,7 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __n) _LIBCPP_D
   if (__n == 1)
     __n = 2;
   else if (__n & (__n - 1))
-    __n = std::__next_prime(__n);
+    __n = std::__get_next_prime(__n);
   size_type __bc = bucket_count();
   if (__n > __bc)
     __do_rehash<_UniqueKeys>(__n);
@@ -1772,7 +1788,7 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __n) _LIBCPP_D
     __n = std::max<size_type>(
         __n,
         std::__is_hash_power2(__bc) ? std::__next_hash_pow2(size_t(__math::ceil(float(size()) / max_load_factor())))
-                                    : std::__next_prime(size_t(__math::ceil(float(size()) / max_load_factor()))));
+                                    : std::__get_next_prime(size_t(__math::ceil(float(size()) / max_load_factor()))));
     if (__n < __bc)
       __do_rehash<_UniqueKeys>(__n);
   }
diff --git a/libcxx/src/hash.cpp b/libcxx/src/hash.cpp
index e1e6d2b4c2bdb..76a2b7085d38b 100644
--- a/libcxx/src/hash.cpp
+++ b/libcxx/src/hash.cpp
@@ -51,17 +51,7 @@ const unsigned indices[] = {
 // are fewer potential primes to search, and fewer potential primes to divide
 // against.
 
-inline void __check_for_overflow(size_t N) {
-  if constexpr (sizeof(size_t) == 4) {
-    if (N > 0xFFFFFFFB)
-      std::__throw_overflow_error("__next_prime overflow");
-  } else {
-    if (N > 0xFFFFFFFFFFFFFFC5ull)
-      std::__throw_overflow_error("__next_prime overflow");
-  }
-}
-
-size_t __next_prime(size_t n) {
+size_t __next_prime_impl(size_t n) noexcept {
   const size_t L = 210;
   const size_t N = sizeof(small_primes) / sizeof(small_primes[0]);
   // If n is small enough, search in small_primes
@@ -446,4 +436,6 @@ size_t __next_prime(size_t n) {
   }
 }
 
+_LIBCPP_EXPORTED_FROM_ABI size_t __next_prime(size_t n) { return __get_next_prime(n); }
+
 _LIBCPP_END_NAMESPACE_STD



More information about the libcxx-commits mailing list