[PATCH] D48516: ADT: Use EBO to shrink SmallVector size 1

Duncan P. N. Exon Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 22 20:54:30 PDT 2018


dexonsmith created this revision.
dexonsmith added a reviewer: rnk.
Herald added a subscriber: hiraditya.

`SmallVectorStorage` is empty when its size is 1; use inheritance so that the empty base class optimization kicks in, saving a pointer for small `SmallVector`s.

I assume all compilers support this so the `static_assert` is valid... or should I only assert under some feature flag?


https://reviews.llvm.org/D48516

Files:
  llvm/include/llvm/ADT/SmallVector.h
  llvm/lib/Support/SmallVector.cpp


Index: llvm/lib/Support/SmallVector.cpp
===================================================================
--- llvm/lib/Support/SmallVector.cpp
+++ llvm/lib/Support/SmallVector.cpp
@@ -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,
Index: llvm/include/llvm/ADT/SmallVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallVector.h
+++ llvm/include/llvm/ADT/SmallVector.h
@@ -858,10 +858,7 @@
 /// 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) {}
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48516.152583.patch
Type: text/x-patch
Size: 1261 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180623/c553c77d/attachment.bin>


More information about the llvm-commits mailing list