Commit: f4e7621ee7c574b4064d948466a3cdbc77779e33
Author: Jacques Lucke
Date:   Sun Jan 27 10:59:55 2019 +0100
Branches: functions
https://developer.blender.org/rBf4e7621ee7c574b4064d948466a3cdbc77779e33

don't construct unneeded objects in SmallVector

===================================================================

M       source/blender/blenlib/BLI_small_vector.hpp

===================================================================

diff --git a/source/blender/blenlib/BLI_small_vector.hpp 
b/source/blender/blenlib/BLI_small_vector.hpp
index 28d9af735e0..222c8e0ca2f 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -10,7 +10,7 @@ namespace BLI {
        template<typename T, uint N = 4>
        class SmallVector {
        private:
-               T m_small_buffer[N];
+               char m_small_buffer[sizeof(T) * N];
                T *m_elements;
                uint m_size = 0;
                uint m_capacity = N;
@@ -18,7 +18,7 @@ namespace BLI {
        public:
                SmallVector()
                {
-                       this->m_elements = this->m_small_buffer;
+                       this->m_elements = this->small_buffer();
                        this->m_capacity = N;
                        this->m_size = 0;
                }
@@ -62,6 +62,7 @@ namespace BLI {
                                return *this;
                        }
 
+                       this->destruct_elements();
                        this->free_own_buffer();
                        this->copy_from_other(other);
 
@@ -70,6 +71,7 @@ namespace BLI {
 
                SmallVector &operator=(SmallVector &&other)
                {
+                       this->destruct_elements();
                        this->free_own_buffer();
                        
this->steal_from_other(std::forward<SmallVector>(other));
 
@@ -87,7 +89,7 @@ namespace BLI {
                                this->grow(std::max(this->m_capacity * 2, 
(uint)1));
                        }
 
-                       this->m_elements[this->m_size] = value;
+                       std::uninitialized_copy(&value, &value + 1, 
this->end());
                        this->m_size++;
                }
 
@@ -126,9 +128,14 @@ namespace BLI {
                { return this->end(); }
 
        private:
+               T *small_buffer() const
+               {
+                       return (T *)this->m_small_buffer;
+               }
+
                bool is_small() const
                {
-                       return this->m_elements == this->m_small_buffer;
+                       return this->m_elements == this->small_buffer();
                }
 
                void grow(uint min_capacity)
@@ -145,9 +152,7 @@ namespace BLI {
                                std::make_move_iterator(this->end()),
                                new_array);
 
-                       for (uint i = 0; i < this->m_size; i++) {
-                               (this->m_elements + i)->~T();
-                       }
+                       this->destruct_elements();
 
                        if (!this->is_small()) {
                                std::free(this->m_elements);
@@ -169,14 +174,13 @@ namespace BLI {
                void copy_from_other(const SmallVector &other)
                {
                        if (other.is_small()) {
-                               this->m_elements = this->m_small_buffer;
-                               std::copy(other.begin(), other.end(), 
this->m_elements);
+                               this->m_elements = this->small_buffer();
                        }
                        else {
                                this->m_elements = (T *)std::malloc(sizeof(T) * 
other.m_capacity);
-                               std::uninitialized_copy(other.begin(), 
other.end(), this->m_elements);
                        }
 
+                       std::uninitialized_copy(other.begin(), other.end(), 
this->m_elements);
                        this->m_capacity = other.m_capacity;
                        this->m_size = other.m_size;
                }
@@ -184,11 +188,11 @@ namespace BLI {
                void steal_from_other(SmallVector &&other)
                {
                        if (other.is_small()) {
-                               std::copy(
+                               std::uninitialized_copy(
                                        std::make_move_iterator(other.begin()),
                                        std::make_move_iterator(other.end()),
-                                       this->m_small_buffer);
-                               this->m_elements = this->m_small_buffer;
+                                       this->small_buffer());
+                               this->m_elements = this->small_buffer();
                        }
                        else {
                                this->m_elements = other.m_elements;
@@ -199,6 +203,13 @@ namespace BLI {
 
                        other.m_elements = nullptr;
                }
+
+               void destruct_elements()
+               {
+                       for (uint i = 0; i < this->m_size; i++) {
+                               (this->m_elements + i)->~T();
+                       }
+               }
        };
 
 } /* namespace BLI */
\ No newline at end of file

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to