Просмотр исходного кода

meta: avoid allocations for meta_template_node

Michele Caini 3 лет назад
Родитель
Сommit
ab3abf23ed
3 измененных файлов с 13 добавлено и 17 удалено
  1. 0 1
      src/entt/meta/factory.hpp
  2. 4 4
      src/entt/meta/meta.hpp
  3. 9 12
      src/entt/meta/node.hpp

+ 0 - 1
src/entt/meta/factory.hpp

@@ -619,7 +619,6 @@ inline void meta_reset(const id_type id) noexcept {
             node->id = {};
             node->base.clear();
             node->conv.clear();
-            node->templ.reset();
             node->dtor.dtor = nullptr;
             *it = std::exchange(node->next, nullptr);
 

+ 4 - 4
src/entt/meta/meta.hpp

@@ -1108,7 +1108,7 @@ public:
      * false otherwise.
      */
     [[nodiscard]] bool is_template_specialization() const noexcept {
-        return (node->templ != nullptr);
+        return (node->templ.arity != 0u);
     }
 
     /**
@@ -1116,7 +1116,7 @@ public:
      * @return The number of template arguments.
      */
     [[nodiscard]] size_type template_arity() const noexcept {
-        return node->templ ? node->templ->arity : size_type{};
+        return node->templ.arity;
     }
 
     /**
@@ -1127,7 +1127,7 @@ public:
      * @return The tag for the class template of the underlying type.
      */
     [[nodiscard]] inline meta_type template_type() const noexcept {
-        return node->templ ? node->templ->type : meta_type{};
+        return node->templ.type;
     }
 
     /**
@@ -1136,7 +1136,7 @@ public:
      * @return The type of the i-th template argument of a type.
      */
     [[nodiscard]] inline meta_type template_arg(const size_type index) const noexcept {
-        return index < template_arity() ? node->templ->arg(index) : meta_type{};
+        return index < template_arity() ? node->templ.arg(index) : meta_type{};
     }
 
     /**

+ 9 - 12
src/entt/meta/node.hpp

@@ -102,9 +102,9 @@ struct meta_func_node {
 struct meta_template_node {
     using size_type = std::size_t;
 
-    size_type arity;
-    meta_type_node *type;
-    meta_type_node *(*arg)(const size_type) noexcept;
+    size_type arity{0u};
+    meta_type_node *type{nullptr};
+    meta_type_node *(*arg)(const size_type) noexcept {nullptr};
 };
 
 struct meta_type_node {
@@ -120,7 +120,7 @@ struct meta_type_node {
     meta_any (*const default_constructor)();
     double (*const conversion_helper)(void *, const void *);
     meta_any (*const from_void)(void *, const void *);
-    std::unique_ptr<meta_template_node> templ;
+    meta_template_node templ;
     meta_ctor_node *ctor{nullptr};
     dense_map<id_type, meta_base_node, identity> base{};
     dense_map<id_type, meta_conv_node, identity> conv{};
@@ -174,15 +174,12 @@ class ENTT_API meta_node {
 
     [[nodiscard]] static auto meta_template_info() noexcept {
         if constexpr(is_complete_v<meta_template_traits<Type>>) {
-            auto node = std::make_unique<meta_template_node>();
-
-            node->arg = +[](const std::size_t index) noexcept -> meta_type_node * { return meta_arg_node(typename meta_template_traits<Type>::args_type{}, index); };
-            node->arity = meta_template_traits<Type>::args_type::size;
-            node->type = meta_node<typename meta_template_traits<Type>::class_type>::resolve();
-
-            return node;
+            return meta_template_node{
+                meta_template_traits<Type>::args_type::size,
+                meta_node<typename meta_template_traits<Type>::class_type>::resolve(),
+                +[](const std::size_t index) noexcept -> meta_type_node * { return meta_arg_node(typename meta_template_traits<Type>::args_type{}, index); }};
         } else {
-            return std::unique_ptr<meta_template_node>{};
+            return meta_template_node{};
         }
     }