From ae9fd5578e8ab59bcffe1fd2b6a91531dff5bde6 Mon Sep 17 00:00:00 2001 From: Sean Silva Date: Thu, 3 Dec 2020 10:38:37 -0800 Subject: [SmallVector] Allow SmallVector 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) <= 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). 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 --- llvm/unittests/ADT/SmallVectorTest.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'llvm/unittests/ADT/SmallVectorTest.cpp') 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 V; + EXPECT_TRUE(V.empty()); + V.push_back(7); + EXPECT_EQ(V[0], 7); + + // Check that at least a couple layers of nested SmallVector'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>> NestedV; + NestedV.emplace_back().emplace_back().emplace_back(42); + EXPECT_EQ(NestedV[0][0][0], 42); +} + TEST(SmallVectorTest, InitializerList) { SmallVector V1 = {}; EXPECT_TRUE(V1.empty()); -- cgit v1.1