Skip to content

Commit

Permalink
Tweaked: Improved chunk utlization
Browse files Browse the repository at this point in the history
  • Loading branch information
richardbiely committed Mar 18, 2024
1 parent 506efc5 commit bc03d08
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 71 deletions.
74 changes: 41 additions & 33 deletions include/gaia/ecs/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,9 @@ namespace gaia {
return;

auto& ec = fetch(entity);
if (!ec.pChunk->has<EntityDesc>())
return;

auto& entityDesc = ec.pChunk->sview_mut<EntityDesc>()[ec.row];
if (entityDesc.name == nullptr)
return;
Expand Down Expand Up @@ -2901,8 +2904,7 @@ namespace gaia {
return;
}

if (!has<EntityDesc>(entity))
return;
add<EntityDesc>(entity);

auto res =
m_nameToEntity.try_emplace(len == 0 ? EntityNameLookupKey(name) : EntityNameLookupKey(name, len), entity);
Expand Down Expand Up @@ -3001,39 +3003,46 @@ namespace gaia {

(void)reg_core_entity<Core_>(Core);

// Register the entity archetype (entity + EntityDesc component)
// Entity archetype matches the root archetype for now
m_pEntityArchetype = m_pRootArchetype;

// Register the component archetype (entity + EntityDesc + Component)
{
const auto id = GAIA_ID(EntityDesc);
const auto& ci = reg_core_entity<EntityDesc>(id);
EntityBuilder(*this, id).add(ci.entity);
sset<EntityDesc>(id) = {ci.name.str(), ci.name.len()};
m_pEntityArchetype = m_recs.entities[id.id()].pArchetype;
Archetype* pCompArchetype{};
{
const auto id = GAIA_ID(EntityDesc);
const auto& ci = reg_core_entity<EntityDesc>(id);
EntityBuilder(*this, id).add(ci.entity);
sset<EntityDesc>(id) = {ci.name.str(), ci.name.len()};
pCompArchetype = m_recs.entities[id.id()].pArchetype;
}
{
const auto id = GAIA_ID(Component);
const auto& ci = reg_core_entity<Component>(id, pCompArchetype);
EntityBuilder(*this, id).add(ci.entity);
acc_mut(id)
// Entity descriptor
.sset<EntityDesc>({ci.name.str(), ci.name.len()})
// Component
.sset<Component>(ci.comp);
m_pCompArchetype = m_recs.entities[id.id()].pArchetype;
}
}

// Register the component archetype (entity + EntityDesc + Component)
// Core components
{
const auto id = GAIA_ID(Component);
const auto& ci = reg_core_entity<Component>(id, m_pEntityArchetype);
EntityBuilder(*this, id).add(ci.entity);
acc_mut(id)
// Entity descriptor
.sset<EntityDesc>({ci.name.str(), ci.name.len()})
// Component
.sset<Component>(ci.comp);
m_pCompArchetype = m_recs.entities[id.id()].pArchetype;
}

(void)reg_core_entity<OnDelete_>(OnDelete);
(void)reg_core_entity<OnDeleteTarget_>(OnDeleteTarget);
(void)reg_core_entity<Remove_>(Remove);
(void)reg_core_entity<Delete_>(Delete);
(void)reg_core_entity<Error_>(Error);
(void)reg_core_entity<DependsOn_>(DependsOn);
(void)reg_core_entity<CantCombine_>(CantCombine);
(void)reg_core_entity<Acyclic_>(Acyclic);
(void)reg_core_entity<All_>(All);
(void)reg_core_entity<ChildOf_>(ChildOf);
(void)reg_core_entity<Is_>(Is);
(void)reg_core_entity<OnDelete_>(OnDelete);
(void)reg_core_entity<OnDeleteTarget_>(OnDeleteTarget);
(void)reg_core_entity<Remove_>(Remove);
(void)reg_core_entity<Delete_>(Delete);
(void)reg_core_entity<Error_>(Error);
(void)reg_core_entity<DependsOn_>(DependsOn);
(void)reg_core_entity<CantCombine_>(CantCombine);
(void)reg_core_entity<Acyclic_>(Acyclic);
(void)reg_core_entity<All_>(All);
(void)reg_core_entity<ChildOf_>(ChildOf);
(void)reg_core_entity<Is_>(Is);
}

// Add special properites for core components
{
Expand Down Expand Up @@ -3104,8 +3113,7 @@ namespace gaia {
}

// Make sure archetype pointers are up-to-date
// m_pEntityArchetype = m_recs.entities[GAIA_ID(EntityDesc).id()].pArchetype;
// m_pCompArchetype = m_recs.entities[GAIA_ID(Component).id()].pArchetype;
m_pCompArchetype = m_recs.entities[GAIA_ID(Component).id()].pArchetype;
}

void done() {
Expand Down
74 changes: 41 additions & 33 deletions single_include/gaia.h
Original file line number Diff line number Diff line change
Expand Up @@ -24190,6 +24190,9 @@ namespace gaia {
return;

auto& ec = fetch(entity);
if (!ec.pChunk->has<EntityDesc>())
return;

auto& entityDesc = ec.pChunk->sview_mut<EntityDesc>()[ec.row];
if (entityDesc.name == nullptr)
return;
Expand Down Expand Up @@ -24891,8 +24894,7 @@ namespace gaia {
return;
}

if (!has<EntityDesc>(entity))
return;
add<EntityDesc>(entity);

auto res =
m_nameToEntity.try_emplace(len == 0 ? EntityNameLookupKey(name) : EntityNameLookupKey(name, len), entity);
Expand Down Expand Up @@ -24991,39 +24993,46 @@ namespace gaia {

(void)reg_core_entity<Core_>(Core);

// Register the entity archetype (entity + EntityDesc component)
// Entity archetype matches the root archetype for now
m_pEntityArchetype = m_pRootArchetype;

// Register the component archetype (entity + EntityDesc + Component)
{
const auto id = GAIA_ID(EntityDesc);
const auto& ci = reg_core_entity<EntityDesc>(id);
EntityBuilder(*this, id).add(ci.entity);
sset<EntityDesc>(id) = {ci.name.str(), ci.name.len()};
m_pEntityArchetype = m_recs.entities[id.id()].pArchetype;
Archetype* pCompArchetype{};
{
const auto id = GAIA_ID(EntityDesc);
const auto& ci = reg_core_entity<EntityDesc>(id);
EntityBuilder(*this, id).add(ci.entity);
sset<EntityDesc>(id) = {ci.name.str(), ci.name.len()};
pCompArchetype = m_recs.entities[id.id()].pArchetype;
}
{
const auto id = GAIA_ID(Component);
const auto& ci = reg_core_entity<Component>(id, pCompArchetype);
EntityBuilder(*this, id).add(ci.entity);
acc_mut(id)
// Entity descriptor
.sset<EntityDesc>({ci.name.str(), ci.name.len()})
// Component
.sset<Component>(ci.comp);
m_pCompArchetype = m_recs.entities[id.id()].pArchetype;
}
}

// Register the component archetype (entity + EntityDesc + Component)
// Core components
{
const auto id = GAIA_ID(Component);
const auto& ci = reg_core_entity<Component>(id, m_pEntityArchetype);
EntityBuilder(*this, id).add(ci.entity);
acc_mut(id)
// Entity descriptor
.sset<EntityDesc>({ci.name.str(), ci.name.len()})
// Component
.sset<Component>(ci.comp);
m_pCompArchetype = m_recs.entities[id.id()].pArchetype;
}

(void)reg_core_entity<OnDelete_>(OnDelete);
(void)reg_core_entity<OnDeleteTarget_>(OnDeleteTarget);
(void)reg_core_entity<Remove_>(Remove);
(void)reg_core_entity<Delete_>(Delete);
(void)reg_core_entity<Error_>(Error);
(void)reg_core_entity<DependsOn_>(DependsOn);
(void)reg_core_entity<CantCombine_>(CantCombine);
(void)reg_core_entity<Acyclic_>(Acyclic);
(void)reg_core_entity<All_>(All);
(void)reg_core_entity<ChildOf_>(ChildOf);
(void)reg_core_entity<Is_>(Is);
(void)reg_core_entity<OnDelete_>(OnDelete);
(void)reg_core_entity<OnDeleteTarget_>(OnDeleteTarget);
(void)reg_core_entity<Remove_>(Remove);
(void)reg_core_entity<Delete_>(Delete);
(void)reg_core_entity<Error_>(Error);
(void)reg_core_entity<DependsOn_>(DependsOn);
(void)reg_core_entity<CantCombine_>(CantCombine);
(void)reg_core_entity<Acyclic_>(Acyclic);
(void)reg_core_entity<All_>(All);
(void)reg_core_entity<ChildOf_>(ChildOf);
(void)reg_core_entity<Is_>(Is);
}

// Add special properites for core components
{
Expand Down Expand Up @@ -25094,8 +25103,7 @@ namespace gaia {
}

// Make sure archetype pointers are up-to-date
// m_pEntityArchetype = m_recs.entities[GAIA_ID(EntityDesc).id()].pArchetype;
// m_pCompArchetype = m_recs.entities[GAIA_ID(Component).id()].pArchetype;
m_pCompArchetype = m_recs.entities[GAIA_ID(Component).id()].pArchetype;
}

void done() {
Expand Down
12 changes: 7 additions & 5 deletions src/test/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1685,17 +1685,19 @@ TEST_CASE("Add - no components") {
auto e = wld.add();
ents.push_back(e);
};
// auto verify = [&](uint32_t i) {
// REQUIRE(arr[i + 3] == ents[i]);
// };
auto verify = [&](uint32_t i) {
const auto a = arr[i + 2];
const auto e = ents[i];
REQUIRE(a == e);
};

GAIA_FOR(N) create();

auto q = wld.query().all<ecs::EntityDesc>().no<ecs::Component>();
auto q = wld.query().no<ecs::Component>();
q.arr(arr);
REQUIRE(arr.size() - 3 == ents.size()); // 3 for core component

// GAIA_FOR(N) verify(i);
GAIA_FOR(N) verify(i);
}

TEST_CASE("Add - 1 component") {
Expand Down

0 comments on commit bc03d08

Please sign in to comment.