aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/ADT/SmallVectorTest.cpp
diff options
context:
space:
mode:
authorSean Silva <silvasean@google.com>2020-12-03 10:38:37 -0800
committerSean Silva <silvasean@google.com>2020-12-03 17:21:44 -0800
commitae9fd5578e8ab59bcffe1fd2b6a91531dff5bde6 (patch)
treeb1d00e8f7dac6ee86949f82fbae44e98150f3d05 /llvm/unittests/ADT/SmallVectorTest.cpp
parent2f0de582949d1c9b5beff591b3735b6c02c45033 (diff)
downloadllvm-ae9fd5578e8ab59bcffe1fd2b6a91531dff5bde6.zip
llvm-ae9fd5578e8ab59bcffe1fd2b6a91531dff5bde6.tar.gz
llvm-ae9fd5578e8ab59bcffe1fd2b6a91531dff5bde6.tar.bz2
[SmallVector] Allow SmallVector<T>
This patch adds a capability to SmallVector to decide a number of inlined elements automatically. The policy is: - A minimum of 1 inlined elements, with more as long as sizeof(SmallVector<T>) <= 64. - If sizeof(T) is "too big", then trigger a static_assert: this dodges the more pathological cases This is expected to systematically improve SmallVector use in the LLVM codebase, which has historically been plagued by semi-arbitrary / cargo culted N parameters, often leading to bad outcomes due to excessive sizeof(SmallVector<T, N>). This default also makes programming more convenient by avoiding edit/rebuild cycles due to forgetting to type the N parameter. Differential Revision: https://reviews.llvm.org/D92522
Diffstat (limited to 'llvm/unittests/ADT/SmallVectorTest.cpp')
-rw-r--r--llvm/unittests/ADT/SmallVectorTest.cpp16
1 files changed, 16 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/SmallVectorTest.cpp b/llvm/unittests/ADT/SmallVectorTest.cpp
index 74b6561..957412f 100644
--- a/llvm/unittests/ADT/SmallVectorTest.cpp
+++ b/llvm/unittests/ADT/SmallVectorTest.cpp
@@ -983,6 +983,22 @@ TEST(SmallVectorTest, EmplaceBack) {
}
}
+TEST(SmallVectorTest, DefaultInlinedElements) {
+ SmallVector<int> V;
+ EXPECT_TRUE(V.empty());
+ V.push_back(7);
+ EXPECT_EQ(V[0], 7);
+
+ // Check that at least a couple layers of nested SmallVector<T>'s are allowed
+ // by the default inline elements policy. This pattern happens in practice
+ // with some frequency, and it seems fairly harmless even though each layer of
+ // SmallVector's will grow the total sizeof by a vector header beyond the
+ // "preferred" maximum sizeof.
+ SmallVector<SmallVector<SmallVector<int>>> NestedV;
+ NestedV.emplace_back().emplace_back().emplace_back(42);
+ EXPECT_EQ(NestedV[0][0][0], 42);
+}
+
TEST(SmallVectorTest, InitializerList) {
SmallVector<int, 2> V1 = {};
EXPECT_TRUE(V1.empty());