[Bf-blender-cvs] [72cc68e2997] master: Functions: only allocate resource scope when it is actually used

Jacques Lucke noreply at git.blender.org
Sat Jan 14 15:56:51 CET 2023


Commit: 72cc68e2997c273c8437146637f453178c8469de
Author: Jacques Lucke
Date:   Sat Jan 14 15:56:43 2023 +0100
Branches: master
https://developer.blender.org/rB72cc68e2997c273c8437146637f453178c8469de

Functions: only allocate resource scope when it is actually used

In most cases it is currently not used, so always having it there
causes unnecessary overhead. In my test file that causes
a 2 % performance improvement.

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

M	source/blender/functions/FN_multi_function_params.hh
M	source/blender/functions/intern/multi_function_params.cc

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

diff --git a/source/blender/functions/FN_multi_function_params.hh b/source/blender/functions/FN_multi_function_params.hh
index 98e03760d47..5ca9f45009b 100644
--- a/source/blender/functions/FN_multi_function_params.hh
+++ b/source/blender/functions/FN_multi_function_params.hh
@@ -25,7 +25,7 @@ namespace blender::fn::multi_function {
 
 class ParamsBuilder {
  private:
-  ResourceScope scope_;
+  std::unique_ptr<ResourceScope> scope_;
   const Signature *signature_;
   IndexMask mask_;
   int64_t min_array_size_;
@@ -90,13 +90,15 @@ class ParamsBuilder {
 
   void add_readonly_vector_input(const GVectorArray &vector_array, StringRef expected_name = "")
   {
-    this->add_readonly_vector_input(scope_.construct<GVVectorArray_For_GVectorArray>(vector_array),
-                                    expected_name);
+    this->add_readonly_vector_input(
+        this->resource_scope().construct<GVVectorArray_For_GVectorArray>(vector_array),
+        expected_name);
   }
   void add_readonly_vector_input(const GSpan single_vector, StringRef expected_name = "")
   {
     this->add_readonly_vector_input(
-        scope_.construct<GVVectorArray_For_SingleGSpan>(single_vector, min_array_size_),
+        this->resource_scope().construct<GVVectorArray_For_SingleGSpan>(single_vector,
+                                                                        min_array_size_),
         expected_name);
   }
   void add_readonly_vector_input(const GVVectorArray &ref, StringRef expected_name = "")
@@ -174,11 +176,6 @@ class ParamsBuilder {
     return *std::get<GVectorArray *>(actual_params_[param_index]);
   }
 
-  ResourceScope &resource_scope()
-  {
-    return scope_;
-  }
-
  private:
   void assert_current_param_type(ParamType param_type, StringRef expected_name = "")
   {
@@ -214,6 +211,14 @@ class ParamsBuilder {
     return actual_params_.size();
   }
 
+  ResourceScope &resource_scope()
+  {
+    if (!scope_) {
+      scope_ = std::make_unique<ResourceScope>();
+    }
+    return *scope_;
+  }
+
   void add_unused_output_for_unsupporting_function(const CPPType &type);
 };
 
@@ -285,7 +290,7 @@ class Params {
   const VVectorArray<T> &readonly_vector_input(int param_index, StringRef name = "")
   {
     const GVVectorArray &vector_array = this->readonly_vector_input(param_index, name);
-    return builder_->scope_.construct<VVectorArray_For_GVVectorArray<T>>(vector_array);
+    return builder_->resource_scope().construct<VVectorArray_For_GVVectorArray<T>>(vector_array);
   }
   const GVVectorArray &readonly_vector_input(int param_index, StringRef name = "")
   {
diff --git a/source/blender/functions/intern/multi_function_params.cc b/source/blender/functions/intern/multi_function_params.cc
index ce5421e4b8c..17e41454b5d 100644
--- a/source/blender/functions/intern/multi_function_params.cc
+++ b/source/blender/functions/intern/multi_function_params.cc
@@ -6,12 +6,13 @@ namespace blender::fn::multi_function {
 
 void ParamsBuilder::add_unused_output_for_unsupporting_function(const CPPType &type)
 {
-  void *buffer = scope_.linear_allocator().allocate(type.size() * min_array_size_,
-                                                    type.alignment());
+  ResourceScope &scope = this->resource_scope();
+  void *buffer = scope.linear_allocator().allocate(type.size() * min_array_size_,
+                                                   type.alignment());
   const GMutableSpan span{type, buffer, min_array_size_};
   actual_params_.append_unchecked_as(std::in_place_type<GMutableSpan>, span);
   if (!type.is_trivially_destructible()) {
-    scope_.add_destruct_call(
+    scope.add_destruct_call(
         [&type, buffer, mask = mask_]() { type.destruct_indices(buffer, mask); });
   }
 }



More information about the Bf-blender-cvs mailing list