Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(skymp5-server): replace stringstream with standard C I/O functions #1672

Merged
merged 8 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions skymp5-server/cpp/server_guest_lib/FormDesc.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
#include "FormDesc.h"
#include <sstream>
#include <cstdio>

std::string FormDesc::ToString(char delimiter) const
{
std::stringstream ss;
ss << std::hex << shortFormId;
if (!file.empty())
ss << delimiter << file;
return ss.str();
auto fullFmt = "%0x%c%s";
auto idFmt = "%0x";
size_t size = !file.empty()
? std::snprintf(nullptr, 0, fullFmt, shortFormId, delimiter, file.c_str())
: std::snprintf(nullptr, 0, idFmt, shortFormId);

std::string buffer;
buffer.resize(size);

if (!file.empty()) {
std::sprintf(buffer.data(), fullFmt, shortFormId, delimiter, file.c_str());
} else {
std::sprintf(buffer.data(), idFmt, shortFormId);
}
return buffer;
}

FormDesc FormDesc::FromString(std::string str, char delimiter)
{
for (auto& ch : str)
if (ch == delimiter)
ch = ' ';

std::istringstream ss(str);
FormDesc res;
ss >> std::hex >> res.shortFormId >> res.file;
std::string id, file;

if (str.find(delimiter) == std::string::npos) {
std::sscanf(str.data(), "%x", &res.shortFormId);
return res;
}

for (auto it = str.begin(); it != str.end(); ++it) {
if (*it == delimiter) {
id = { str.begin(), it };
res.file = { it + 1, str.end() };
break;
}
}

std::sscanf(id.data(), "%x", &res.shortFormId);
return res;
}

Expand Down Expand Up @@ -75,3 +95,10 @@ FormDesc FormDesc::FromFormId(uint32_t formId,
}
return res;
}

static const FormDesc kTamriel = FormDesc::FromString("3c:Skyrim.esm");

FormDesc FormDesc::Tamriel()
{
return kTamriel;
}
2 changes: 1 addition & 1 deletion skymp5-server/cpp/server_guest_lib/FormDesc.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class FormDesc
std::make_tuple(right.shortFormId, right.file);
}

static FormDesc Tamriel() { return FromString("3c:Skyrim.esm"); }
static FormDesc Tamriel();

uint32_t shortFormId = 0;
std::string file;
Expand Down
Loading