Skip to content

Commit

Permalink
use small_vector for Locate
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Mar 15, 2021
1 parent 4f6e328 commit f9635fc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
6 changes: 3 additions & 3 deletions include/UECS/EntityMngr.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ namespace Ubpa::UECS {
std::size_t TotalEntityNum() const noexcept { return entityTable.size() - entityTableFreeEntry.size(); }
std::size_t EntityNum(const EntityQuery&) const;
// use entry in reverse
const std::vector<std::size_t>& GetEntityFreeEntries() const noexcept { return entityTableFreeEntry; }
std::size_t GetEntityVersion(std::size_t idx) const noexcept { return entityTable.at(idx).version; }
std::span<const std::size_t> GetEntityFreeEntries() const noexcept { return { entityTableFreeEntry.data(), entityTableFreeEntry.size() }; }
std::size_t GetEntityVersion(std::size_t idx) const noexcept { return entityTable[idx].version; }

bool IsSingleton(TypeID) const;
Entity GetSingletonEntity(TypeID) const;
Expand Down Expand Up @@ -116,7 +116,7 @@ namespace Ubpa::UECS {
Archetype* AttachWithoutInit(Entity);
Archetype* AttachWithoutInit(Entity, std::span<const TypeID> types);

std::vector<CmptAccessPtr> LocateSingletons(const SingletonLocator&) const;
small_vector<CmptAccessPtr, 16> LocateSingletons(const SingletonLocator&) const;

const std::set<Archetype*>& QueryArchetypes(const EntityQuery&) const;
mutable std::unordered_map<EntityQuery, std::set<Archetype*>> queryCache;
Expand Down
6 changes: 5 additions & 1 deletion include/UECS/details/Archetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ namespace Ubpa::UECS {
static Archetype* Remove(const Archetype* from, std::span<const TypeID> types);

// Entity + Components
std::tuple<std::vector<Entity*>, std::vector<std::vector<CmptAccessPtr>>, std::vector<std::size_t>>
std::tuple<
small_vector<Entity*, 16>,
small_vector<small_vector<CmptAccessPtr, 16>, 16>,
small_vector<std::size_t, 16>
>
Locate(const CmptLocator& locator) const;

// nullptr if not contains
Expand Down
13 changes: 9 additions & 4 deletions src/core/Archetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,20 @@ std::size_t Archetype::Instantiate(Entity e, std::size_t srcIdx) {
return dstIdx;
}

tuple<vector<Entity*>, vector<vector<CmptAccessPtr>>, vector<std::size_t>> Archetype::Locate(const CmptLocator& locator) const {
std::tuple<
Ubpa::small_vector<Entity*, 16>,
Ubpa::small_vector<Ubpa::small_vector<CmptAccessPtr, 16>, 16>,
Ubpa::small_vector<std::size_t, 16>
>
Archetype::Locate(const CmptLocator& locator) const {
assert(types.IsMatch(locator));

const std::size_t numChunk = chunks.size();
const std::size_t numType = locator.AccessTypeIDs().size();
const std::size_t offsetEntity = Offsetof(TypeID_of<Entity>);

vector<vector<CmptAccessPtr>> chunkCmpts(numChunk);
vector<Entity*> chunkEntity(numChunk);
Ubpa::small_vector<Ubpa::small_vector<CmptAccessPtr, 16>, 16> chunkCmpts(numChunk);
Ubpa::small_vector<Entity*, 16> chunkEntity(numChunk);

for (std::size_t i = 0; i < numChunk; i++) {
byte* data = chunks[i]->Data();
Expand All @@ -222,7 +227,7 @@ tuple<vector<Entity*>, vector<vector<CmptAccessPtr>>, vector<std::size_t>> Arche
chunkEntity[i] = reinterpret_cast<Entity*>(data + offsetEntity);
}

vector<std::size_t> sizes;
Ubpa::small_vector<std::size_t, 16> sizes;
sizes.reserve(numType);
for (const auto& type : locator.AccessTypeIDs())
sizes.push_back(cmptTraits.Sizeof(type));
Expand Down
4 changes: 2 additions & 2 deletions src/core/EntityMngr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ void EntityMngr::Destroy(Entity e) {
RecycleEntityEntry(e);
}

vector<CmptAccessPtr> EntityMngr::LocateSingletons(const SingletonLocator& locator) const {
Ubpa::small_vector<CmptAccessPtr, 16> EntityMngr::LocateSingletons(const SingletonLocator& locator) const {
std::size_t numSingletons = 0;
vector<CmptAccessPtr> rst;
small_vector<CmptAccessPtr, 16> rst;
rst.reserve(locator.SingletonTypes().size());
for (const auto& t : locator.SingletonTypes()) {
auto ptr = GetSingleton(t);
Expand Down

0 comments on commit f9635fc

Please sign in to comment.