[Bf-blender-cvs] [67f3259c546] master: Curves: Avoid creating types array when unnecessary

Hans Goudey noreply at git.blender.org
Sun Aug 28 21:34:24 CEST 2022


Commit: 67f3259c54657996d47112967c9b982f78ebfe6e
Author: Hans Goudey
Date:   Sun Aug 28 14:33:03 2022 -0500
Branches: master
https://developer.blender.org/rB67f3259c54657996d47112967c9b982f78ebfe6e

Curves: Avoid creating types array when unnecessary

When the curve type attribute doesn't exist, there is no reason to
create an array for it only to fill the default value, which will add
overhead to subsequent "add" operations. I added a "get_if_single"
method to virtual array to simplify this check. Also use the existing
functions for filling curve types.

Differential Revision: https://developer.blender.org/D15560

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

M	source/blender/blenkernel/intern/curves_geometry.cc
M	source/blender/blenlib/BLI_virtual_array.hh
M	source/blender/geometry/intern/add_curves_on_mesh.cc

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

diff --git a/source/blender/blenkernel/intern/curves_geometry.cc b/source/blender/blenkernel/intern/curves_geometry.cc
index e60523c23da..fe9f6775995 100644
--- a/source/blender/blenkernel/intern/curves_geometry.cc
+++ b/source/blender/blenkernel/intern/curves_geometry.cc
@@ -255,6 +255,12 @@ void CurvesGeometry::fill_curve_types(const IndexMask selection, const CurveType
     this->fill_curve_types(type);
     return;
   }
+  if (std::optional<int8_t> single_type = this->curve_types().get_if_single()) {
+    if (single_type == type) {
+      /* No need for an array if the types are already a single with the correct type. */
+      return;
+    }
+  }
   /* A potential performance optimization is only counting the changed indices. */
   this->curve_types_for_write().fill_indices(selection, type);
   this->update_curve_types();
diff --git a/source/blender/blenlib/BLI_virtual_array.hh b/source/blender/blenlib/BLI_virtual_array.hh
index 7eab960b302..00677cf28a2 100644
--- a/source/blender/blenlib/BLI_virtual_array.hh
+++ b/source/blender/blenlib/BLI_virtual_array.hh
@@ -23,6 +23,8 @@
  * see of the increased compile time and binary size is worth it.
  */
 
+#include <optional>
+
 #include "BLI_any.hh"
 #include "BLI_array.hh"
 #include "BLI_index_mask.hh"
@@ -802,6 +804,18 @@ template<typename T> class VArrayCommon {
     return *static_cast<const T *>(info.data);
   }
 
+  /**
+   * Return the value that is returned for every index, if the array is stored as a single value.
+   */
+  std::optional<T> get_if_single() const
+  {
+    const CommonVArrayInfo info = impl_->common_info();
+    if (info.type != CommonVArrayInfo::Type::Single) {
+      return std::nullopt;
+    }
+    return *static_cast<const T *>(info.data);
+  }
+
   /**
    * Return true when the other virtual references the same underlying memory.
    */
diff --git a/source/blender/geometry/intern/add_curves_on_mesh.cc b/source/blender/geometry/intern/add_curves_on_mesh.cc
index 299040d4d32..7f269578f5d 100644
--- a/source/blender/geometry/intern/add_curves_on_mesh.cc
+++ b/source/blender/geometry/intern/add_curves_on_mesh.cc
@@ -276,6 +276,7 @@ AddCurvesOnMeshOutputs add_curves_on_mesh(CurvesGeometry &curves,
 
   /* Grow number of curves first, so that the offsets array can be filled. */
   curves.resize(old_points_num, new_curves_num);
+  const IndexRange new_curves_range = curves.curves_range().drop_front(old_curves_num);
 
   /* Compute new curve offsets. */
   MutableSpan<int> curve_offsets = curves.offsets_for_write();
@@ -290,8 +291,8 @@ AddCurvesOnMeshOutputs add_curves_on_mesh(CurvesGeometry &curves,
   else {
     new_point_counts_per_curve.fill(inputs.fallback_point_count);
   }
-  for (const int i : IndexRange(added_curves_num)) {
-    curve_offsets[old_curves_num + i + 1] += curve_offsets[old_curves_num + i];
+  for (const int i : new_curves_range) {
+    curve_offsets[i + 1] += curve_offsets[i];
   }
 
   const int new_points_num = curves.offsets().last();
@@ -342,7 +343,7 @@ AddCurvesOnMeshOutputs add_curves_on_mesh(CurvesGeometry &curves,
   const VArray<float> curves_selection = curves.selection_curve_float();
   if (curves_selection.is_span()) {
     MutableSpan<float> curves_selection_span = curves.selection_curve_float_for_write();
-    curves_selection_span.drop_front(old_curves_num).fill(1.0f);
+    curves_selection_span.slice(new_curves_range).fill(1.0f);
   }
 
   /* Initialize position attribute. */
@@ -366,10 +367,7 @@ AddCurvesOnMeshOutputs add_curves_on_mesh(CurvesGeometry &curves,
                                                inputs.transforms->surface_to_curves_normal);
   }
 
-  /* Set curve types. */
-  MutableSpan<int8_t> types_span = curves.curve_types_for_write();
-  types_span.drop_front(old_curves_num).fill(CURVE_TYPE_CATMULL_ROM);
-  curves.update_curve_types();
+  curves.fill_curve_types(new_curves_range, CURVE_TYPE_CATMULL_ROM);
 
   return outputs;
 }



More information about the Bf-blender-cvs mailing list