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 -8
View File
@@ -77,7 +77,8 @@ private:
CheckState m_state; // State save-restored on each new coverage scope/block
AstNodeModule* m_modp = nullptr; // Current module to add statement to
bool m_inToggleOff = false; // In function/task etc
std::unordered_map<std::string, int> m_varnames; // Uniquification of inserted variable names
// Uniquification of inserted variable names
std::unordered_map<std::string, uint32_t> m_varnames;
string m_beginHier; // AstBegin hier name for user coverage points
std::unordered_map<int, LinenoSet>
m_handleLines; // All line numbers for a given m_stateHandle
@@ -141,13 +142,7 @@ private:
string traceNameForLine(AstNode* nodep, const string& type) {
string name = "vlCoverageLineTrace_" + nodep->fileline()->filebasenameNoExt() + "__"
+ cvtToStr(nodep->fileline()->lineno()) + "_" + type;
const auto it = m_varnames.find(name);
if (it == m_varnames.end()) {
m_varnames.emplace(name, 1);
} else {
const int suffix = (it->second)++;
name += "_" + cvtToStr(suffix);
}
if (const uint32_t suffix = m_varnames[name]++) name += "_" + cvtToStr(suffix);
return name;
}