[PATCH] D90884: [SmallVector] Add a default small size.

Sean Silva via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 5 15:42:22 PST 2020


silvas updated this revision to Diff 303284.
silvas added a comment.

Add 64B upper bound, per Duncan's suggestion.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90884/new/

https://reviews.llvm.org/D90884

Files:
  llvm/include/llvm/ADT/SmallVector.h
  llvm/unittests/ADT/SmallVectorTest.cpp


Index: llvm/unittests/ADT/SmallVectorTest.cpp
===================================================================
--- llvm/unittests/ADT/SmallVectorTest.cpp
+++ 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 <typename ValueT> struct TestDefaultSmallSize {
+  static_assert(sizeof(SmallVector<ValueT>) <=
+                    kSmallVectorMaxSizeofForDefaultSmallSize,
+                "");
+};
+using instantiateTestDefaultSmallSize = ::testing::Types<
+    TestDefaultSmallSize<int8_t>, TestDefaultSmallSize<int16_t>,
+    TestDefaultSmallSize<int32_t>, TestDefaultSmallSize<int64_t>,
+    TestDefaultSmallSize<void *>, TestDefaultSmallSize<BigElementType>>;
+
 } // end namespace
Index: llvm/include/llvm/ADT/SmallVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallVector.h
+++ llvm/include/llvm/ADT/SmallVector.h
@@ -884,6 +884,20 @@
 /// well-defined.
 template <typename T> struct alignas(T) SmallVectorStorage<T, 0> {};
 
+// Parameter controlling the default small size for SmallVector.
+//
+// We guarantee that
+// `sizeof(SmallVector<T>) <= kSmallVectorMaxSizeofForDefaultSmallSize`.
+constexpr size_t kSmallVectorMaxSizeofForDefaultSmallSize = 64;
+template <typename ElementTy>
+constexpr size_t calculateSmallVectorDefaultInlineElements() {
+  // Discount the size of the header itself when calculating the maximum inline
+  // bytes.
+  size_t MaxInlineBytes = kSmallVectorMaxSizeofForDefaultSmallSize -
+                          sizeof(SmallVectorImpl<ElementTy>);
+  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 <typename T, unsigned N>
+template <typename T,
+          unsigned N = calculateSmallVectorDefaultInlineElements<T>()>
 class LLVM_GSL_OWNER SmallVector : public SmallVectorImpl<T>,
                                    SmallVectorStorage<T, N> {
 public:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D90884.303284.patch
Type: text/x-patch
Size: 2310 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201105/584ba2cc/attachment.bin>


More information about the llvm-commits mailing list