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 -6
View File
@@ -187,12 +187,9 @@ class EmitCSyms final : EmitCBaseVisitorConst {
const auto scpit = m_vpiScopeCandidates.find(scopeSymString(scp));
if ((scpit != m_vpiScopeCandidates.end())
&& (m_scopeNames.find(scp) == m_scopeNames.end())) {
const auto scopeNameit = m_scopeNames.find(scpit->second.m_symName);
if (scopeNameit == m_scopeNames.end()) {
m_scopeNames.emplace(scpit->second.m_symName, scpit->second);
} else {
scopeNameit->second.m_type = scpit->second.m_type;
}
// If not in m_scopeNames, add it, otherwise just update m_type
const auto pair = m_scopeNames.emplace(scpit->second.m_symName, scpit->second);
if (!pair.second) pair.first->second.m_type = scpit->second.m_type;
}
string::size_type pos = scp.rfind("__DOT__");
if (pos == string::npos) {