[Bf-blender-cvs] [a98760f7dad] master: BLI: speedup adding to VectorSet by removing a check

Jacques Lucke noreply at git.blender.org
Sat Sep 14 14:41:39 CEST 2019


Commit: a98760f7dad7948fc50613d2c39cb9cbe8a4fb08
Author: Jacques Lucke
Date:   Sat Sep 14 14:41:19 2019 +0200
Branches: master
https://developer.blender.org/rBa98760f7dad7948fc50613d2c39cb9cbe8a4fb08

BLI: speedup adding to VectorSet by removing a check

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

M	source/blender/blenlib/BLI_vector.h
M	source/blender/blenlib/BLI_vector_set.h

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

diff --git a/source/blender/blenlib/BLI_vector.h b/source/blender/blenlib/BLI_vector.h
index c9701dcaa52..7a001c653d2 100644
--- a/source/blender/blenlib/BLI_vector.h
+++ b/source/blender/blenlib/BLI_vector.h
@@ -512,6 +512,14 @@ template<typename T, uint N = 4, typename Allocator = GuardedAllocator> class Ve
     return m_end;
   }
 
+  /**
+   * Get the current capacity of the vector.
+   */
+  uint capacity() const
+  {
+    return (uint)(m_capacity_end - m_begin);
+  }
+
   void print_stats() const
   {
     std::cout << "Small Vector at " << (void *)this << ":" << std::endl;
@@ -538,11 +546,6 @@ template<typename T, uint N = 4, typename Allocator = GuardedAllocator> class Ve
     }
   }
 
-  uint capacity() const
-  {
-    return (uint)(m_capacity_end - m_begin);
-  }
-
   BLI_NOINLINE void grow(uint min_capacity)
   {
     if (this->capacity() >= min_capacity) {
diff --git a/source/blender/blenlib/BLI_vector_set.h b/source/blender/blenlib/BLI_vector_set.h
index c61643fa3d8..5de3ae298a7 100644
--- a/source/blender/blenlib/BLI_vector_set.h
+++ b/source/blender/blenlib/BLI_vector_set.h
@@ -115,19 +115,22 @@ template<typename T, typename Allocator = GuardedAllocator> class VectorSet {
   Vector<T, 4, Allocator> m_elements;
 
  public:
-  VectorSet() = default;
+  VectorSet()
+  {
+    BLI_assert(m_array.slots_usable() <= m_elements.capacity());
+  }
 
-  VectorSet(ArrayRef<T> values)
+  VectorSet(ArrayRef<T> values) : VectorSet()
   {
     this->add_multiple(values);
   }
 
-  VectorSet(const std::initializer_list<T> &values)
+  VectorSet(const std::initializer_list<T> &values) : VectorSet()
   {
     this->add_multiple(values);
   }
 
-  VectorSet(const Vector<T> &values)
+  VectorSet(const Vector<T> &values) : VectorSet()
   {
     this->add_multiple(values);
   }
@@ -316,7 +319,7 @@ template<typename T, typename Allocator = GuardedAllocator> class VectorSet {
   {
     uint index = m_elements.size();
     slot.set_index(index);
-    m_elements.append(std::forward<ForwardT>(value));
+    m_elements.append_unchecked(std::forward<ForwardT>(value));
     m_array.update__empty_to_set();
   }
 
@@ -336,6 +339,7 @@ template<typename T, typename Allocator = GuardedAllocator> class VectorSet {
     }
 
     m_array = std::move(new_array);
+    m_elements.reserve(m_array.slots_usable());
   }
 
   void add_after_grow(uint index, ArrayType &new_array)



More information about the Bf-blender-cvs mailing list