Skip to content

Commit 6ec4106

Browse files
authored
refactor: optimize string operations and improve code clarity (#451)
- Avoid redundant substr() calls in packet parsing hot-path (TServer.cpp) The previous code called substr(3) twice per packet, creating unnecessary temporary strings. Now stores the result once. - Replace .size() == 0 with .empty() for idiomatic C++ (TConsole.cpp, TLuaEngine.cpp)
2 parents b094e35 + 31122ab commit 6ec4106

3 files changed

Lines changed: 9 additions & 7 deletions

File tree

src/TConsole.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void TConsole::ChangeToRegularConsole() {
160160
}
161161

162162
bool TConsole::EnsureArgsCount(const std::vector<std::string>& args, size_t n) {
163-
if (n == 0 && args.size() != 0) {
163+
if (n == 0 && !args.empty()) {
164164
Application::Console().WriteRaw("This command expects no arguments.");
165165
return false;
166166
} else if (args.size() != n) {
@@ -198,7 +198,7 @@ void TConsole::Command_Lua(const std::string&, const std::vector<std::string>& a
198198
} else {
199199
Application::Console().WriteRaw("Lua state '" + NewStateId + "' is not a known state. Didn't switch to Lua.");
200200
}
201-
} else if (args.size() == 0) {
201+
} else if (args.empty()) {
202202
ChangeToLuaConsole(mDefaultStateId);
203203
}
204204
}
@@ -423,7 +423,7 @@ void TConsole::Command_Settings(const std::string&, const std::vector<std::strin
423423
settings set <category> <setting> <value> sets specified setting to value
424424
)";
425425

426-
if (args.size() == 0) {
426+
if (args.empty()) {
427427
beammp_errorf("No arguments specified for command 'settings'!");
428428
Application::Console().WriteRaw("BeamMP-Server Console: " + std::string(sHelpString));
429429
return;
@@ -705,7 +705,7 @@ void TConsole::RunAsCommand(const std::string& cmd, bool IgnoreNotACommand) {
705705
}
706706
}
707707
}
708-
if (NonNilFutures.size() == 0) {
708+
if (NonNilFutures.empty()) {
709709
if (!IgnoreNotACommand) {
710710
Application::Console().WriteRaw("Error: Unknown command: '" + cmd + "'. Type 'help' to see a list of valid commands.");
711711
}

src/TLuaEngine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void TLuaEngine::operator()() {
131131
}
132132
}
133133
}
134-
if (mLuaStates.size() == 0) {
134+
if (mLuaStates.empty()) {
135135
beammp_trace("No Lua states, event loop running extremely sparsely");
136136
Application::SleepSafeSeconds(10);
137137
} else {

src/TServer.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
198198
int PID = -1;
199199
int VID = -1;
200200

201-
auto MaybePidVid = GetPidVid(StringPacket.substr(3).substr(0, StringPacket.substr(3).find(':', 1)));
201+
auto pidVidPart = StringPacket.substr(3);
202+
auto MaybePidVid = GetPidVid(pidVidPart.substr(0, pidVidPart.find(':')));
202203
if (MaybePidVid) {
203204
std::tie(PID, VID) = MaybePidVid.value();
204205
}
@@ -293,7 +294,8 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
293294
int PID = -1;
294295
int VID = -1;
295296

296-
auto MaybePidVid = GetPidVid(StringPacket.substr(3).substr(0, StringPacket.substr(3).find(':', 1)));
297+
auto pidVidPart = StringPacket.substr(3);
298+
auto MaybePidVid = GetPidVid(pidVidPart.substr(0, pidVidPart.find(':')));
297299
if (MaybePidVid) {
298300
std::tie(PID, VID) = MaybePidVid.value();
299301
}

0 commit comments

Comments
 (0)