Using switch statement to improve performance

This commit is contained in:
WolverinDEV 2019-07-18 18:01:45 +02:00
parent 338ff1c399
commit 3885b426e4

View File

@ -18,7 +18,12 @@ using namespace ts::server;
extern ts::server::InstanceHandler* serverInstance;
constexpr unsigned int string_hash(const char* str, int h = 0) {
return !str[h] ? 5381 : (string_hash(str, h + 1) * 33) ^ str[h];
}
CommandResult QueryClient::handleCommand(Command& cmd) {
/*
if (cmd.command() == "exit" || cmd.command() == "quit") return this->handleCommandExit(cmd);
else if (cmd.command() == "use" || cmd.command() == "serverselect") return this->handleCommandServerSelect(cmd);
else if (cmd.command() == "serverinfo") return this->handleCommandServerInfo(cmd);
@ -49,6 +54,67 @@ CommandResult QueryClient::handleCommand(Command& cmd) {
else if (cmd.command() == "servernotifyregister") return this->handleCommandServerNotifyRegister(cmd);
else if (cmd.command() == "servernotifylist") return this->handleCommandServerNotifyList(cmd);
else if (cmd.command() == "servernotifyunregister") return this->handleCommandServerNotifyUnregister(cmd);
*/
auto command = cmd.command();
auto command_hash = string_hash(command.c_str());
switch (command_hash) {
case string_hash("exit"):
case string_hash("quit"):
return this->handleCommandExit(cmd);
case string_hash("use"):
case string_hash("serverselect"):
return this->handleCommandServerSelect(cmd);
case string_hash("serverinfo"):
return this->handleCommandServerInfo(cmd);
case string_hash("channellist"):
return this->handleCommandChannelList(cmd);
case string_hash("login"):
return this->handleCommandLogin(cmd);
case string_hash("logout"):
return this->handleCommandLogout(cmd);
case string_hash("join"):
return this->handleCommandJoin(cmd);
case string_hash("left"):
return this->handleCommandLeft(cmd);
case string_hash("globalmessage"):
case string_hash("gm"):
return this->handleCommandGlobalMessage(cmd);
case string_hash("serverlist"):
return this->handleCommandServerList(cmd);
case string_hash("servercreate"):
return this->handleCommandServerCreate(cmd);
case string_hash("serverstart"):
return this->handleCommandServerStart(cmd);
case string_hash("serverstop"):
return this->handleCommandServerStop(cmd);
case string_hash("serverdelete"):
return this->handleCommandServerDelete(cmd);
case string_hash("serveridgetbyport"):
return this->handleCommandServerIdGetByPort(cmd);
case string_hash("instanceinfo"):
return this->handleCommandInstanceInfo(cmd);
case string_hash("instanceedit"):
return this->handleCommandInstanceEdit(cmd);
case string_hash("hostinfo"):
return this->handleCommandHostInfo(cmd);
case string_hash("bindinglist"):
return this->handleCommandBindingList(cmd);
case string_hash("serversnapshotdeploy"):
return this->handleCommandServerSnapshotDeploy(cmd);
case string_hash("serversnapshotcreate"):
return this->handleCommandServerSnapshotCreate(cmd);
case string_hash("serverprocessstop"):
return this->handleCommandServerProcessStop(cmd);
case string_hash("servernotifyregister"):
return this->handleCommandServerNotifyRegister(cmd);
case string_hash("servernotifylist"):
return this->handleCommandServerNotifyList(cmd);
case string_hash("servernotifyunregister"):
return this->handleCommandServerNotifyUnregister(cmd);
default:
break;
}
return ConnectedClient::handleCommand(cmd);
}