Skip to content

time wasted in _graph.emplace #3167

@mangoschorle

Description

@mangoschorle

https://github.com/ros-planning/navigation2/blob/634d2e3d9b6bde5558c90fe6583d52b3ed66cf55/nav2_smac_planner/src/a_star.cpp#L122
can be improved.
emplace_back will always construct the value even if it is already in the graph. So if it is in the graph, it is created and immediately afterwards destroyed. (see https://en.cppreference.com/w/cpp/container/unordered_map/emplace)

We could spare unnecessary constructions doing sth like this

  auto iter = graph_.find(index);
  if (iter == graph_.end()) {
    return &(std::get<0>(graph_.emplace(std::piecewise_construct, std::forward_as_tuple(index),
                                        std::forward_as_tuple(index /*initializer list of node t or NodeT(index)*/)))
                 ->second);
  }
  return &(iter->second);

Another thing I don't understand is why AStar reserves _graph.reserve(100000);
Seems a bit random to me. Why not reserving max_iterations?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions