diff --git a/include/gaia/ecs/world.h b/include/gaia/ecs/world.h index 1344f8eb..e3019ade 100644 --- a/include/gaia/ecs/world.h +++ b/include/gaia/ecs/world.h @@ -461,7 +461,7 @@ namespace gaia { if (entity.pair()) { // Make sure the entity container record exists if it is a pair - m_world.assign(entity, *m_world.m_pEntityArchetype); + m_world.assign(entity, *m_world.m_pEntityArchetype); } if (!handle_add_deps(entity)) @@ -2019,19 +2019,20 @@ namespace gaia { GAIA_NODISCARD Entity add(Archetype& archetype, bool isEntity, bool isPair, EntityKind kind) { EntityContainerCtx ctx{isEntity, isPair, kind}; const auto entity = m_recs.entities.alloc(&ctx); - assign(entity, archetype); + assign(entity, archetype); return entity; } //! Assigns an entity to a given archetype //! \param archetype Archetype the entity should inherit //! \param entity Entity + template void assign(Entity entity, Archetype& archetype) { if (entity.pair()) { // Pairs are always added to m_pEntityArchetype initially and this can't change. GAIA_ASSERT(&archetype == m_pEntityArchetype); - const auto it = m_recs.pairs.find(EntityLookupKey(entity)); - if (it == m_recs.pairs.end()) { + + auto assingNewEntity = [&]() { // Update the container record EntityContainer ec{}; ec.idx = entity.id(); @@ -2057,6 +2058,22 @@ namespace gaia { addPair(m_relationsToTargets, All, tgt); addPair(m_targetsToRelations, tgt, rel); addPair(m_targetsToRelations, All, rel); + }; + + if constexpr (IsNewEntity) { + // No need to make a lookup if this is a new entity +#if GAIA_ASSERT_ENABLED + // Just make sure no such record exists. + // The only way for this to happen is if the entity id overflew in which + // case everything falls apart anyway. + const auto it = m_recs.pairs.find(EntityLookupKey(entity)); + GAIA_ASSERT(it == m_recs.pairs.end()); +#endif + assingNewEntity(); + } else { + const auto it = m_recs.pairs.find(EntityLookupKey(entity)); + if (it == m_recs.pairs.end()) + assingNewEntity(); } } else { auto* pChunk = archetype.foc_free_chunk(); diff --git a/single_include/gaia.h b/single_include/gaia.h index c26e5f90..86229277 100644 --- a/single_include/gaia.h +++ b/single_include/gaia.h @@ -21719,7 +21719,7 @@ namespace gaia { if (entity.pair()) { // Make sure the entity container record exists if it is a pair - m_world.assign(entity, *m_world.m_pEntityArchetype); + m_world.assign(entity, *m_world.m_pEntityArchetype); } if (!handle_add_deps(entity)) @@ -23277,19 +23277,20 @@ namespace gaia { GAIA_NODISCARD Entity add(Archetype& archetype, bool isEntity, bool isPair, EntityKind kind) { EntityContainerCtx ctx{isEntity, isPair, kind}; const auto entity = m_recs.entities.alloc(&ctx); - assign(entity, archetype); + assign(entity, archetype); return entity; } //! Assigns an entity to a given archetype //! \param archetype Archetype the entity should inherit //! \param entity Entity + template void assign(Entity entity, Archetype& archetype) { if (entity.pair()) { // Pairs are always added to m_pEntityArchetype initially and this can't change. GAIA_ASSERT(&archetype == m_pEntityArchetype); - const auto it = m_recs.pairs.find(EntityLookupKey(entity)); - if (it == m_recs.pairs.end()) { + + auto assingNewEntity = [&]() { // Update the container record EntityContainer ec{}; ec.idx = entity.id(); @@ -23315,6 +23316,22 @@ namespace gaia { addPair(m_relationsToTargets, All, tgt); addPair(m_targetsToRelations, tgt, rel); addPair(m_targetsToRelations, All, rel); + }; + + if constexpr (IsNewEntity) { + // No need to make a lookup if this is a new entity +#if GAIA_ASSERT_ENABLED + // Just make sure no such record exists. + // The only way for this to happen is if the entity id overflew in which + // case everything falls apart anyway. + const auto it = m_recs.pairs.find(EntityLookupKey(entity)); + GAIA_ASSERT(it == m_recs.pairs.end()); +#endif + assingNewEntity(); + } else { + const auto it = m_recs.pairs.find(EntityLookupKey(entity)); + if (it == m_recs.pairs.end()) + assingNewEntity(); } } else { auto* pChunk = archetype.foc_free_chunk();