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
+10 -14
View File
@@ -145,30 +145,26 @@ private:
int width /*0==fromoldvar*/, AstNodeDType* newdtypep) {
// Because we've already scoped it, we may need to add both the AstVar and the AstVarScope
UASSERT_OBJ(oldvarscp->scopep(), oldvarscp, "Var unscoped");
AstVar* varp;
FileLine* const flp = oldvarscp->fileline();
AstNodeModule* const addmodp = oldvarscp->scopep()->modp();
// We need a new AstVar, but only one for all scopes, to match the new AstVarScope
const auto it = m_modVarMap.find(std::make_pair(addmodp, name));
if (it != m_modVarMap.end()) {
// Created module's AstVar earlier under some other scope
varp = it->second;
} else {
const auto pair = m_modVarMap.emplace(std::make_pair(addmodp, name), nullptr);
if (pair.second) {
AstVar* varp = nullptr;
if (newdtypep) {
varp = new AstVar{oldvarscp->fileline(), VVarType::BLOCKTEMP, name, newdtypep};
varp = new AstVar{flp, VVarType::BLOCKTEMP, name, newdtypep};
} else if (width == 0) {
varp = new AstVar{oldvarscp->fileline(), VVarType::BLOCKTEMP, name,
oldvarscp->varp()};
varp = new AstVar{flp, VVarType::BLOCKTEMP, name, oldvarscp->varp()};
varp->dtypeFrom(oldvarscp);
} else { // Used for vset and dimensions, so can zero init
varp = new AstVar{oldvarscp->fileline(), VVarType::BLOCKTEMP, name,
VFlagBitPacked{}, width};
varp = new AstVar{flp, VVarType::BLOCKTEMP, name, VFlagBitPacked{}, width};
}
addmodp->addStmtsp(varp);
m_modVarMap.emplace(std::make_pair(addmodp, name), varp);
pair.first->second = varp;
}
AstVar* const varp = pair.first->second;
AstVarScope* const varscp
= new AstVarScope{oldvarscp->fileline(), oldvarscp->scopep(), varp};
AstVarScope* const varscp = new AstVarScope{flp, oldvarscp->scopep(), varp};
oldvarscp->scopep()->addVarsp(varscp);
return varscp;
}