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
This commit is contained in:
Hans Goudey 2022-08-28 14:33:03 -05:00
parent 28750bcf7e
commit 67f3259c54
3 changed files with 25 additions and 7 deletions

View File

@ -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();

View File

@ -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.
*/

View File

@ -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;
}