Browse Source

faster accommodate

Michele Caini 8 years ago
parent
commit
67858bf300
2 changed files with 10 additions and 5 deletions
  1. 2 2
      README.md
  2. 8 3
      src/entt/entity/registry.hpp

+ 2 - 2
README.md

@@ -462,8 +462,8 @@ velocity.dx = 0.;
 velocity.dy = 0.;
 ```
 
-Note that `accommodate` is mostly syntactic sugar for the following `if`/`else`
-statement and nothing more:
+Note that `accommodate` is a slightly faster alternative for the following
+`if/else` statement and nothing more:
 
 ```cpp
 if(registry.has<Comp>(entity)) {

+ 8 - 3
src/entt/entity/registry.hpp

@@ -808,6 +808,8 @@ public:
      * }
      * @endcode
      *
+     * Prefer this function anyway because it has slightly better performance.
+     *
      * @warning
      * Attempting to use an invalid entity results in undefined behavior.<br/>
      * An assertion will abort the execution at runtime in debug mode in case of
@@ -821,9 +823,12 @@ public:
      */
     template<typename Component, typename... Args>
     Component & accommodate(entity_type entity, Args &&... args) {
-        return (has<Component>(entity)
-                ? replace<Component>(entity, std::forward<Args>(args)...)
-                : assign<Component>(entity, std::forward<Args>(args)...));
+        assure<Component>();
+        auto &cpool = pool<Component>();
+
+        return (cpool.has(entity)
+                ? cpool.get(entity) = Component{std::forward<Args>(args)...}
+                : cpool.construct(entity, std::forward<Args>(args)...));
     }
 
     /**