Console: Avoid unnecessary copies and lookups within executeCommand()

We can make use of the result of a find() call to avoid performing two
redundant lookups into the command map and also use std::move to avoid
performing potentially reallocating copies.
This commit is contained in:
Lioncash 2019-10-21 01:26:19 -04:00
parent 7fbdf384de
commit 96721d96ae
1 changed files with 11 additions and 9 deletions

View File

@ -100,10 +100,11 @@ void Console::executeString(const std::string& str) {
isInLiteral = false; isInLiteral = false;
curLiteral.clear(); curLiteral.clear();
} }
} else if (isInLiteral) } else if (isInLiteral) {
curLiteral += arg; curLiteral += arg;
else } else {
args.push_back(arg); args.push_back(std::move(arg));
}
} }
if (isInLiteral) { if (isInLiteral) {
@ -111,7 +112,7 @@ void Console::executeString(const std::string& str) {
report(Level::Warning, fmt("Unterminated string literal")); report(Level::Warning, fmt("Unterminated string literal"));
return; return;
} }
args.push_back(curLiteral); args.push_back(std::move(curLiteral));
} }
std::string commandName = args[0]; std::string commandName = args[0];
@ -119,8 +120,8 @@ void Console::executeString(const std::string& str) {
std::string lowComName = commandName; std::string lowComName = commandName;
athena::utility::tolower(lowComName); athena::utility::tolower(lowComName);
if (m_commands.find(lowComName) != m_commands.end()) { if (const auto iter = m_commands.find(lowComName); iter != m_commands.end()) {
const SConsoleCommand& cmd = m_commands[lowComName]; const SConsoleCommand& cmd = iter->second;
if (bool(cmd.m_flags & SConsoleCommand::ECommandFlags::Developer) && !com_developer->toBoolean()) { if (bool(cmd.m_flags & SConsoleCommand::ECommandFlags::Developer) && !com_developer->toBoolean()) {
report(Level::Error, fmt("This command can only be executed in developer mode"), commandName); report(Level::Error, fmt("This command can only be executed in developer mode"), commandName);
return; return;
@ -130,15 +131,16 @@ void Console::executeString(const std::string& str) {
report(Level::Error, fmt("This command can only be executed with cheats enabled"), commandName); report(Level::Error, fmt("This command can only be executed with cheats enabled"), commandName);
return; return;
} }
m_commands[lowComName].m_func(this, args); cmd.m_func(this, args);
} else if (const CVar* cv = m_cvarMgr->findCVar(commandName)) { } else if (const CVar* cv = m_cvarMgr->findCVar(commandName)) {
args.insert(args.begin(), commandName); args.insert(args.begin(), std::move(commandName));
if (args.size() > 1) if (args.size() > 1)
m_cvarMgr->setCVar(this, args); m_cvarMgr->setCVar(this, args);
else else
m_cvarMgr->getCVar(this, args); m_cvarMgr->getCVar(this, args);
} else } else {
report(Level::Error, fmt("Command '{}' is not valid!"), commandName); report(Level::Error, fmt("Command '{}' is not valid!"), commandName);
}
} }
} }