Speed up searching for ecs table columns using a hash_map<uuid, column_id>
This commit is contained in:
parent
a27378a3a7
commit
dbeee752b7
3 changed files with 16 additions and 16 deletions
|
|
@ -4,6 +4,7 @@
|
|||
#include <psemek/ecs/detail/stride.hpp>
|
||||
#include <psemek/util/uuid.hpp>
|
||||
#include <psemek/util/span.hpp>
|
||||
#include <psemek/util/hash_table.hpp>
|
||||
#include <psemek/util/assert.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
|
@ -32,6 +33,13 @@ namespace psemek::ecs::detail
|
|||
return component_uuids_;
|
||||
}
|
||||
|
||||
std::optional<std::size_t> get_component_column(util::uuid const & uuid) const
|
||||
{
|
||||
if (auto it = component_uuid_to_column_.find(uuid); it != component_uuid_to_column_.end())
|
||||
return it->second;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
util::span<component_pointer const> get_component_pointers() const
|
||||
{
|
||||
return component_pointers_;
|
||||
|
|
@ -86,6 +94,7 @@ namespace psemek::ecs::detail
|
|||
|
||||
protected:
|
||||
std::vector<util::uuid> component_uuids_;
|
||||
util::hash_map<util::uuid, std::size_t> component_uuid_to_column_;
|
||||
std::vector<component_pointer> component_pointers_;
|
||||
std::size_t row_count_ = 0;
|
||||
|
||||
|
|
@ -130,6 +139,8 @@ namespace psemek::ecs::detail
|
|||
assert(sizeof...(Components) == component_uuids.size());
|
||||
component_uuids_.assign(component_uuids.begin(), component_uuids.end());
|
||||
component_pointers_.resize(sizeof...(Components));
|
||||
for (std::size_t i = 0; i < component_uuids_.size(); ++i)
|
||||
component_uuid_to_column_.insert({component_uuids_[i], i});
|
||||
}
|
||||
|
||||
template <typename ... Components>
|
||||
|
|
|
|||
|
|
@ -309,13 +309,11 @@ namespace psemek::ecs
|
|||
{
|
||||
util::uuid const uuid = Component::uuid();
|
||||
|
||||
auto const component_uuids = table_->get_component_uuids();
|
||||
auto column = table_->get_component_column(uuid);
|
||||
if (!column)
|
||||
return nullptr;
|
||||
|
||||
for (std::size_t i = 0; i < component_uuids.size(); ++i)
|
||||
if (uuid == component_uuids[i])
|
||||
return reinterpret_cast<Component *>(table_->get_component_pointers()[i].data + detail::stride<Component>() * row_);
|
||||
|
||||
return nullptr;
|
||||
return reinterpret_cast<Component *>(table_->get_component_pointers()[*column].data + detail::stride<Component>() * row_);
|
||||
}
|
||||
|
||||
template <typename Component>
|
||||
|
|
|
|||
|
|
@ -10,16 +10,7 @@ namespace psemek::ecs::detail
|
|||
entry.table = table;
|
||||
|
||||
for (auto const & uuid : component_uuids)
|
||||
{
|
||||
for (std::size_t i = 0; i < table->component_count(); ++i)
|
||||
{
|
||||
if (uuid == table->get_component_uuids()[i])
|
||||
{
|
||||
entry.column_ids.push_back(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
entry.column_ids.push_back(*(table->get_component_column(uuid)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue