Skip to content

Commit 2602e4b

Browse files
Remove hash map allocation on every update step (backport #1005) (#1012)
* Remove hash map allocation on every update step (#1005) Signed-off-by: Luca Della Vedova <lucadv@intrinsic.ai> (cherry picked from commit 84c9ba1) --------- Signed-off-by: Luca Della Vedova <lucadv@intrinsic.ai> Co-authored-by: Luca Della Vedova <lucadv@intrinsic.ai>
1 parent a4bb8ee commit 2602e4b

6 files changed

Lines changed: 16 additions & 36 deletions

File tree

bullet-featherstone/src/Base.hh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <algorithm>
4040
#include <limits>
4141
#include <memory>
42+
#include <optional>
4243
#include <string>
4344
#include <unordered_map>
4445
#include <unordered_set>
@@ -48,6 +49,7 @@
4849

4950
#include <gz/common/Console.hh>
5051
#include <gz/math/eigen3/Conversions.hh>
52+
#include <gz/math/Pose3.hh>
5153
#include <gz/physics/Implements.hh>
5254

5355
namespace gz {
@@ -235,6 +237,8 @@ struct LinkInfo
235237
std::unordered_map<std::string, std::size_t> collisionNameToEntityId = {};
236238
// Link is either static, fixed to world, or has zero dofs
237239
bool isStaticOrFixed = false;
240+
// Cached pose from the previous physics step for performance optimization
241+
mutable std::optional<math::Pose3d> prevPose = std::nullopt;
238242
};
239243

240244
struct CollisionInfo

bullet-featherstone/src/SimulationFeatures.cc

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -370,31 +370,21 @@ void SimulationFeatures::Write(ChangedWorldPoses &_changedPoses) const
370370
_changedPoses.entries.clear();
371371
_changedPoses.entries.reserve(this->links.size());
372372

373-
std::unordered_map<std::size_t, math::Pose3d> newPoses;
374-
375373
for (const auto &[id, info] : this->links)
376374
{
377375
const auto &model = this->ReferenceInterface<ModelInfo>(info->model);
378376
WorldPose wp;
379377
wp.pose = gz::math::eigen3::convert(GetWorldTransformOfLink(*model, *info));
380378
wp.body = id;
381379

382-
auto iter = this->prevLinkPoses.find(id);
383-
if ((iter == this->prevLinkPoses.end()) ||
384-
!iter->second.Pos().Equal(wp.pose.Pos(), 1e-6) ||
385-
!iter->second.Rot().Equal(wp.pose.Rot(), 1e-6))
380+
if (!info->prevPose.has_value() ||
381+
!info->prevPose->Pos().Equal(wp.pose.Pos(), 1e-6) ||
382+
!info->prevPose->Rot().Equal(wp.pose.Rot(), 1e-6))
386383
{
387384
_changedPoses.entries.push_back(wp);
388-
newPoses[id] = wp.pose;
385+
info->prevPose = wp.pose;
389386
}
390-
else
391-
newPoses[id] = iter->second;
392387
}
393-
394-
// Save the new poses so that they can be used to check for updates in the
395-
// next iteration. Re-setting this->prevLinkPoses with the contents of
396-
// newPoses ensures that we aren't caching data for links that were removed
397-
this->prevLinkPoses = std::move(newPoses);
398388
}
399389
} // namespace bullet_featherstone
400390
} // namespace physics

bullet-featherstone/src/SimulationFeatures.hh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ class SimulationFeatures :
5757

5858
public: std::vector<ContactInternal> GetContactsFromLastStep(
5959
const Identity &_worldID) const override;
60-
61-
/// \brief link poses from the most recent pose change/update.
62-
/// The key is the link's ID, and the value is the link's pose
63-
private: mutable std::unordered_map<std::size_t, math::Pose3d> prevLinkPoses;
6460
};
6561

6662
} // namespace bullet_featherstone

dartsim/src/Base.hh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <gz/common/Console.hh>
3838
#include <gz/math/eigen3/Conversions.hh>
3939
#include <gz/math/Inertial.hh>
40+
#include <gz/math/Pose3.hh>
4041
#include <gz/physics/detail/EntityStorage.hh>
4142
#include <gz/physics/Implements.hh>
4243

@@ -81,6 +82,9 @@ struct LinkInfo
8182
/// \brief The total link inertia, which may be split between the `link` and
8283
/// `weldedNodes` body nodes.
8384
std::optional<math::Inertiald> inertial;
85+
/// \brief Cached pose from the previous physics step, used for performance
86+
/// optimization.
87+
std::optional<math::Pose3d> prevPose;
8488
};
8589

8690
struct JointInfo

dartsim/src/SimulationFeatures.cc

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ void SimulationFeatures::Write(ChangedWorldPoses &_changedPoses) const
166166
_changedPoses.entries.clear();
167167
_changedPoses.entries.reserve(this->links.size());
168168

169-
std::unordered_map<std::size_t, math::Pose3d> newPoses;
170-
171169
for (const auto &[id, info] : this->links.idToObject)
172170
{
173171
// make sure the link exists
@@ -180,23 +178,15 @@ void SimulationFeatures::Write(ChangedWorldPoses &_changedPoses) const
180178

181179
// If the link's pose is new or has changed, save this new pose and
182180
// add it to the output poses. Otherwise, keep the existing link pose
183-
auto iter = this->prevLinkPoses.find(id);
184-
if ((iter == this->prevLinkPoses.end()) ||
185-
!iter->second.Pos().Equal(wp.pose.Pos(), 1e-6) ||
186-
!iter->second.Rot().Equal(wp.pose.Rot(), 1e-6))
181+
if (!info->prevPose.has_value() ||
182+
!info->prevPose->Pos().Equal(wp.pose.Pos(), 1e-6) ||
183+
!info->prevPose->Rot().Equal(wp.pose.Rot(), 1e-6))
187184
{
188185
_changedPoses.entries.push_back(wp);
189-
newPoses[id] = wp.pose;
186+
info->prevPose = wp.pose;
190187
}
191-
else
192-
newPoses[id] = iter->second;
193188
}
194189
}
195-
196-
// Save the new poses so that they can be used to check for updates in the
197-
// next iteration. Re-setting this->prevLinkPoses with the contents of
198-
// newPoses ensures that we aren't caching data for links that were removed
199-
this->prevLinkPoses = std::move(newPoses);
200190
}
201191

202192
SimulationFeatures::RayIntersection

dartsim/src/SimulationFeatures.hh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,6 @@ class SimulationFeatures :
123123
const LinearVector3d &_from,
124124
const LinearVector3d &_end) const override;
125125

126-
/// \brief link poses from the most recent pose change/update.
127-
/// The key is the link's ID, and the value is the link's pose
128-
private: mutable std::unordered_map<std::size_t, math::Pose3d> prevLinkPoses;
129-
130126
private: std::optional<ContactInternal> convertContact(
131127
const dart::collision::Contact& _contact) const;
132128

0 commit comments

Comments
 (0)