Skip to content

Commit 125069b

Browse files
stefan-iligcbot
authored andcommitted
Avoid loops during ensureVariableNameUnique check
Avoid previously done incremental check while searching for the next available variable sufix by using map.
1 parent 7610176 commit 125069b

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

visa/VISAKernel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,8 @@ class VISAKernelImpl : public VISAFunction {
14031403

14041404
// TODO: this should be merged and re-worked to fit into the symbol table
14051405
// scheme
1406-
std::unordered_set<std::string> varNames;
1406+
// Save variable name and its next free sufix
1407+
std::unordered_map <std::string, size_t> varNames;
14071408
std::unordered_set<std::string> reservedNames;
14081409

14091410
int m_vISAInstCount;

visa/VISAKernelImpl.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -863,16 +863,16 @@ void VISAKernelImpl::ensureVariableNameUnique(const char *&varName) {
863863

864864
// case 4: if "x" already exists, then use "x_#" where # is 0,1,..
865865
std::string varNameS = escdName.str();
866-
if (varNames.find(varNameS) != varNames.end()) {
867-
// not unqiue, add a counter until it is unique
868-
int instance = 0;
869-
do {
870-
std::stringstream ss;
871-
ss << escdName.str() << '_' << instance++;
872-
varNameS = ss.str();
873-
} while (varNames.find(varNameS) != varNames.end());
874-
}
875-
varNames.insert(varNameS);
866+
if (auto it = varNames.find(varNameS); it != varNames.end()) {
867+
size_t instanceNumber = it->second;
868+
varNames[varNameS] = instanceNumber + 1;
869+
870+
std::stringstream ss;
871+
ss << escdName.str() << '_' << instanceNumber;
872+
varNameS = ss.str();
873+
} else {
874+
varNames.emplace(varNameS, 0);
875+
}
876876

877877
char *buf = (char *)m_mem.alloc(varNameS.size() + 1);
878878
memcpy_s(buf, varNameS.size(), varNameS.c_str(), varNameS.size());

0 commit comments

Comments
 (0)