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
+3 -3
View File
@@ -306,12 +306,12 @@ public:
//######################################################################
void V3HierBlockPlan::add(const AstNodeModule* modp, const std::vector<AstVar*>& gparams) {
const iterator it = m_blocks.find(modp);
if (it == m_blocks.end()) {
const auto pair = m_blocks.emplace(modp, nullptr);
if (pair.second) {
V3HierBlock* hblockp = new V3HierBlock{modp, gparams};
UINFO(3, "Add " << modp->prettyNameQ() << " with " << gparams.size() << " parameters"
<< std::endl);
m_blocks.emplace(modp, hblockp);
pair.first->second = hblockp;
}
}