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
+7 -6
View File
@@ -178,12 +178,13 @@ private:
const std::string type = AstCDType::typeToHold(items);
const std::string name = "VlRandC<" + type + ", " + cvtToStr(items) + "ULL>";
// Create or reuse (to avoid duplicates) randomization object dtype
auto it = m_randcDtypes.find(name);
if (it != m_randcDtypes.end()) return it->second;
AstCDType* newp = new AstCDType{fl, name};
v3Global.rootp()->typeTablep()->addTypesp(newp);
m_randcDtypes.emplace(name, newp);
return newp;
const auto pair = m_randcDtypes.emplace(name, nullptr);
if (pair.second) {
AstCDType* newp = new AstCDType{fl, name};
v3Global.rootp()->typeTablep()->addTypesp(newp);
pair.first->second = newp;
}
return pair.first->second;
}
AstVar* newRandcVarsp(AstVar* varp) {