diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h --- a/llvm/include/llvm/ADT/SmallVector.h +++ b/llvm/include/llvm/ADT/SmallVector.h @@ -884,6 +884,20 @@ /// well-defined. template struct alignas(T) SmallVectorStorage {}; +// Parameter controlling the default small size for SmallVector. +// +// We guarantee that +// `sizeof(SmallVector) <= kSmallVectorMaxSizeofForDefaultSmallSize`. +constexpr size_t kSmallVectorMaxSizeofForDefaultSmallSize = 64; +template +constexpr size_t calculateSmallVectorDefaultInlineElements() { + // Discount the size of the header itself when calculating the maximum inline + // bytes. + size_t MaxInlineBytes = kSmallVectorMaxSizeofForDefaultSmallSize - + sizeof(SmallVectorImpl); + return MaxInlineBytes / sizeof(ElementTy); +} + /// This is a 'vector' (really, a variable-sized array), optimized /// for the case when the array is small. It contains some number of elements /// in-place, which allows it to avoid heap allocation when the actual number of @@ -892,7 +906,8 @@ /// /// Note that this does not attempt to be exception safe. /// -template +template ()> class LLVM_GSL_OWNER SmallVector : public SmallVectorImpl, SmallVectorStorage { public: diff --git a/llvm/unittests/ADT/SmallVectorTest.cpp b/llvm/unittests/ADT/SmallVectorTest.cpp --- a/llvm/unittests/ADT/SmallVectorTest.cpp +++ b/llvm/unittests/ADT/SmallVectorTest.cpp @@ -999,4 +999,17 @@ EXPECT_TRUE(makeArrayRef(V2).equals({4, 5, 3, 2})); } +struct BigElementType { + char Arr[10000] = {0}; +}; +template struct TestDefaultSmallSize { + static_assert(sizeof(SmallVector) <= + kSmallVectorMaxSizeofForDefaultSmallSize, + ""); +}; +using instantiateTestDefaultSmallSize = ::testing::Types< + TestDefaultSmallSize, TestDefaultSmallSize, + TestDefaultSmallSize, TestDefaultSmallSize, + TestDefaultSmallSize, TestDefaultSmallSize>; + } // end namespace