Stratum: faster JSON parsing
Some checks failed
C/C++ CI / build-ubuntu (map[c:gcc-8 cpp:g++-8 flags: os:ubuntu-20.04]) (push) Has been cancelled
C/C++ CI / build-alpine-static (map[arch:aarch64 branch:latest-stable flags:-ffunction-sections -Wno-error=inline -mfix-cortex-a53-835769 -mfix-cortex-a53-843419]) (push) Has been cancelled
C/C++ CI / build-alpine-static (map[arch:riscv64 branch:edge flags:-ffunction-sections -Wno-error=inline]) (push) Has been cancelled
C/C++ CI / build-openbsd (map[architecture:x86-64 host:ubuntu-latest name:openbsd version:7.4]) (push) Has been cancelled
clang-tidy / clang-tidy (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
cppcheck / cppcheck-ubuntu (push) Has been cancelled
cppcheck / cppcheck-windows (push) Has been cancelled
Microsoft C++ Code Analysis / Analyze (push) Has been cancelled
source-snapshot / source-snapshot (push) Has been cancelled
Sync test (old) / sync-test-windows-debug-asan (push) Has been cancelled
Sync test (old) / sync-test-ubuntu-tsan (push) Has been cancelled
Sync test (old) / sync-test-ubuntu-msan (push) Has been cancelled
Sync test (old) / sync-test-ubuntu-ubsan (push) Has been cancelled
Sync test (old) / sync-test-ubuntu-asan (push) Has been cancelled
Sync test (old) / sync-test-macos (map[flags:-Og -ftrapv -target arm64-apple-macos-11 os:macos-14]) (push) Has been cancelled
Sync test (old) / sync-test-macos (map[flags:-Og -ftrapv os:macos-13]) (push) Has been cancelled
Sync test (old) / sync-test-windows-leaks (push) Has been cancelled
C/C++ CI / build-alpine-static (map[arch:x86_64 branch:latest-stable flags:-ffunction-sections -Wno-error=inline]) (push) Has been cancelled
C/C++ CI / build-ubuntu (map[c:gcc-11 cpp:g++-11 flags: os:ubuntu-20.04]) (push) Has been cancelled
C/C++ CI / build-ubuntu (map[c:gcc-12 cpp:g++-12 flags: os:ubuntu-22.04]) (push) Has been cancelled
C/C++ CI / build-ubuntu-static-libs (map[flags:-fuse-linker-plugin -ffunction-sections -Wno-error=inline]) (push) Has been cancelled
C/C++ CI / build-ubuntu-aarch64 (map[flags:-fuse-linker-plugin -ffunction-sections -mfix-cortex-a53-835769 -mfix-cortex-a53-843419 os:ubuntu-20.04]) (push) Has been cancelled
C/C++ CI / build-ubuntu-aarch64 (map[flags:-fuse-linker-plugin -ffunction-sections -mfix-cortex-a53-835769 -mfix-cortex-a53-843419 os:ubuntu-22.04]) (push) Has been cancelled
C/C++ CI / build-windows-msys2 (map[c:clang cxx:clang++ flags:-fuse-ld=lld -Wno-unused-command-line-argument -Wno-nan-infinity-disabled]) (push) Has been cancelled
C/C++ CI / build-windows-msys2 (map[c:gcc cxx:g++ flags:-ffunction-sections -Wno-error=maybe-uninitialized -Wno-error=attributes]) (push) Has been cancelled
C/C++ CI / build-windows-msbuild (map[grpc:OFF os:2019 rx:OFF upnp:OFF vs:Visual Studio 16 2019 vspath:C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise]) (push) Has been cancelled
C/C++ CI / build-windows-msbuild (map[grpc:OFF os:2019 rx:OFF upnp:ON vs:Visual Studio 16 2019 vspath:C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise]) (push) Has been cancelled
C/C++ CI / build-windows-msbuild (map[grpc:OFF os:2019 rx:ON upnp:ON vs:Visual Studio 16 2019 vspath:C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise]) (push) Has been cancelled
C/C++ CI / build-windows-msbuild (map[grpc:ON os:2019 rx:ON upnp:ON vs:Visual Studio 16 2019 vspath:C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise]) (push) Has been cancelled
C/C++ CI / build-windows-msbuild (map[grpc:ON os:2022 rx:ON upnp:ON vs:Visual Studio 17 2022 vspath:C:\Program Files\Microsoft Visual Studio\2022\Enterprise]) (push) Has been cancelled
C/C++ CI / build-macos (push) Has been cancelled
C/C++ CI / build-macos-aarch64 (push) Has been cancelled
C/C++ CI / build-freebsd (map[architecture:x86-64 host:ubuntu-latest name:freebsd version:13.3]) (push) Has been cancelled
Sync test / sync-test-windows-leaks (push) Has been cancelled
Sync test / sync-test-ubuntu-tsan (push) Has been cancelled
Sync test / sync-test-ubuntu-msan (push) Has been cancelled
Sync test / sync-test-ubuntu-ubsan (push) Has been cancelled
Sync test / sync-test-ubuntu-asan (push) Has been cancelled
Sync test / sync-test-macos (map[flags:-Og -ftrapv -target arm64-apple-macos-11 os:macos-14]) (push) Has been cancelled
Sync test / sync-test-macos (map[flags:-Og -ftrapv os:macos-13]) (push) Has been cancelled
Sync test / sync-test-windows-debug-asan (push) Has been cancelled

This commit is contained in:
SChernykh 2024-07-01 23:22:14 +02:00
parent 17de2ff4bc
commit b16cf00dae
2 changed files with 37 additions and 24 deletions

View file

@ -38,10 +38,14 @@ struct parse_wrapper<T, out_type> \
{ \
static FORCEINLINE bool parse(T& v, const char* name, out_type& out_value) \
{ \
if (v.IsObject() && v.HasMember(name)) { \
const auto& t = v[name]; \
if (t.Is##type()) { \
out_value = static_cast<out_type>(t.Get##type()); \
if (!v.IsObject()) { \
return false; \
} \
const auto t = v.FindMember(name); \
if (t != v.MemberEnd()) { \
const auto& value = t->value; \
if (value.Is##type()) { \
out_value = static_cast<out_type>(value.Get##type()); \
return true; \
} \
} \

View file

@ -1158,10 +1158,10 @@ bool StratumServer::StratumClient::on_read(char* data, uint32_t size)
return true;
}
bool StratumServer::StratumClient::process_request(char* data, uint32_t /*size*/)
bool StratumServer::StratumClient::process_request(char* data, uint32_t size)
{
rapidjson::Document doc;
if (doc.ParseInsitu(data).HasParseError()) {
if (doc.Parse(data, size).HasParseError()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid JSON request (parse error)");
return false;
}
@ -1171,23 +1171,25 @@ bool StratumServer::StratumClient::process_request(char* data, uint32_t /*size*/
return false;
}
if (!doc.HasMember("id")) {
const auto id_it = doc.FindMember("id");
if (id_it == doc.MemberEnd()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid JSON request ('id' field not found)");
return false;
}
auto& id = doc["id"];
const auto& id = id_it->value;
if (!id.IsUint()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid JSON request ('id' field is not an integer)");
return false;
}
if (!doc.HasMember("method")) {
const auto method_it = doc.FindMember("method");
if (method_it == doc.MemberEnd()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid JSON request ('method' field not found)");
return false;
}
auto& method = doc["method"];
const auto& method = method_it->value;
if (!method.IsString()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid JSON request ('method' field is not a string)");
return false;
@ -1213,23 +1215,25 @@ bool StratumServer::StratumClient::process_request(char* data, uint32_t /*size*/
bool StratumServer::StratumClient::process_login(rapidjson::Document& doc, uint32_t id)
{
if (!doc.HasMember("params")) {
const auto params_it = doc.FindMember("params");
if (params_it == doc.MemberEnd()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid JSON login request ('params' field not found)");
return false;
}
auto& params = doc["params"];
const auto& params = params_it->value;
if (!params.IsObject()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid JSON login request ('params' field is not an object)");
return false;
}
if (!params.HasMember("login")) {
const auto login_it = params.FindMember("login");
if (login_it == params.MemberEnd()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid login params ('login' field not found)");
return false;
}
auto& login = params["login"];
const auto& login = login_it->value;
if (!login.IsString()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid login params ('login' field is not a string)");
return false;
@ -1240,45 +1244,49 @@ bool StratumServer::StratumClient::process_login(rapidjson::Document& doc, uint3
bool StratumServer::StratumClient::process_submit(rapidjson::Document& doc, uint32_t id)
{
if (!doc.HasMember("params")) {
const auto params_it = doc.FindMember("params");
if (params_it == doc.MemberEnd()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid JSON submit request ('params' field not found)");
return false;
}
auto& params = doc["params"];
const auto& params = params_it->value;
if (!params.IsObject()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid JSON submit request ('params' field is not an object)");
return false;
}
if (!params.HasMember("id")) {
const auto rpcId_it = params.FindMember("id");
if (rpcId_it == params.MemberEnd()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid submit params ('id' field not found)");
return false;
}
auto& rpcId = params["id"];
const auto& rpcId = rpcId_it->value;
if (!rpcId.IsString()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid submit params ('id' field is not a string)");
return false;
}
if (!params.HasMember("job_id")) {
const auto job_id_it = params.FindMember("job_id");
if (job_id_it == params.MemberEnd()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid submit params ('job_id' field not found)");
return false;
}
auto& job_id = params["job_id"];
const auto& job_id = job_id_it->value;
if (!job_id.IsString()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid submit params ('job_id' field is not a string)");
return false;
}
if (!params.HasMember("nonce")) {
const auto nonce_it = params.FindMember("nonce");
if (nonce_it == params.MemberEnd()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid submit params ('nonce' field not found)");
return false;
}
auto& nonce = params["nonce"];
const auto& nonce = nonce_it->value;
if (!nonce.IsString()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid submit params ('nonce' field is not a string)");
return false;
@ -1289,12 +1297,13 @@ bool StratumServer::StratumClient::process_submit(rapidjson::Document& doc, uint
return false;
}
if (!params.HasMember("result")) {
const auto result_it = params.FindMember("result");
if (result_it == params.MemberEnd()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid submit params ('result' field not found)");
return false;
}
auto& result = params["result"];
const auto& result = result_it->value;
if (!result.IsString()) {
LOGWARN(4, "client " << static_cast<char*>(m_addrString) << " invalid submit params ('result' field is not a string)");
return false;