Skip to content

Commit

Permalink
speed up
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Aug 28, 2020
1 parent 83932d8 commit 3cbed3e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
4 changes: 4 additions & 0 deletions include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,13 @@ namespace Ubpa::UECS {
const std::set<Archetype*>& QueryArchetypes(const EntityQuery&) const;
mutable std::unordered_map<EntityQuery, std::set<Archetype*>> queryCache;

// if job is nullptr, direct run
void GenEntityJob(World*, Job*, SystemFunc*) const;
// if job is nullptr, direct run
void GenChunkJob(World*, Job*, SystemFunc*) const;
// if job is nullptr, direct run
void GenJob(World*, Job*, SystemFunc*) const;
// if job is nullptr, direct run
void AutoGen(World*, Job*, SystemFunc*) const;

struct EntityInfo {
Expand Down
40 changes: 30 additions & 10 deletions src/core/EntityMngr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ void EntityMngr::GenEntityJob(World* w, Job* job, SystemFunc* sys) const {
return;

if (sys->IsParallel()) {
assert(job);
size_t indexOffsetInQuery = 0;
for (Archetype* archetype : QueryArchetypes(sys->entityQuery)) {
auto [chunkEntity, chunkCmpts, sizes] = archetype->Locate(sys->entityQuery.locator);
Expand All @@ -281,12 +282,13 @@ void EntityMngr::GenEntityJob(World* w, Job* job, SystemFunc* sys) const {
size_t idxOffsetInChunk = i * chunkCapacity;
size_t indexOffsetInQueryChunk = indexOffsetInQuery + idxOffsetInChunk;
CmptsView chunkView{ cmpts.data(), cmpts.size() };
SingletonsView singletonsView{ singletons.data(), singletons.size() };

size_t J = min(chunkCapacity, num - idxOffsetInChunk);
for (size_t j = 0; j < J; j++) {
(*sys)(
w,
SingletonsView{ singletons.data(), singletons.size() },
singletonsView,
entities[j],
indexOffsetInQueryChunk + j,
chunkView
Expand All @@ -301,7 +303,7 @@ void EntityMngr::GenEntityJob(World* w, Job* job, SystemFunc* sys) const {
}
}
else {
job->emplace([this, singletons = std::move(singletons), sys, w]() {
auto work = [this, singletons = std::move(singletons), sys, w]() {
size_t indexOffsetInQuery = 0;
for (Archetype* archetype : QueryArchetypes(sys->entityQuery)) {
auto [chunkEntity, chunkCmpts, sizes] = archetype->Locate(sys->entityQuery.locator);
Expand All @@ -314,24 +316,30 @@ void EntityMngr::GenEntityJob(World* w, Job* job, SystemFunc* sys) const {
size_t idxOffsetInChunk = i * chunkCapacity;
size_t indexOffsetInQueryChunk = indexOffsetInQuery + idxOffsetInChunk;
CmptsView chunkView{ chunkCmpts[i].data(), chunkCmpts[i].size() };
SingletonsView singletonsView{ singletons.data(), singletons.size() };

size_t J = min(chunkCapacity, num - idxOffsetInChunk);
for (size_t j = 0; j < J; j++) {
(*sys)(
w,
SingletonsView{ singletons.data(), singletons.size() },
singletonsView,
chunkEntity[i][j],
indexOffsetInQueryChunk + j,
chunkView
);
);
for (size_t k = 0; k < chunkCmpts[i].size(); k++)
reinterpret_cast<uint8_t*&>(chunkCmpts[i][k].p) += sizes[k];
}
}

indexOffsetInQuery += num;
}
});
};

if (job)
job->emplace(std::move(work));
else
work();
}
}

Expand All @@ -343,22 +351,24 @@ void EntityMngr::GenChunkJob(World* w, Job* job, SystemFunc* sys) const {
return;

if (sys->IsParallel()) {
assert(job != nullptr);
for (Archetype* archetype : QueryArchetypes(sys->entityQuery)) {
size_t chunkNum = archetype->ChunkNum();
SingletonsView singletonsView{ singletons.data(), singletons.size() };

for (size_t i = 0; i < chunkNum; i++) {
job->emplace([=, singletons = singletons]() {
(*sys)(
w,
SingletonsView{ singletons.data(), singletons.size() },
singletonsView,
ChunkView{ archetype, i }
);
});
}
}
}
else {
job->emplace([this, w, sys, singletons = std::move(singletons)]() {
auto work = [this, w, sys, singletons = std::move(singletons)]() {
for (Archetype* archetype : QueryArchetypes(sys->entityQuery)) {
size_t chunkNum = archetype->ChunkNum();
SingletonsView singletonsView{ singletons.data(), singletons.size() };
Expand All @@ -371,7 +381,12 @@ void EntityMngr::GenChunkJob(World* w, Job* job, SystemFunc* sys) const {
);
}
}
});
};

if (job)
job->emplace(std::move(work));
else
work();
}
}

Expand All @@ -382,12 +397,17 @@ void EntityMngr::GenJob(World* w, Job* job, SystemFunc* sys) const {
if (!success)
return;

job->emplace([=, singletons = std::move(singletons)]() {
auto work = [=, singletons = std::move(singletons)]() {
(*sys)(
w,
SingletonsView{ singletons.data(), singletons.size() }
);
});
};

if (job)
job->emplace(std::move(work));
else
work();
}

void EntityMngr::AutoGen(World* w, Job* job, SystemFunc* sys) const {
Expand Down
12 changes: 8 additions & 4 deletions src/core/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ string World::DumpUpdateJobGraph() const {
}

void World::Run(SystemFunc* sys) {
Job job;
JobExecutor executor;
entityMngr.AutoGen(this, &job, sys);
executor.run(job).wait();
if (sys->IsParallel()) {
Job job;
JobExecutor executor;
entityMngr.AutoGen(this, &job, sys);
executor.run(job).wait();
}
else
entityMngr.AutoGen(this, nullptr, sys);
}

// after running Update
Expand Down

0 comments on commit 3cbed3e

Please sign in to comment.