[Bf-blender-cvs] [9ec8e308e7d] functions: more efficient small vector extend

Jacques Lucke noreply at git.blender.org
Wed May 22 17:32:31 CEST 2019


Commit: 9ec8e308e7df34b2d245d16834f4c6412f92c647
Author: Jacques Lucke
Date:   Wed May 22 16:16:34 2019 +0200
Branches: functions
https://developer.blender.org/rB9ec8e308e7df34b2d245d16834f4c6412f92c647

more efficient small vector extend

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

M	source/blender/blenlib/BLI_small_vector.hpp
M	tests/gtests/blenlib/BLI_small_vector_test.cc

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

diff --git a/source/blender/blenlib/BLI_small_vector.hpp b/source/blender/blenlib/BLI_small_vector.hpp
index 90273524ea9..08cbe2f0f27 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -53,9 +53,8 @@ template<typename T, uint N = 4> class SmallVector {
   SmallVector(std::initializer_list<T> values) : SmallVector()
   {
     this->reserve(values.size());
-    for (T value : values) {
-      this->append(value);
-    }
+    std::uninitialized_copy_n(values.begin(), values.size(), this->begin());
+    m_size = values.size();
   }
 
   SmallVector(const SmallVector &other)
@@ -124,16 +123,14 @@ template<typename T, uint N = 4> class SmallVector {
 
   void extend(const SmallVector &other)
   {
-    for (const T &value : other) {
-      this->append(value);
-    }
+    this->extend(other.begin(), other.size());
   }
 
   void extend(const T *start, uint amount)
   {
-    for (uint i = 0; i < amount; i++) {
-      this->append(start[i]);
-    }
+    this->reserve(m_size + amount);
+    std::uninitialized_copy_n(start, amount, this->end());
+    m_size += amount;
   }
 
   void fill(const T &value)
diff --git a/tests/gtests/blenlib/BLI_small_vector_test.cc b/tests/gtests/blenlib/BLI_small_vector_test.cc
index 2bd943a5e0b..ca35520bbd5 100644
--- a/tests/gtests/blenlib/BLI_small_vector_test.cc
+++ b/tests/gtests/blenlib/BLI_small_vector_test.cc
@@ -209,6 +209,19 @@ TEST(small_vector, AllEqual_True)
   EXPECT_TRUE(result);
 }
 
+TEST(small_vector, ExtendSmallVector)
+{
+  IntVector a = {2, 3, 4};
+  IntVector b = {11, 12};
+  b.extend(a);
+  EXPECT_EQ(b.size(), 5);
+  EXPECT_EQ(b[0], 11);
+  EXPECT_EQ(b[1], 12);
+  EXPECT_EQ(b[2], 2);
+  EXPECT_EQ(b[3], 3);
+  EXPECT_EQ(b[4], 4);
+}
+
 TEST(small_vector, ExtendArray)
 {
   int array[] = {3, 4, 5, 6};



More information about the Bf-blender-cvs mailing list