Skip to content

Commit

Permalink
Merge pull request #413 from AnthonyVH/anthonyvh/fix_reserve_performance
Browse files Browse the repository at this point in the history
Fix performance issues for vector-based storage

Also remove requirement for a mutable graph on vertex_by_property().
  • Loading branch information
jeremy-murphy authored Jan 22, 2025
2 parents ce1daeb + 15b587a commit a372a07
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
15 changes: 8 additions & 7 deletions include/boost/graph/detail/adjacency_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2228,9 +2228,10 @@ inline typename Config::vertex_descriptor add_vertex(
vec_adj_list_impl< Graph, Config, Base >& g_)
{
Graph& g = static_cast< Graph& >(g_);
g.m_vertices.resize(g.m_vertices.size() + 1);
g.added_vertex(g.m_vertices.size() - 1);
return g.m_vertices.size() - 1;
auto const added_descriptor = g.m_vertices.size();
g.m_vertices.emplace_back();
g.added_vertex(added_descriptor);
return added_descriptor;
}

template < class Graph, class Config, class Base >
Expand All @@ -2243,10 +2244,10 @@ inline typename Config::vertex_descriptor add_vertex(
if (optional< vertex_descriptor > v
= g.vertex_by_property(get_property_value(p, vertex_bundle)))
return *v;
typedef typename Config::stored_vertex stored_vertex;
g.m_vertices.push_back(stored_vertex(p));
g.added_vertex(g.m_vertices.size() - 1);
return g.m_vertices.size() - 1;
auto const added_descriptor = g.m_vertices.size();
g.m_vertices.emplace_back(p);
g.added_vertex(added_descriptor);
return added_descriptor;
}

// Here we override the directed_graph_helper add_edge() function
Expand Down
8 changes: 4 additions & 4 deletions include/boost/graph/named_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,12 @@ namespace graph

/// Extract the name from a vertex property instance
typename extract_name_type::result_type extract_name(
const bundled_vertex_property_type& property);
const bundled_vertex_property_type& property) const;

/// Search for a vertex that has the given property (based on its
/// name)
optional< vertex_descriptor > vertex_by_property(
const bundled_vertex_property_type& property);
const bundled_vertex_property_type& property) const;

/// Mapping from names to vertices
named_vertices_type named_vertices;
Expand Down Expand Up @@ -340,15 +340,15 @@ namespace graph

template < BGL_NAMED_GRAPH_PARAMS >
typename BGL_NAMED_GRAPH::extract_name_type::result_type
BGL_NAMED_GRAPH::extract_name(const bundled_vertex_property_type& property)
BGL_NAMED_GRAPH::extract_name(const bundled_vertex_property_type& property) const
{
return named_vertices.key_extractor().extract(property);
}

template < BGL_NAMED_GRAPH_PARAMS >
optional< typename BGL_NAMED_GRAPH::vertex_descriptor >
BGL_NAMED_GRAPH::vertex_by_property(
const bundled_vertex_property_type& property)
const bundled_vertex_property_type& property) const
{
return find_vertex(extract_name(property), *this);
}
Expand Down
2 changes: 1 addition & 1 deletion include/boost/graph/transitive_closure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void transitive_closure(const Graph& g, GraphTC& tc,
cg_vertex v = *i;
if (!in_a_chain[v])
{
chains.resize(chains.size() + 1);
chains.emplace_back();
std::vector< cg_vertex >& chain = chains.back();
for (;;)
{
Expand Down
2 changes: 1 addition & 1 deletion include/boost/graph/vector_as_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ void remove_edge_if(Predicate p, std::vector< EdgeList, Allocator >& g)
template < class EdgeList, class Allocator >
typename EdgeList::value_type add_vertex(std::vector< EdgeList, Allocator >& g)
{
g.resize(g.size() + 1);
g.emplace_back();
return g.size() - 1;
}

Expand Down

0 comments on commit a372a07

Please sign in to comment.