Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use unordered maps for component look-ups in views #752

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/ignition/gazebo/detail/ComponentStorageBase.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef IGNITION_GAZEBO_DETAIL_COMPONENTSTORAGEBASE_HH_
#define IGNITION_GAZEBO_DETAIL_COMPONENTSTORAGEBASE_HH_

#include <map>
#include <unordered_map>
#include <utility>
#include <vector>
#include "ignition/gazebo/components/Component.hh"
Expand Down Expand Up @@ -212,7 +212,7 @@ namespace ignition
private: ComponentId idCounter = 0;

/// \brief Map of ComponentId to Components (see the components vector).
private: std::map<ComponentId, int> idMap;
private: std::unordered_map<ComponentId, int> idMap;

/// \brief Sequential storage of components.
public: std::vector<ComponentTypeT> components;
Expand Down
29 changes: 26 additions & 3 deletions include/ignition/gazebo/detail/View.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
#ifndef IGNITION_GAZEBO_DETAIL_VIEW_HH_
#define IGNITION_GAZEBO_DETAIL_VIEW_HH_

#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include "ignition/gazebo/components/Component.hh"
#include "ignition/gazebo/Entity.hh"
Expand Down Expand Up @@ -126,9 +126,32 @@ class IGNITION_GAZEBO_VISIBLE View
/// \brief List of entities about to be removed
public: std::set<Entity> toRemoveEntities;

/// \brief Hash functor for std::pair<Entity, ComponentTypeId>
public: struct Hasher
{
std::size_t operator()(
std::pair<Entity, ComponentTypeId> _pair) const
{
_pair.first ^= _pair.second + 0x9e3779b9 + (_pair.second << 6)
+ (_pair.second >> 2);
return _pair.first;
}
};
Comment on lines +129 to +139
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hash function was motivated by https://stackoverflow.com/a/27952689, but I'm wondering if there's a better hash function that we can use. The main concern I have with the current function is that we are implicitly converting uint64_t (_pair.first) to std::size_t. Any thoughts?


/// \brief Equality functor for std::pair<Entity, ComponentTypeId>
public: struct EqualTo
{
bool operator()(const std::pair<Entity, ComponentTypeId> &_lhs,
const std::pair<Entity, ComponentTypeId> &_rhs) const
{
return _lhs.first == _rhs.first &&
_lhs.second == _rhs.second;
}
};

/// \brief All of the components for each entity.
public: std::map<std::pair<Entity, ComponentTypeId>,
ComponentId> components;
public: std::unordered_map<std::pair<Entity, ComponentTypeId>, ComponentId,
Hasher, EqualTo> components;
};
/// \endcond
}
Expand Down