Skip to content

[ADT] Use a constexpr function in SmallVector (NFC) #149455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kazutakahirata
Copy link
Contributor

SmallVector uses CalculateSmallVectorDefaultInlinedElements to compute
the default number of inlined elements. This patch converts the
struct to a constexpr function.

SmallVector uses CalculateSmallVectorDefaultInlinedElements to compute
the default number of inlined elements.  This patch converts the
struct to a constexpr function.
@llvmbot
Copy link
Member

llvmbot commented Jul 18, 2025

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

SmallVector uses CalculateSmallVectorDefaultInlinedElements to compute
the default number of inlined elements. This patch converts the
struct to a constexpr function.


Full diff: https://github.com/llvm/llvm-project/pull/149455.diff

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/SmallVector.h (+9-12)
diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index 80f7734b86907..9d75b31a0ac04 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -1122,12 +1122,10 @@ template <typename T> struct alignas(T) SmallVectorStorage<T, 0> {};
 /// `sizeof(SmallVector<T, 0>)`.
 template <typename T, unsigned N> class LLVM_GSL_OWNER SmallVector;
 
-/// Helper class for calculating the default number of inline elements for
+/// Helper function for calculating the default number of inline elements for
 /// `SmallVector<T>`.
-///
-/// This should be migrated to a constexpr function when our minimum
-/// compiler support is enough for multi-statement constexpr functions.
-template <typename T> struct CalculateSmallVectorDefaultInlinedElements {
+template <typename T>
+constexpr size_t calculateSmallVectorDefaultInlinedElements() {
   // Parameter controlling the default number of inlined elements
   // for `SmallVector<T>`.
   //
@@ -1135,7 +1133,7 @@ template <typename T> struct CalculateSmallVectorDefaultInlinedElements {
   // 1. There is at least one inlined element.
   // 2. `sizeof(SmallVector<T>) <= kPreferredSmallVectorSizeof` unless
   // it contradicts 1.
-  static constexpr size_t kPreferredSmallVectorSizeof = 64;
+  constexpr size_t kPreferredSmallVectorSizeof = 64;
 
   // static_assert that sizeof(T) is not "too big".
   //
@@ -1168,12 +1166,11 @@ template <typename T> struct CalculateSmallVectorDefaultInlinedElements {
 
   // Discount the size of the header itself when calculating the maximum inline
   // bytes.
-  static constexpr size_t PreferredInlineBytes =
+  constexpr size_t PreferredInlineBytes =
       kPreferredSmallVectorSizeof - sizeof(SmallVector<T, 0>);
-  static constexpr size_t NumElementsThatFit = PreferredInlineBytes / sizeof(T);
-  static constexpr size_t value =
-      NumElementsThatFit == 0 ? 1 : NumElementsThatFit;
-};
+  constexpr size_t NumElementsThatFit = PreferredInlineBytes / sizeof(T);
+  return NumElementsThatFit == 0 ? 1 : NumElementsThatFit;
+}
 
 /// This is a 'vector' (really, a variable-sized array), optimized
 /// for the case when the array is small.  It contains some number of elements
@@ -1192,7 +1189,7 @@ template <typename T> struct CalculateSmallVectorDefaultInlinedElements {
 ///
 /// \see https://llvm.org/docs/ProgrammersManual.html#llvm-adt-smallvector-h
 template <typename T,
-          unsigned N = CalculateSmallVectorDefaultInlinedElements<T>::value>
+          unsigned N = calculateSmallVectorDefaultInlinedElements<T>()>
 class LLVM_GSL_OWNER SmallVector : public SmallVectorImpl<T>,
                                    SmallVectorStorage<T, N> {
 public:

@kazutakahirata
Copy link
Contributor Author

I'm going to fix the Windows build before I merge this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants