Skip to content

Commit 8b925a3

Browse files
[ADT] Use a constexpr function in SmallVector (NFC)
SmallVector uses CalculateSmallVectorDefaultInlinedElements to compute the default number of inlined elements. This patch converts the struct to a constexpr function.
1 parent de453e8 commit 8b925a3

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

llvm/include/llvm/ADT/SmallVector.h

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,20 +1122,18 @@ template <typename T> struct alignas(T) SmallVectorStorage<T, 0> {};
11221122
/// `sizeof(SmallVector<T, 0>)`.
11231123
template <typename T, unsigned N> class LLVM_GSL_OWNER SmallVector;
11241124

1125-
/// Helper class for calculating the default number of inline elements for
1125+
/// Helper function for calculating the default number of inline elements for
11261126
/// `SmallVector<T>`.
1127-
///
1128-
/// This should be migrated to a constexpr function when our minimum
1129-
/// compiler support is enough for multi-statement constexpr functions.
1130-
template <typename T> struct CalculateSmallVectorDefaultInlinedElements {
1127+
template <typename T>
1128+
constexpr size_t calculateSmallVectorDefaultInlinedElements() {
11311129
// Parameter controlling the default number of inlined elements
11321130
// for `SmallVector<T>`.
11331131
//
11341132
// The default number of inlined elements ensures that
11351133
// 1. There is at least one inlined element.
11361134
// 2. `sizeof(SmallVector<T>) <= kPreferredSmallVectorSizeof` unless
11371135
// it contradicts 1.
1138-
static constexpr size_t kPreferredSmallVectorSizeof = 64;
1136+
constexpr size_t kPreferredSmallVectorSizeof = 64;
11391137

11401138
// static_assert that sizeof(T) is not "too big".
11411139
//
@@ -1168,12 +1166,11 @@ template <typename T> struct CalculateSmallVectorDefaultInlinedElements {
11681166

11691167
// Discount the size of the header itself when calculating the maximum inline
11701168
// bytes.
1171-
static constexpr size_t PreferredInlineBytes =
1169+
constexpr size_t PreferredInlineBytes =
11721170
kPreferredSmallVectorSizeof - sizeof(SmallVector<T, 0>);
1173-
static constexpr size_t NumElementsThatFit = PreferredInlineBytes / sizeof(T);
1174-
static constexpr size_t value =
1175-
NumElementsThatFit == 0 ? 1 : NumElementsThatFit;
1176-
};
1171+
constexpr size_t NumElementsThatFit = PreferredInlineBytes / sizeof(T);
1172+
return NumElementsThatFit == 0 ? 1 : NumElementsThatFit;
1173+
}
11771174

11781175
/// This is a 'vector' (really, a variable-sized array), optimized
11791176
/// for the case when the array is small. It contains some number of elements
@@ -1192,7 +1189,7 @@ template <typename T> struct CalculateSmallVectorDefaultInlinedElements {
11921189
///
11931190
/// \see https://llvm.org/docs/ProgrammersManual.html#llvm-adt-smallvector-h
11941191
template <typename T,
1195-
unsigned N = CalculateSmallVectorDefaultInlinedElements<T>::value>
1192+
unsigned N = calculateSmallVectorDefaultInlinedElements<T>()>
11961193
class LLVM_GSL_OWNER SmallVector : public SmallVectorImpl<T>,
11971194
SmallVectorStorage<T, N> {
11981195
public:

0 commit comments

Comments
 (0)