Title: [153514] trunk/Source/WTF
Revision
153514
Author
[email protected]
Date
2013-07-31 01:12:32 -0700 (Wed, 31 Jul 2013)

Log Message

Shrink Vectors with inline capacity.
<http://webkit.org/b/119295>
<rdar://problem/14598360>

Reviewed by Anders Carlsson.

Pack Vector::m_size next to VectorBufferBase::m_capacity so there's no space wasted
on padding when sizeof(T) >= 8. Since m_size is not conceptually part of the buffer,
I just using'ed it into Vector.

* wtf/SizeLimits.cpp:
* wtf/Vector.h:
(WTF::VectorBufferBase::VectorBufferBase):
(WTF::VectorBuffer::VectorBuffer):
(WTF::Vector::Vector):
(WTF::::Vector):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (153513 => 153514)


--- trunk/Source/WTF/ChangeLog	2013-07-31 08:06:54 UTC (rev 153513)
+++ trunk/Source/WTF/ChangeLog	2013-07-31 08:12:32 UTC (rev 153514)
@@ -1,3 +1,22 @@
+2013-07-31  Andreas Kling  <[email protected]>
+
+        Shrink Vectors with inline capacity.
+        <http://webkit.org/b/119295>
+        <rdar://problem/14598360>
+
+        Reviewed by Anders Carlsson.
+
+        Pack Vector::m_size next to VectorBufferBase::m_capacity so there's no space wasted
+        on padding when sizeof(T) >= 8. Since m_size is not conceptually part of the buffer,
+        I just using'ed it into Vector.
+
+        * wtf/SizeLimits.cpp:
+        * wtf/Vector.h:
+        (WTF::VectorBufferBase::VectorBufferBase):
+        (WTF::VectorBuffer::VectorBuffer):
+        (WTF::Vector::Vector):
+        (WTF::::Vector):
+
 2013-07-30  Patrick Gansterer  <[email protected]>
 
         Move WindowsExtras.h from WebCore to WTF

Modified: trunk/Source/WTF/wtf/SizeLimits.cpp (153513 => 153514)


--- trunk/Source/WTF/wtf/SizeLimits.cpp	2013-07-31 08:06:54 UTC (rev 153513)
+++ trunk/Source/WTF/wtf/SizeLimits.cpp	2013-07-31 08:12:32 UTC (rev 153514)
@@ -54,13 +54,20 @@
     // Don't add anything here because this should stay small.
 };
 #endif
+template<typename T, unsigned inlineCapacity>
+struct SameSizeAsVectorWithInlineCapacity {
+    void* bufferPointer;
+    unsigned capacity;
+    unsigned size;
+    AlignedBuffer<inlineCapacity * sizeof(T), WTF_ALIGN_OF(T)> inlineBuffer;
+};
 
 COMPILE_ASSERT(sizeof(OwnPtr<int>) == sizeof(int*), OwnPtr_should_stay_small);
 COMPILE_ASSERT(sizeof(PassRefPtr<RefCounted<int> >) == sizeof(int*), PassRefPtr_should_stay_small);
 COMPILE_ASSERT(sizeof(RefCounted<int>) == sizeof(SameSizeAsRefCounted), RefCounted_should_stay_small);
 COMPILE_ASSERT(sizeof(RefCountedCustomAllocated<int>) == sizeof(SameSizeAsRefCounted), RefCountedCustomAllocated_should_stay_small);
 COMPILE_ASSERT(sizeof(RefPtr<RefCounted<int> >) == sizeof(int*), RefPtr_should_stay_small);
-COMPILE_ASSERT(sizeof(Vector<int>) == sizeof(int*) + 2 * sizeof(int), Vector_should_stay_small);
-COMPILE_ASSERT(sizeof(Vector<int, 1>) == 2 * sizeof(int*) + 2 * sizeof(int), Vector_should_stay_small);
-
+COMPILE_ASSERT(sizeof(Vector<int, 1>) == sizeof(SameSizeAsVectorWithInlineCapacity<int, 1>), Vector_should_stay_small);
+COMPILE_ASSERT(sizeof(Vector<int, 2>) == sizeof(SameSizeAsVectorWithInlineCapacity<int, 2>), Vector_should_stay_small);
+COMPILE_ASSERT(sizeof(Vector<int, 3>) == sizeof(SameSizeAsVectorWithInlineCapacity<int, 3>), Vector_should_stay_small);
 }

Modified: trunk/Source/WTF/wtf/Vector.h (153513 => 153514)


--- trunk/Source/WTF/wtf/Vector.h	2013-07-31 08:06:54 UTC (rev 153513)
+++ trunk/Source/WTF/wtf/Vector.h	2013-07-31 08:12:32 UTC (rev 153514)
@@ -319,12 +319,14 @@
         VectorBufferBase()
             : m_buffer(0)
             , m_capacity(0)
+            , m_size(0)
         {
         }
 
-        VectorBufferBase(T* buffer, size_t capacity)
+        VectorBufferBase(T* buffer, size_t capacity, size_t size)
             : m_buffer(buffer)
             , m_capacity(capacity)
+            , m_size(size)
         {
         }
 
@@ -335,6 +337,7 @@
 
         T* m_buffer;
         unsigned m_capacity;
+        unsigned m_size; // Only used by the Vector subclass, but placed here to avoid padding the struct.
     };
 
     template<typename T, size_t inlineCapacity>
@@ -349,8 +352,9 @@
         {
         }
 
-        VectorBuffer(size_t capacity)
+        VectorBuffer(size_t capacity, size_t size = 0)
         {
+            m_size = size;
             // Calling malloc(0) might take a lock and may actually do an
             // allocation on some systems.
             if (capacity)
@@ -380,6 +384,10 @@
         using Base::capacity;
 
         using Base::releaseBuffer;
+
+    protected:
+        using Base::m_size;
+
     private:
         using Base::m_buffer;
         using Base::m_capacity;
@@ -392,12 +400,12 @@
         typedef VectorBufferBase<T> Base;
     public:
         VectorBuffer()
-            : Base(inlineBuffer(), inlineCapacity)
+            : Base(inlineBuffer(), inlineCapacity, 0)
         {
         }
 
-        VectorBuffer(size_t capacity)
-            : Base(inlineBuffer(), inlineCapacity)
+        VectorBuffer(size_t capacity, size_t size = 0)
+            : Base(inlineBuffer(), inlineCapacity, size)
         {
             if (capacity > inlineCapacity)
                 Base::allocateBuffer(capacity);
@@ -486,6 +494,9 @@
             return Base::releaseBuffer();
         }
 
+    protected:
+        using Base::m_size;
+
     private:
         using Base::m_buffer;
         using Base::m_capacity;
@@ -520,13 +531,11 @@
         typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
 
         Vector()
-            : m_size(0)
         {
         }
         
         explicit Vector(size_t size)
-            : Base(size)
-            , m_size(size)
+            : Base(size, size)
         {
             if (begin())
                 TypeOperations::initialize(begin(), end());
@@ -645,8 +654,7 @@
         }
 
         Vector(size_t size, const T& val)
-            : Base(size)
-            , m_size(size)
+            : Base(size, size)
         {
             if (begin())
                 TypeOperations::uninitializedFill(begin(), end(), val);
@@ -677,8 +685,7 @@
         template<typename U> U* expandCapacity(size_t newMinCapacity, U*); 
         template<typename U> void appendSlowCase(const U&);
 
-        unsigned m_size;
-
+        using Base::m_size;
         using Base::buffer;
         using Base::capacity;
         using Base::swap;
@@ -693,8 +700,7 @@
 
     template<typename T, size_t inlineCapacity, typename OverflowHandler>
     Vector<T, inlineCapacity, OverflowHandler>::Vector(const Vector& other)
-        : Base(other.capacity())
-        , m_size(other.size())
+        : Base(other.capacity(), other.size())
     {
         if (begin())
             TypeOperations::uninitializedCopy(other.begin(), other.end(), begin());
@@ -703,8 +709,7 @@
     template<typename T, size_t inlineCapacity, typename OverflowHandler>
     template<size_t otherCapacity, typename otherOverflowBehaviour>
     Vector<T, inlineCapacity, OverflowHandler>::Vector(const Vector<T, otherCapacity, otherOverflowBehaviour>& other)
-        : Base(other.capacity())
-        , m_size(other.size())
+        : Base(other.capacity(), other.size())
     {
         if (begin())
             TypeOperations::uninitializedCopy(other.begin(), other.end(), begin());
@@ -774,7 +779,6 @@
 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
     template<typename T, size_t inlineCapacity, typename OverflowHandler>
     Vector<T, inlineCapacity, OverflowHandler>::Vector(Vector<T, inlineCapacity, OverflowHandler>&& other)
-        : m_size(0)
     {
         // It's a little weird to implement a move constructor using swap but this way we
         // don't have to add a move constructor to VectorBuffer.
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to