[Bf-blender-cvs] [937a448de2f] functions: Remove Composition data structure

Jacques Lucke noreply at git.blender.org
Tue Jul 23 19:16:56 CEST 2019


Commit: 937a448de2f4a1c09e9dd5f48e81d8fbd2dd8478
Author: Jacques Lucke
Date:   Tue Jul 23 16:54:40 2019 +0200
Branches: functions
https://developer.blender.org/rB937a448de2f4a1c09e9dd5f48e81d8fbd2dd8478

Remove Composition data structure

This simplifies the code and speeds up accessing function
bodies and type extensions. Now, a lock is only necessary
when create new bodies or extensions.

This is achieved at the cost of having a slighly less generic
system, because we have to give e.g. different function body
types different IDs. Since the number of different types
is expected to be fairly low, this trade off is worth it.

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

D	source/blender/blenlib/BLI_composition.hpp
M	source/blender/blenlib/CMakeLists.txt
M	source/blender/functions/backends/dependencies/dependencies.cpp
M	source/blender/functions/backends/dependencies/dependencies.hpp
M	source/blender/functions/backends/llvm/build_ir_body.cpp
M	source/blender/functions/backends/llvm/build_ir_body.hpp
M	source/blender/functions/backends/llvm/llvm_types.cpp
M	source/blender/functions/backends/llvm/llvm_types.hpp
M	source/blender/functions/backends/tuple/cpp_types.cpp
M	source/blender/functions/backends/tuple/cpp_types.hpp
M	source/blender/functions/backends/tuple_call/tuple_call.cpp
M	source/blender/functions/backends/tuple_call/tuple_call.hpp
M	source/blender/functions/core/function.cpp
M	source/blender/functions/core/function.hpp
M	source/blender/functions/core/type.cpp
M	source/blender/functions/core/type.hpp

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

diff --git a/source/blender/blenlib/BLI_composition.hpp b/source/blender/blenlib/BLI_composition.hpp
deleted file mode 100644
index ee6673203b5..00000000000
--- a/source/blender/blenlib/BLI_composition.hpp
+++ /dev/null
@@ -1,86 +0,0 @@
-#pragma once
-
-/* This class allows to create a set of objects
- * of different types. Only one instance of a type
- * can exist in the set.
- *
- * In some cases, this approach should be preferred
- * over multiple inheritance.
- */
-
-#include "BLI_map.hpp"
-
-namespace BLI {
-
-class Composition {
- public:
-  typedef void (*FreeFunction)(void *value);
-
- private:
-  struct Entry {
-    void *value;
-    FreeFunction free;
-
-    template<typename T> Entry(T *value) : value((void *)value), free(get_free_function<T>())
-    {
-    }
-  };
-
- public:
-  template<typename T> void add(T *value)
-  {
-    m_elements.add(this->get_key<T>(), Entry(value));
-  }
-
-  template<typename T> inline T *get() const
-  {
-    uint64_t key = this->get_key<T>();
-    if (m_elements.contains(key)) {
-      return (T *)m_elements.lookup(key).value;
-    }
-    else {
-      return nullptr;
-    }
-  }
-
-  template<typename T> inline bool has() const
-  {
-    return m_elements.contains(this->get_key<T>());
-  }
-
-  ~Composition()
-  {
-    for (const Entry &entry : m_elements.values()) {
-      entry.free(entry.value);
-    }
-  }
-
- private:
-  template<typename T> static uint64_t get_key()
-  {
-    return (uint64_t)T::identifier_in_composition();
-  }
-
-  template<typename T> static FreeFunction get_free_function()
-  {
-    return T::free_self;
-  }
-
-  BLI::Map<uint64_t, Entry> m_elements;
-};
-
-} /* namespace BLI */
-
-#define BLI_COMPOSITION_DECLARATION(Type) \
-  static const char *identifier_in_composition(); \
-  static void free_self(void *value);
-
-#define BLI_COMPOSITION_IMPLEMENTATION(Type) \
-  const char *Type::identifier_in_composition() \
-  { \
-    return STRINGIFY(Type); \
-  } \
-  void Type::free_self(void *value) \
-  { \
-    delete (Type *)value; \
-  }
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 4250eb8b754..02a256c3dd5 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -238,7 +238,6 @@ set(SRC
   BLI_array_lookup.hpp
   BLI_array_ref.hpp
   BLI_chained_strings.hpp
-  BLI_composition.hpp
   BLI_lazy_init.h
   BLI_lazy_init.hpp
   intern/BLI_lazy_init.cpp
diff --git a/source/blender/functions/backends/dependencies/dependencies.cpp b/source/blender/functions/backends/dependencies/dependencies.cpp
index 211f65a2b5b..f4f5c527d53 100644
--- a/source/blender/functions/backends/dependencies/dependencies.cpp
+++ b/source/blender/functions/backends/dependencies/dependencies.cpp
@@ -2,6 +2,4 @@
 
 namespace FN {
 
-BLI_COMPOSITION_IMPLEMENTATION(DepsBody);
-
 } /* namespace FN */
diff --git a/source/blender/functions/backends/dependencies/dependencies.hpp b/source/blender/functions/backends/dependencies/dependencies.hpp
index 868e14fc08e..95acef230c6 100644
--- a/source/blender/functions/backends/dependencies/dependencies.hpp
+++ b/source/blender/functions/backends/dependencies/dependencies.hpp
@@ -71,7 +71,7 @@ class FunctionDepsBuilder {
 
 class DepsBody : public FunctionBody {
  public:
-  BLI_COMPOSITION_DECLARATION(DepsBody);
+  static const uint FUNCTION_BODY_ID = 0;
 
   virtual ~DepsBody()
   {
diff --git a/source/blender/functions/backends/llvm/build_ir_body.cpp b/source/blender/functions/backends/llvm/build_ir_body.cpp
index 06aba087433..a1888201087 100644
--- a/source/blender/functions/backends/llvm/build_ir_body.cpp
+++ b/source/blender/functions/backends/llvm/build_ir_body.cpp
@@ -2,6 +2,4 @@
 
 namespace FN {
 
-BLI_COMPOSITION_IMPLEMENTATION(LLVMBuildIRBody);
-
 } /* namespace FN */
diff --git a/source/blender/functions/backends/llvm/build_ir_body.hpp b/source/blender/functions/backends/llvm/build_ir_body.hpp
index 72cd1b0edbe..af03d8b9ae6 100644
--- a/source/blender/functions/backends/llvm/build_ir_body.hpp
+++ b/source/blender/functions/backends/llvm/build_ir_body.hpp
@@ -64,7 +64,7 @@ class CodeInterface {
 
 class LLVMBuildIRBody : public FunctionBody {
  public:
-  BLI_COMPOSITION_DECLARATION(LLVMBuildIRBody);
+  static const uint FUNCTION_BODY_ID = 3;
 
   virtual ~LLVMBuildIRBody(){};
 
diff --git a/source/blender/functions/backends/llvm/llvm_types.cpp b/source/blender/functions/backends/llvm/llvm_types.cpp
index cde089d987c..fa1277d6829 100644
--- a/source/blender/functions/backends/llvm/llvm_types.cpp
+++ b/source/blender/functions/backends/llvm/llvm_types.cpp
@@ -4,8 +4,6 @@ namespace FN {
 
 /******************** LLVMTypeInfo ********************/
 
-BLI_COMPOSITION_IMPLEMENTATION(LLVMTypeInfo);
-
 LLVMTypeInfo::~LLVMTypeInfo()
 {
 }
diff --git a/source/blender/functions/backends/llvm/llvm_types.hpp b/source/blender/functions/backends/llvm/llvm_types.hpp
index ad5bf27259b..30929275fdd 100644
--- a/source/blender/functions/backends/llvm/llvm_types.hpp
+++ b/source/blender/functions/backends/llvm/llvm_types.hpp
@@ -14,7 +14,7 @@ namespace FN {
  */
 class LLVMTypeInfo : public TypeExtension {
  public:
-  BLI_COMPOSITION_DECLARATION(LLVMTypeInfo);
+  static const uint TYPE_EXTENSION_ID = 1;
 
   virtual ~LLVMTypeInfo();
 
diff --git a/source/blender/functions/backends/tuple/cpp_types.cpp b/source/blender/functions/backends/tuple/cpp_types.cpp
index 0a8be45fc95..603be895c6f 100644
--- a/source/blender/functions/backends/tuple/cpp_types.cpp
+++ b/source/blender/functions/backends/tuple/cpp_types.cpp
@@ -2,6 +2,4 @@
 
 namespace FN {
 
-BLI_COMPOSITION_IMPLEMENTATION(CPPTypeInfo);
-
 } /* namespace FN */
diff --git a/source/blender/functions/backends/tuple/cpp_types.hpp b/source/blender/functions/backends/tuple/cpp_types.hpp
index 2ba6badf962..93edc2e7250 100644
--- a/source/blender/functions/backends/tuple/cpp_types.hpp
+++ b/source/blender/functions/backends/tuple/cpp_types.hpp
@@ -14,7 +14,7 @@ namespace FN {
 
 class CPPTypeInfo : public TypeExtension {
  public:
-  BLI_COMPOSITION_DECLARATION(CPPTypeInfo);
+  static const uint TYPE_EXTENSION_ID = 0;
 
   virtual ~CPPTypeInfo()
   {
diff --git a/source/blender/functions/backends/tuple_call/tuple_call.cpp b/source/blender/functions/backends/tuple_call/tuple_call.cpp
index 853a735b2c1..f042904aef0 100644
--- a/source/blender/functions/backends/tuple_call/tuple_call.cpp
+++ b/source/blender/functions/backends/tuple_call/tuple_call.cpp
@@ -2,9 +2,6 @@
 
 namespace FN {
 
-BLI_COMPOSITION_IMPLEMENTATION(TupleCallBody);
-BLI_COMPOSITION_IMPLEMENTATION(LazyInTupleCallBody);
-
 void TupleCallBodyBase::init_defaults(Tuple &fn_in) const
 {
   fn_in.init_default_all();
diff --git a/source/blender/functions/backends/tuple_call/tuple_call.hpp b/source/blender/functions/backends/tuple_call/tuple_call.hpp
index 1809a0ca1bf..3e8d0a9522d 100644
--- a/source/blender/functions/backends/tuple_call/tuple_call.hpp
+++ b/source/blender/functions/backends/tuple_call/tuple_call.hpp
@@ -85,7 +85,7 @@ class TupleCallBodyBase : public FunctionBody {
 
 class TupleCallBody : public TupleCallBodyBase {
  public:
-  BLI_COMPOSITION_DECLARATION(TupleCallBody);
+  static const uint FUNCTION_BODY_ID = 1;
 
   /**
    * Calls the function with additional stack frames.
@@ -199,7 +199,7 @@ class LazyState {
  */
 class LazyInTupleCallBody : public TupleCallBodyBase {
  public:
-  BLI_COMPOSITION_DECLARATION(LazyInTupleCallBody);
+  static const uint FUNCTION_BODY_ID = 2;
 
   /**
    * Required buffer size for temporary data.
diff --git a/source/blender/functions/core/function.cpp b/source/blender/functions/core/function.cpp
index 3e80662728e..b439fefa080 100644
--- a/source/blender/functions/core/function.cpp
+++ b/source/blender/functions/core/function.cpp
@@ -25,6 +25,11 @@ Function::Function(ChainedStringRef name,
 Function::~Function()
 {
   MEM_freeN((void *)m_strings);
+  for (uint i = 0; i < ARRAY_SIZE(m_bodies); i++) {
+    if (m_bodies[i] != nullptr) {
+      delete m_bodies[i];
+    }
+  }
 }
 
 void Function::print()
diff --git a/source/blender/functions/core/function.hpp b/source/blender/functions/core/function.hpp
index 2091ab4340e..bc09fe41637 100644
--- a/source/blender/functions/core/function.hpp
+++ b/source/blender/functions/core/function.hpp
@@ -41,6 +41,8 @@ class FunctionBody {
   virtual ~FunctionBody();
 
   Function *owner() const;
+
+  static const uint BODY_TYPE_AMOUNT = 4;
 };
 
 class Function final : public RefCountedBase {
@@ -140,8 +142,8 @@ class Function final : public RefCountedBase {
 
  private:
   ChainedStringRef m_name;
-  Composition m_bodies;
-  mutable std::mutex m_body_mutex;
+  std::mutex m_add_body_mutex;
+  FunctionBody *m_bodies[FunctionBody::BODY_TYPE_AMOUNT] = {0};
 
   Vector<ChainedStringRef> m_input_names;
   Vector<SharedType> m_input_types;
@@ -188,36 +190,40 @@ inline const StringRefNull Function::name() const
   return m_name.to_string_ref(m_strings);
 }
 
+#define STATIC_ASSERT_BODY_TYPE(T) \
+  BLI_STATIC_ASSERT((std::is_base_of<FunctionBody, T>::value), ""); \
+  BLI_STATIC_ASSERT(T::FUNCTION_BODY_ID < FunctionBody::BODY_TYPE_AMOUNT, "")
+
 template<typename T> inline bool Function::has_body() const
 {
-  std::lock_guard<std::mutex> lock(m_body_mutex);
-  BLI_STATIC_ASSERT((std::is_base_of<FunctionBody, T>::value), "");
-  return this->m_bodies.has<T>();
+  STATIC_ASSERT_BODY_TYPE(T);
+  return m_bodies[T::FUNCTION_BODY_ID] != nullptr;
 }
 
 template<typename T> inline T *Function::body() const
 {
-  std::lock_guard<std::mutex> lock(m_body_mutex);
-  BLI_STATIC_ASSERT((std::is_base_of<FunctionBody, T>::value), "");
-  return m_bodies.get<T>();
+  STATIC_ASSERT_BODY_TYPE(T);
+  return (T *)m_bodies[T::FUNCTION_BODY_ID];
 }
 
 template<typename T, typename... Args> inline bool Function::add_body(Args &&... args)
 {
-  std::lock_guard<std::mutex> lock(m_body_mutex);
-  BLI_STATIC_ASSERT((std::is_base_of<FunctionBody, T>::value), "");
+  STATIC_ASSERT_BODY_TYPE(T);
 
-  if (m_bodies.has<T>()) {
-    return false;
-  }
-  else {
+  std::lock_guard<std::mutex> lock(m_add_body_mutex);
+  if (m_bodies[T::FUNCTION_BODY_ID] == nullptr) {
     T *new_body = new T(std::forward<Args>(args)...);
     new_body->set_own

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list