[Bf-blender-cvs] [9c68e2ecc92] functions: speedup multi function evaluation

Jacques Lucke noreply at git.blender.org
Sun Dec 22 14:25:42 CET 2019


Commit: 9c68e2ecc92aa87798c6e2f87c76c6fdcfe663da
Author: Jacques Lucke
Date:   Sun Dec 22 10:58:28 2019 +0100
Branches: functions
https://developer.blender.org/rB9c68e2ecc92aa87798c6e2f87c76c6fdcfe663da

speedup multi function evaluation

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

M	source/blender/functions/FN_multi_function_context.h
M	source/blender/functions/intern/cpp_types.cc
M	source/blender/functions/intern/multi_functions/network.cc
M	source/blender/functions/intern/multi_functions/particles.cc
M	source/blender/modifiers/intern/MOD_functiondeform_cxx.cc
M	source/blender/simulations/bparticles/events.cpp
M	source/blender/simulations/bparticles/particle_function.cpp

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

diff --git a/source/blender/functions/FN_multi_function_context.h b/source/blender/functions/FN_multi_function_context.h
index b672749ec0f..b32e869ec17 100644
--- a/source/blender/functions/FN_multi_function_context.h
+++ b/source/blender/functions/FN_multi_function_context.h
@@ -20,11 +20,32 @@ using BLI::Optional;
 using BLI::Vector;
 using BLI::VirtualListRef;
 
+class MFElementContextIndices {
+ private:
+  MFElementContextIndices() = default;
+
+ public:
+  static MFElementContextIndices FromDirectMapping()
+  {
+    return MFElementContextIndices();
+  }
+
+  uint operator[](uint index) const
+  {
+    return index;
+  }
+
+  bool is_direct_mapping() const
+  {
+    return true;
+  }
+};
+
 class MFElementContexts {
  private:
   Vector<BLI::class_id_t> m_ids;
   Vector<const void *> m_contexts;
-  Vector<VirtualListRef<uint>> m_indices;
+  Vector<MFElementContextIndices> m_indices;
 
   friend class MFContextBuilder;
 
@@ -33,7 +54,7 @@ class MFElementContexts {
 
   template<typename T> struct TypedContext {
     const T *data;
-    VirtualListRef<uint> indices;
+    MFElementContextIndices indices;
   };
 
   template<typename T> Optional<TypedContext<T>> try_find() const
@@ -88,19 +109,13 @@ class MFContextBuilder : BLI::NonCopyable, BLI::NonMovable {
 
   void add_global_contexts(const MFContext &other);
 
-  template<typename T> void add_element_context(const T &context, VirtualListRef<uint> indices)
+  template<typename T> void add_element_context(const T &context, MFElementContextIndices indices)
   {
     m_element_contexts.m_ids.append(BLI::get_class_id<T>());
     m_element_contexts.m_contexts.append((const void *)&context);
     m_element_contexts.m_indices.append(indices);
   }
 
-  template<typename T> void add_element_context(const T &context, IndexRange indices)
-  {
-    this->add_element_context(context,
-                              VirtualListRef<uint>::FromFullArray(indices.as_array_ref()));
-  }
-
   template<typename T> void add_global_context(const T &context)
   {
     this->add_global_context(BLI::get_class_id<T>(), (const void *)&context);
diff --git a/source/blender/functions/intern/cpp_types.cc b/source/blender/functions/intern/cpp_types.cc
index 21f7d7f8dd3..ba6473b5dda 100644
--- a/source/blender/functions/intern/cpp_types.cc
+++ b/source/blender/functions/intern/cpp_types.cc
@@ -27,9 +27,7 @@ template<typename T> void ConstructDefaultN_CB(void *ptr, uint n)
 }
 template<typename T> void ConstructDefaultIndices_CB(void *ptr, IndexMask index_mask)
 {
-  for (uint i : index_mask.indices()) {
-    BLI::construct_default((T *)ptr + i);
-  }
+  index_mask.foreach_index([=](uint i) { BLI::construct_default((T *)ptr + i); });
 }
 
 template<typename T> void Destruct_CB(void *ptr)
@@ -42,9 +40,7 @@ template<typename T> void DestructN_CB(void *ptr, uint n)
 }
 template<typename T> void DestructIndices_CB(void *ptr, IndexMask index_mask)
 {
-  for (uint i : index_mask.indices()) {
-    BLI::destruct((T *)ptr + i);
-  }
+  index_mask.foreach_index([=](uint i) { BLI::destruct((T *)ptr + i); });
 }
 
 template<typename T> void CopyToInitialized_CB(const void *src, void *dst)
@@ -66,9 +62,7 @@ void CopyToInitializedIndices_CB(const void *src, void *dst, IndexMask index_mas
   const T *src_ = (const T *)src;
   T *dst_ = (T *)dst;
 
-  for (uint i : index_mask.indices()) {
-    dst_[i] = src_[i];
-  }
+  index_mask.foreach_index([=](uint i) { dst_[i] = src_[i]; });
 }
 
 template<typename T> void CopyToUninitialized_CB(const void *src, void *dst)
@@ -85,9 +79,7 @@ void CopyToUninitializedIndices_CB(const void *src, void *dst, IndexMask index_m
   const T *src_ = (const T *)src;
   T *dst_ = (T *)dst;
 
-  for (uint i : index_mask.indices()) {
-    new (dst_ + i) T(src_[i]);
-  }
+  index_mask.foreach_index([=](uint i) { new (dst_ + i) T(src_[i]); });
 }
 
 template<typename T> void RelocateToInitialized_CB(void *src, void *dst)
@@ -104,10 +96,10 @@ void RelocateToInitializedIndices_CB(void *src, void *dst, IndexMask index_mask)
   T *src_ = (T *)src;
   T *dst_ = (T *)dst;
 
-  for (uint i : index_mask.indices()) {
+  index_mask.foreach_index([=](uint i) {
     dst_[i] = std::move(src_[i]);
     src_[i].~T();
-  }
+  });
 }
 
 template<typename T> void RelocateToUninitialized_CB(void *src, void *dst)
@@ -124,10 +116,10 @@ void RelocateToUninitializedIndices_CB(void *src, void *dst, IndexMask index_mas
   T *src_ = (T *)src;
   T *dst_ = (T *)dst;
 
-  for (uint i : index_mask.indices()) {
+  index_mask.foreach_index([=](uint i) {
     new (dst_ + i) T(std::move(src_[i]));
     src_[i].~T();
-  }
+  });
 }
 
 template<typename T> void FillInitialized_CB(const void *value, void *dst, uint n)
@@ -145,9 +137,7 @@ void FillInitializedIndices_CB(const void *value, void *dst, IndexMask index_mas
   const T &value_ = *(const T *)value;
   T *dst_ = (T *)dst;
 
-  for (uint i : index_mask.indices()) {
-    dst_[i] = value_;
-  }
+  index_mask.foreach_index([=](uint i) { dst_[i] = value_; });
 }
 
 template<typename T> void FillUninitialized_CB(const void *value, void *dst, uint n)
@@ -165,9 +155,7 @@ void FillUninitializedIndices_CB(const void *value, void *dst, IndexMask index_m
   const T &value_ = *(const T *)value;
   T *dst_ = (T *)dst;
 
-  for (uint i : index_mask.indices()) {
-    new (dst_ + i) T(value_);
-  }
+  index_mask.foreach_index([=](uint i) { new (dst_ + i) T(value_); });
 }
 
 template<typename T> static std::unique_ptr<const CPPType> create_cpp_type(StringRef name)
diff --git a/source/blender/functions/intern/multi_functions/network.cc b/source/blender/functions/intern/multi_functions/network.cc
index 397af26e87b..d9c6d641ccb 100644
--- a/source/blender/functions/intern/multi_functions/network.cc
+++ b/source/blender/functions/intern/multi_functions/network.cc
@@ -4,6 +4,7 @@ namespace FN {
 
 class MF_EvaluateNetwork_Storage {
  private:
+  MonotonicAllocator<256> m_single_allocator;
   IndexMask m_mask;
   Vector<GenericVectorArray *> m_vector_arrays;
   Vector<GenericMutableArrayRef> m_arrays;
@@ -29,7 +30,6 @@ class MF_EvaluateNetwork_Storage {
     }
     for (GenericMutableArrayRef array : m_single_element_arrays) {
       array.destruct_indices(IndexMask(1));
-      MEM_freeN(array.buffer());
     }
   }
 
@@ -49,7 +49,7 @@ class MF_EvaluateNetwork_Storage {
 
   GenericMutableArrayRef allocate_array__single_element(const CPPType &type)
   {
-    void *buffer = MEM_mallocN(type.size(), __func__);
+    void *buffer = m_single_allocator.allocate(type.size(), type.alignment());
     GenericMutableArrayRef array(type, buffer, 1);
     m_single_element_arrays.append(array);
     return array;
diff --git a/source/blender/functions/intern/multi_functions/particles.cc b/source/blender/functions/intern/multi_functions/particles.cc
index 2b82e255404..06c49a4d777 100644
--- a/source/blender/functions/intern/multi_functions/particles.cc
+++ b/source/blender/functions/intern/multi_functions/particles.cc
@@ -26,7 +26,7 @@ void MF_ParticleAttribute::call(IndexMask mask, MFParams params, MFContext conte
   }
 
   AttributesRef attributes = context_data->data->attributes;
-  VirtualListRef<uint> element_indices = context_data->indices;
+  MFElementContextIndices element_indices = context_data->indices;
 
   group_indices_by_same_value(
       mask, attribute_names, [&](StringRef attribute_name, IndexMask indices_with_same_name) {
@@ -36,9 +36,15 @@ void MF_ParticleAttribute::call(IndexMask mask, MFParams params, MFContext conte
           return;
         }
         GenericMutableArrayRef array = opt_array.value();
-        for (uint i : indices_with_same_name) {
-          uint index = element_indices[i];
-          r_values.copy_in__initialized(i, array[index]);
+        if (element_indices.is_direct_mapping()) {
+          m_type.copy_to_uninitialized_indices(
+              array.buffer(), r_values.buffer(), indices_with_same_name);
+        }
+        else {
+          for (uint i : indices_with_same_name) {
+            uint index = element_indices[i];
+            r_values.copy_in__initialized(i, array[index]);
+          }
         }
       });
 }
diff --git a/source/blender/modifiers/intern/MOD_functiondeform_cxx.cc b/source/blender/modifiers/intern/MOD_functiondeform_cxx.cc
index 612b2ea3bd5..7bc20514d6c 100644
--- a/source/blender/modifiers/intern/MOD_functiondeform_cxx.cc
+++ b/source/blender/modifiers/intern/MOD_functiondeform_cxx.cc
@@ -72,7 +72,8 @@ void MOD_functiondeform_do(FunctionDeformModifierData *fdmd,
   MFContextBuilder context_builder;
   context_builder.add_global_context(id_handle_lookup);
   context_builder.add_global_context(time_context);
-  context_builder.add_element_context(vertex_positions_context, IndexRange(numVerts));
+  context_builder.add_element_context(vertex_positions_context,
+                                      FN::MFElementContextIndices::FromDirectMapping());
 
   function->call(IndexRange(numVerts), params_builder, context_builder);
 
diff --git a/source/blender/simulations/bparticles/events.cpp b/source/blender/simulations/bparticles/events.cpp
index 70d069e5a1a..1a4f366d8f2 100644
--- a/source/blender/simulations/bparticles/events.cpp
+++ b/source/blender/simulations/bparticles/events.cpp
@@ -67,7 +67,7 @@ void CustomEvent::filter(EventFilterInterface &interface)
   ParticleFunctionEvaluator inputs{m_inputs_fn, interface.index_mask(), interface.attributes()};
   inputs.context_builder().add_global_context(end_time_context);
   inputs.context_builder().add_element_context(durations_context,
-                                               IndexRange(interface.array_size()));
+                                               FN::MFElementContextIndices::FromDirectMapping());
   inputs.compute();
 
   for (uint pindex : interface.index_mask().indices()) {
diff --git a/source/blender/simulations/bparticles/particle_function.cpp b/source/blender/simulations/bparticles/particle_function.cpp
index eb1c6b6476c..331d3ef2bce 100644
--- a/source/blender/simulations/bparticles/particle_function.cpp
+++ b/source/blender/simulations/bparticles/particle_function.cpp
@@ -60,7 +60,8 @@ void ParticleFunctionEvaluator::compute()
   uint array_size = m_mask.min_array_size();
 
   FN::ParticleA

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list