Avoid double traversal of maps

The typical find/if-not-exists-insert pattern can be achieved with 1
lookup instead of 2 using emplace with a sentinel value. Also maps value
initialize their values when inserted with the [] operator, this is
defined and so there is no need to explicitly insert zeroes for integer
values.
This commit is contained in:
Geza Lore
2023-10-28 13:41:43 +01:00
parent 30318a6654
commit d60f180f43
25 changed files with 240 additions and 311 deletions
+4 -5
View File
@@ -155,10 +155,9 @@ class SchedGraphBuilder final : public VNVisitor {
// rst), so we use a hash map to get the unique SchedSenVertex. (Note: This creates
// separate vertices for ET_CHANGED and ET_HYBRID over the same expression, but that is
// OK for now).
auto it = m_senVertices.find(*senItemp);
const auto pair = m_senVertices.emplace(*senItemp, nullptr);
// If it does not exist, create it
if (it == m_senVertices.end()) {
if (pair.second) {
// Create the vertex
SchedSenVertex* const vtxp = new SchedSenVertex{m_graphp, senItemp};
@@ -168,11 +167,11 @@ class SchedGraphBuilder final : public VNVisitor {
});
// Store back to hash map so we can find it next time
it = m_senVertices.emplace(*senItemp, vtxp).first;
pair.first->second = vtxp;
}
// Cache sensitivity vertex
senItemp->user1p(it->second);
senItemp->user1p(pair.first->second);
}
return senItemp->user1u().to<SchedSenVertex*>();
}