From 3885b426e4e0bd5efc281b7c02513cd4c3a913c6 Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Thu, 18 Jul 2019 18:01:45 +0200 Subject: [PATCH] Using switch statement to improve performance --- .../src/client/query/QueryClientCommands.cpp | 116 ++++++++++++++---- 1 file changed, 91 insertions(+), 25 deletions(-) diff --git a/server/src/client/query/QueryClientCommands.cpp b/server/src/client/query/QueryClientCommands.cpp index f80f960..cc661b4 100644 --- a/server/src/client/query/QueryClientCommands.cpp +++ b/server/src/client/query/QueryClientCommands.cpp @@ -18,37 +18,103 @@ 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); - else if (cmd.command() == "channellist") return this->handleCommandChannelList(cmd); - else if (cmd.command() == "login") return this->handleCommandLogin(cmd); - else if (cmd.command() == "logout") return this->handleCommandLogout(cmd); - else if (cmd.command() == "join") return this->handleCommandJoin(cmd); - else if (cmd.command() == "left") return this->handleCommandLeft(cmd); - else if (cmd.command() == "globalmessage" || cmd.command() == "gm") return this->handleCommandGlobalMessage(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); + else if (cmd.command() == "channellist") return this->handleCommandChannelList(cmd); + else if (cmd.command() == "login") return this->handleCommandLogin(cmd); + else if (cmd.command() == "logout") return this->handleCommandLogout(cmd); + else if (cmd.command() == "join") return this->handleCommandJoin(cmd); + else if (cmd.command() == "left") return this->handleCommandLeft(cmd); + else if (cmd.command() == "globalmessage" || cmd.command() == "gm") return this->handleCommandGlobalMessage(cmd); - else if (cmd.command() == "serverlist") return this->handleCommandServerList(cmd); - else if (cmd.command() == "servercreate") return this->handleCommandServerCreate(cmd); - else if (cmd.command() == "serverstart") return this->handleCommandServerStart(cmd); - else if (cmd.command() == "serverstop") return this->handleCommandServerStop(cmd); - else if (cmd.command() == "serverdelete") return this->handleCommandServerDelete(cmd); - else if (cmd.command() == "serveridgetbyport") return this->handleCommandServerIdGetByPort(cmd); - else if (cmd.command() == "instanceinfo") return this->handleCommandInstanceInfo(cmd); - else if (cmd.command() == "instanceedit") return this->handleCommandInstanceEdit(cmd); - else if (cmd.command() == "hostinfo") return this->handleCommandHostInfo(cmd); + else if (cmd.command() == "serverlist") return this->handleCommandServerList(cmd); + else if (cmd.command() == "servercreate") return this->handleCommandServerCreate(cmd); + else if (cmd.command() == "serverstart") return this->handleCommandServerStart(cmd); + else if (cmd.command() == "serverstop") return this->handleCommandServerStop(cmd); + else if (cmd.command() == "serverdelete") return this->handleCommandServerDelete(cmd); + else if (cmd.command() == "serveridgetbyport") return this->handleCommandServerIdGetByPort(cmd); + else if (cmd.command() == "instanceinfo") return this->handleCommandInstanceInfo(cmd); + else if (cmd.command() == "instanceedit") return this->handleCommandInstanceEdit(cmd); + else if (cmd.command() == "hostinfo") return this->handleCommandHostInfo(cmd); - else if (cmd.command() == "bindinglist") return this->handleCommandBindingList(cmd); + else if (cmd.command() == "bindinglist") return this->handleCommandBindingList(cmd); - else if (cmd.command() == "serversnapshotdeploy") return this->handleCommandServerSnapshotDeploy(cmd); - else if (cmd.command() == "serversnapshotcreate") return this->handleCommandServerSnapshotCreate(cmd); + else if (cmd.command() == "serversnapshotdeploy") return this->handleCommandServerSnapshotDeploy(cmd); + else if (cmd.command() == "serversnapshotcreate") return this->handleCommandServerSnapshotCreate(cmd); - else if (cmd.command() == "serverprocessstop") return this->handleCommandServerProcessStop(cmd); + else if (cmd.command() == "serverprocessstop") return this->handleCommandServerProcessStop(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); + 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); }