[llvm] r335421 - ADT: Use EBO to shrink SmallVector size 1

Duncan P. N. Exon Smith via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 23 11:39:45 PDT 2018


Author: dexonsmith
Date: Sat Jun 23 11:39:44 2018
New Revision: 335421

URL: http://llvm.org/viewvc/llvm-project?rev=335421&view=rev
Log:
ADT: Use EBO to shrink SmallVector size 1

SmallVectorStorage is empty when its size is 1; use inheritance so that
the empty base class optimization kicks in.

Modified:
    llvm/trunk/include/llvm/ADT/SmallVector.h
    llvm/trunk/lib/Support/SmallVector.cpp

Modified: llvm/trunk/include/llvm/ADT/SmallVector.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SmallVector.h?rev=335421&r1=335420&r2=335421&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/SmallVector.h (original)
+++ llvm/trunk/include/llvm/ADT/SmallVector.h Sat Jun 23 11:39:44 2018
@@ -858,10 +858,7 @@ template <typename T> struct SmallVector
 /// Note that this does not attempt to be exception safe.
 ///
 template <typename T, unsigned N>
-class SmallVector : public SmallVectorImpl<T> {
-  /// Inline space for elements which aren't stored in the base class.
-  SmallVectorStorage<T, N> Storage;
-
+class SmallVector : public SmallVectorImpl<T>, SmallVectorStorage<T, N> {
 public:
   SmallVector() : SmallVectorImpl<T>(N) {}
 

Modified: llvm/trunk/lib/Support/SmallVector.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/SmallVector.cpp?rev=335421&r1=335420&r2=335421&view=diff
==============================================================================
--- llvm/trunk/lib/Support/SmallVector.cpp (original)
+++ llvm/trunk/lib/Support/SmallVector.cpp Sat Jun 23 11:39:44 2018
@@ -14,6 +14,10 @@
 #include "llvm/ADT/SmallVector.h"
 using namespace llvm;
 
+// Check that no bytes are wasted.
+static_assert(sizeof(SmallVector<void *, 1>) == sizeof(void *) * 4,
+              "wasted space in SmallVector size 1; missing EBO?");
+
 /// grow_pod - This is an implementation of the grow() method which only works
 /// on POD-like datatypes and is out of line to reduce code duplication.
 void SmallVectorBase::grow_pod(void *FirstEl, size_t MinSizeInBytes,




More information about the llvm-commits mailing list