[Bf-blender-cvs] [90253ad2e75] master: Geometry Nodes: avoid creating a lazy function many times

Jacques Lucke noreply at git.blender.org
Sat Jan 28 15:29:05 CET 2023


Commit: 90253ad2e753acde161b38d82bd650d54d3f6581
Author: Jacques Lucke
Date:   Sat Jan 28 15:28:55 2023 +0100
Branches: master
https://developer.blender.org/rB90253ad2e753acde161b38d82bd650d54d3f6581

Geometry Nodes: avoid creating a lazy function many times

It's better to use some statically allocated functions instead
of dynamically allocating them all the time.

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

M	source/blender/nodes/intern/geometry_nodes_lazy_function.cc

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

diff --git a/source/blender/nodes/intern/geometry_nodes_lazy_function.cc b/source/blender/nodes/intern/geometry_nodes_lazy_function.cc
index 4c8a67f81b3..8075694edca 100644
--- a/source/blender/nodes/intern/geometry_nodes_lazy_function.cc
+++ b/source/blender/nodes/intern/geometry_nodes_lazy_function.cc
@@ -1078,6 +1078,33 @@ class LazyFunctionForAnonymousAttributeSetJoin : public lf::LazyFunction {
   {
     return 2 * i + 1;
   }
+
+  /**
+   * Cache for functions small amounts to avoid to avoid building them many times.
+   */
+  static const LazyFunctionForAnonymousAttributeSetJoin &get_cached(
+      const int amount, Vector<std::unique_ptr<LazyFunction>> &r_functions)
+  {
+    constexpr int cache_amount = 16;
+    static std::array<LazyFunctionForAnonymousAttributeSetJoin, cache_amount> cached_functions =
+        get_cache(std::make_index_sequence<cache_amount>{});
+    if (amount <= cached_functions.size()) {
+      return cached_functions[amount];
+    }
+
+    auto fn = std::make_unique<LazyFunctionForAnonymousAttributeSetJoin>(amount);
+    const auto &fn_ref = *fn;
+    r_functions.append(std::move(fn));
+    return fn_ref;
+  }
+
+ private:
+  template<size_t... I>
+  static std::array<LazyFunctionForAnonymousAttributeSetJoin, sizeof...(I)> get_cache(
+      std::index_sequence<I...> /*indices*/)
+  {
+    return {LazyFunctionForAnonymousAttributeSetJoin(I)...};
+  }
 };
 
 enum class AttributeReferenceKeyType {
@@ -2529,18 +2556,17 @@ struct GeometryNodesLazyFunctionGraphBuilder {
     key.extend(used_sockets);
     std::sort(key.begin(), key.end());
     return cache.lookup_or_add_cb(key, [&]() {
-      auto lazy_function = std::make_unique<LazyFunctionForAnonymousAttributeSetJoin>(
-          attribute_set_sockets.size());
-      lf::Node &lf_node = lf_graph_->add_function(*lazy_function);
+      const auto &lazy_function = LazyFunctionForAnonymousAttributeSetJoin::get_cached(
+          attribute_set_sockets.size(), lf_graph_info_->functions);
+      lf::Node &lf_node = lf_graph_->add_function(lazy_function);
       for (const int i : attribute_set_sockets.index_range()) {
-        lf::InputSocket &lf_use_input = lf_node.input(lazy_function->get_use_input(i));
+        lf::InputSocket &lf_use_input = lf_node.input(lazy_function.get_use_input(i));
         socket_usage_inputs_.add(&lf_use_input);
         lf::InputSocket &lf_attributes_input = lf_node.input(
-            lazy_function->get_attribute_set_input(i));
+            lazy_function.get_attribute_set_input(i));
         lf_graph_->add_link(*used_sockets[i], lf_use_input);
         lf_graph_->add_link(*attribute_set_sockets[i], lf_attributes_input);
       }
-      lf_graph_info_->functions.append(std::move(lazy_function));
       return &lf_node.output(0);
     });
   }



More information about the Bf-blender-cvs mailing list