Get rid of double lookup in TopoSort::node(). This speeds up typical TopoSort time overall by ~10%.

This commit is contained in:
Rasmus Munk Larsen 2023-10-06 12:53:05 -07:00
parent 6a5799cc2e
commit bc0df04e06
1 changed files with 5 additions and 8 deletions

View File

@ -159,15 +159,12 @@ template <typename T, typename C = std::less<T>, typename OPS = hash_ops<T>> cla
int node(T n)
{
auto it = node_to_index.find(n);
if (it == node_to_index.end()) {
int index = static_cast<size_t>(nodes.size());
node_to_index[n] = index;
nodes.push_back(n);
edges.push_back(std::set<int, IndirectCmp>(indirect_cmp));
return index;
auto rv = node_to_index.emplace(n, static_cast<int>(nodes.size()));
if (rv.second) {
nodes.push_back(n);
edges.push_back(std::set<int, IndirectCmp>(indirect_cmp));
}
return it->second;
return rv.first->second;
}
void edge(int l_index, int r_index) { edges[r_index].insert(l_index); }