From a12f7717a1e1d4d02115ef347135b478e447aebc Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Tue, 24 Jun 2025 22:15:41 -0400 Subject: [PATCH] Refactor: Attributes: Add assign_data function Prevent an extra copy that could happen when calling data_for_write() to retrieve mutable access, only to just replace the data. Pull Request: https://projects.blender.org/blender/blender/pulls/140946 --- source/blender/blenkernel/BKE_attribute_storage.hh | 8 ++++++++ source/blender/blenkernel/intern/pointcloud.cc | 2 +- source/blender/geometry/intern/randomize.cc | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/source/blender/blenkernel/BKE_attribute_storage.hh b/source/blender/blenkernel/BKE_attribute_storage.hh index b1c8df3bd2b..ee53dbef360 100644 --- a/source/blender/blenkernel/BKE_attribute_storage.hh +++ b/source/blender/blenkernel/BKE_attribute_storage.hh @@ -108,6 +108,9 @@ class Attribute { * \warning Does not yet support attributes stored as a single value (#AttrStorageType::Single). */ DataVariant &data_for_write(); + + /** Replace the attribute's data without first making the existing data mutable. */ + void assign_data(DataVariant &&data); }; class AttributeStorageRuntime { @@ -228,6 +231,11 @@ inline const Attribute::DataVariant &Attribute::data() const return data_; } +inline void Attribute::assign_data(DataVariant &&data) +{ + data_ = std::move(data); +} + } // namespace blender::bke inline blender::bke::AttributeStorage &AttributeStorage::wrap() diff --git a/source/blender/blenkernel/intern/pointcloud.cc b/source/blender/blenkernel/intern/pointcloud.cc index 5af023631bf..9adec5bad5c 100644 --- a/source/blender/blenkernel/intern/pointcloud.cc +++ b/source/blender/blenkernel/intern/pointcloud.cc @@ -232,7 +232,7 @@ static MutableSpan get_mutable_attribute(PointCloud &pointcloud, if (const auto *single_data = std::get_if(&attr->data())) { /* Convert single value storage to array storage. */ const GPointer g_value(CPPType::get(), single_data->value); - attr->data_for_write() = bke::Attribute::ArrayData::ForValue(g_value, pointcloud.totpoint); + attr->assign_data(bke::Attribute::ArrayData::ForValue(g_value, pointcloud.totpoint)); } auto &array_data = std::get(attr->data_for_write()); BLI_assert(array_data.size == pointcloud.totpoint); diff --git a/source/blender/geometry/intern/randomize.cc b/source/blender/geometry/intern/randomize.cc index 02b7a04104d..0a6989254f3 100644 --- a/source/blender/geometry/intern/randomize.cc +++ b/source/blender/geometry/intern/randomize.cc @@ -98,7 +98,7 @@ static void reorder_attribute_domain(bke::AttributeStorage &data, bke::attribute_math::gather(GSpan(type, data.data, data.size), new_by_old_map, GMutableSpan(type, new_data.data, new_data.size)); - attr.data_for_write() = std::move(new_data); + attr.assign_data(std::move(new_data)); } case bke::AttrStorageType::Single: { return;