From 4aa9845f2481d93614c2768a10220e4778518959 Mon Sep 17 00:00:00 2001 From: Timothy Liu Date: Sat, 20 May 2023 13:49:18 +0800 Subject: [PATCH 1/4] perf(capi): :zap: use move semantics to optimize string --- CAPI/cpp/API/include/constants.h | 2 +- CAPI/cpp/API/src/API.cpp | 16 ++++++++-------- CAPI/cpp/API/src/Communication.cpp | 2 +- CAPI/cpp/API/src/DebugAPI.cpp | 16 ++++++++-------- CAPI/cpp/API/src/logic.cpp | 2 +- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/CAPI/cpp/API/include/constants.h b/CAPI/cpp/API/include/constants.h index 51d2034..a43b872 100644 --- a/CAPI/cpp/API/include/constants.h +++ b/CAPI/cpp/API/include/constants.h @@ -166,7 +166,7 @@ namespace Constants struct Robot { SCCI int moveSpeed = basicStudentSpeed; - SCCI int maxHp = basicHp * 0.4; + SCCI int maxHp = basicHp * 2 / 5; SCCI int maxAddiction = basicMaxGamingAddiction * 0; SCCI int fixSpeed = basicFixSpeed; SCCI int encourageSpeed = 0; diff --git a/CAPI/cpp/API/src/API.cpp b/CAPI/cpp/API/src/API.cpp index 49138a6..f397848 100644 --- a/CAPI/cpp/API/src/API.cpp +++ b/CAPI/cpp/API/src/API.cpp @@ -192,26 +192,26 @@ std::future TrickerAPI::EndAllAction() std::future StudentAPI::SendTextMessage(int64_t toID, std::string message) { - return std::async(std::launch::async, [=]() - { return logic.SendMessage(toID, message, false); }); + return std::async(std::launch::async, [=, message = std::move(message)]() + { return logic.SendMessage(toID, std::move(message), false); }); } std::future TrickerAPI::SendTextMessage(int64_t toID, std::string message) { - return std::async(std::launch::async, [=]() - { return logic.SendMessage(toID, message, false); }); + return std::async(std::launch::async, [=, message = std::move(message)]() + { return logic.SendMessage(toID, std::move(message), false); }); } std::future StudentAPI::SendBinaryMessage(int64_t toID, std::string message) { - return std::async(std::launch::async, [=]() - { return logic.SendMessage(toID, message, false); }); + return std::async(std::launch::async, [=, message = std::move(message)]() + { return logic.SendMessage(toID, std::move(message), false); }); } std::future TrickerAPI::SendBinaryMessage(int64_t toID, std::string message) { - return std::async(std::launch::async, [=]() - { return logic.SendMessage(toID, message, false); }); + return std::async(std::launch::async, [=, message = std::move(message)]() + { return logic.SendMessage(toID, std::move(message), false); }); } bool StudentAPI::HaveMessage() diff --git a/CAPI/cpp/API/src/Communication.cpp b/CAPI/cpp/API/src/Communication.cpp index 09156d2..f7ec7b7 100644 --- a/CAPI/cpp/API/src/Communication.cpp +++ b/CAPI/cpp/API/src/Communication.cpp @@ -82,7 +82,7 @@ bool Communication::SendMessage(int64_t toID, std::string message, bool binary, { protobuf::BoolRes sendMessageResult; ClientContext context; - auto request = THUAI62Proto::THUAI62ProtobufSend(message, toID, binary, playerID); + auto request = THUAI62Proto::THUAI62ProtobufSend(std::move(message), toID, binary, playerID); auto status = THUAI6Stub->SendMessage(&context, request, &sendMessageResult); if (status.ok()) return sendMessageResult.act_success(); diff --git a/CAPI/cpp/API/src/DebugAPI.cpp b/CAPI/cpp/API/src/DebugAPI.cpp index b56cd4c..b343075 100644 --- a/CAPI/cpp/API/src/DebugAPI.cpp +++ b/CAPI/cpp/API/src/DebugAPI.cpp @@ -355,8 +355,8 @@ std::future TrickerDebugAPI::EndAllAction() std::future StudentDebugAPI::SendTextMessage(int64_t toID, std::string message) { logger->info("SendTextMessage: toID = {}, message = {}, called at {}ms", toID, message, Time::TimeSinceStart(startPoint)); - return std::async(std::launch::async, [=]() - { auto result = logic.SendMessage(toID, message, false); + return std::async(std::launch::async, [=, message = std::move(message)]() + { auto result = logic.SendMessage(toID, std::move(message), false); if (!result) logger->warn("SendTextMessage: failed at {}ms", Time::TimeSinceStart(startPoint)); return result; }); @@ -365,8 +365,8 @@ std::future StudentDebugAPI::SendTextMessage(int64_t toID, std::string mes std::future TrickerDebugAPI::SendTextMessage(int64_t toID, std::string message) { logger->info("SendTextMessage: toID = {}, message = {}, called at {}ms", toID, message, Time::TimeSinceStart(startPoint)); - return std::async(std::launch::async, [=]() - { auto result = logic.SendMessage(toID, message, false); + return std::async(std::launch::async, [=, message = std::move(message)]() + { auto result = logic.SendMessage(toID, std::move(message), false); if (!result) logger->warn("SendTextMessage: failed at {}ms", Time::TimeSinceStart(startPoint)); return result; }); @@ -375,8 +375,8 @@ std::future TrickerDebugAPI::SendTextMessage(int64_t toID, std::string mes std::future StudentDebugAPI::SendBinaryMessage(int64_t toID, std::string message) { logger->info("SendBinaryMessage: toID = {}, message = {}, called at {}ms", toID, message, Time::TimeSinceStart(startPoint)); - return std::async(std::launch::async, [=]() - { auto result = logic.SendMessage(toID, message, true); + return std::async(std::launch::async, [=, message = std::move(message)]() + { auto result = logic.SendMessage(toID, std::move(message), true); if (!result) logger->warn("SendBinaryMessage: failed at {}ms", Time::TimeSinceStart(startPoint)); return result; }); @@ -385,8 +385,8 @@ std::future StudentDebugAPI::SendBinaryMessage(int64_t toID, std::string m std::future TrickerDebugAPI::SendBinaryMessage(int64_t toID, std::string message) { logger->info("SendBinaryMessage: toID = {}, message = {}, called at {}ms", toID, message, Time::TimeSinceStart(startPoint)); - return std::async(std::launch::async, [=]() - { auto result = logic.SendMessage(toID, message, true); + return std::async(std::launch::async, [=, message = std::move(message)]() + { auto result = logic.SendMessage(toID, std::move(message), true); if (!result) logger->warn("SendBinaryMessage: failed at {}ms", Time::TimeSinceStart(startPoint)); return result; }); diff --git a/CAPI/cpp/API/src/logic.cpp b/CAPI/cpp/API/src/logic.cpp index 62167c4..2579740 100644 --- a/CAPI/cpp/API/src/logic.cpp +++ b/CAPI/cpp/API/src/logic.cpp @@ -217,7 +217,7 @@ bool Logic::UseSkill(int32_t skill) bool Logic::SendMessage(int64_t toID, std::string message, bool binary) { logger->debug("Called SendMessage"); - return pComm->SendMessage(toID, message, binary, playerID); + return pComm->SendMessage(toID, std::move(message), binary, playerID); } bool Logic::HaveMessage() From ad4db599f97449786e6c910940bf4f69224d5408 Mon Sep 17 00:00:00 2001 From: Timothy Liu Date: Sat, 20 May 2023 13:59:45 +0800 Subject: [PATCH 2/4] build(capi): :zap: enable multicore compilation for cpp --- CAPI/cpp/API/API.vcxproj | 4 ++++ CAPI/cpp/README.md | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CAPI/cpp/API/API.vcxproj b/CAPI/cpp/API/API.vcxproj index 8416fe8..6b9f8a9 100644 --- a/CAPI/cpp/API/API.vcxproj +++ b/CAPI/cpp/API/API.vcxproj @@ -93,6 +93,7 @@ ..\spdlog\include;..\tclap\include;..\grpc\include;..\proto;include;%(AdditionalIncludeDirectories) /source-charset:utf-8 %(AdditionalOptions) MultiThreadedDebug + true Console @@ -114,6 +115,7 @@ ..\spdlog\include;..\tclap\include;..\grpc\include;..\proto;include;%(AdditionalIncludeDirectories) /source-charset:utf-8 %(AdditionalOptions) MultiThreaded + true Console @@ -135,6 +137,7 @@ ..\spdlog\include;..\tclap\include;..\grpc\include;..\proto;include;%(AdditionalIncludeDirectories) /source-charset:utf-8 %(AdditionalOptions) MultiThreadedDebug + true Console @@ -156,6 +159,7 @@ ..\spdlog\include;..\tclap\include;..\grpc\include;..\proto;include;%(AdditionalIncludeDirectories) /source-charset:utf-8 %(AdditionalOptions) MultiThreaded + true Console diff --git a/CAPI/cpp/README.md b/CAPI/cpp/README.md index 21daeeb..0087ee8 100644 --- a/CAPI/cpp/README.md +++ b/CAPI/cpp/README.md @@ -74,7 +74,7 @@ C++ 通信组件与选手接口 - 将之前提取的 Debug 和 Release 的 `.lib` 分别放在项目中的单独的文件夹里(THUAI6 使用的是 `CAPI\cpp\lib\debug` 和 `CAPI\cpp\lib\release`),并[使用 `.gitignore` 忽略掉](https://github.com/eesast/THUAI6/blob/c8e1fbe299c67a6e101fa02e85bcc971acd0f48b/CAPI/cpp/.gitignore#L502) - 在项目属性的“链接器”的首页的“附加库目录”中分别配置 Debug 和 Release 的 [`.lib` 文件的相应路径](https://github.com/eesast/THUAI6/blob/c8e1fbe299c67a6e101fa02e85bcc971acd0f48b/CAPI/cpp/API/API.vcxproj#L166) - 在项目属性中的“链接器”的“输入”的“附加依赖库”中分别配置 Debug 和 Release [所需要链接的库的文件名](https://github.com/eesast/THUAI6/blob/c8e1fbe299c67a6e101fa02e85bcc971acd0f48b/CAPI/cpp/API/API.vcxproj#L165)。注意 Debug 和 Release 链接的库可能并不完全相同,建议在 cmd 中使用 `dir /b` 将其自动列举并复制。还需要注意需要手动指定链接一些 Windows 自带的 `lib`,例如 `Ws2_32.lib`、`Crypt32.lib`、`Iphlpapi.lib` 等。如果生成过程中不通过,表示找不到一些函数,则在 Google 中搜索该函数,如果发现是 Windows 系统的 API 函数则会搜到[微软官方文档](https://learn.microsoft.com) 的对应链接的页面,则在页面最下方会表明它所在的 `.lib`(例如 [`CreateProcessA` 的页面](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa#requirements)),加进去即可 - - 然后进行生成解决方案。如果感觉编译的速度过慢,可以在项目属性的 `C/C++` 的“所有选项”中搜索并行编译,并开启之(`/Qpar`)。不过由于 THUAI6 的疏忽,忘记开启了并行编译 + - 然后进行生成解决方案。如果感觉编译的速度过慢,可以在项目属性的 `C/C++` 的“所有选项”中搜索多处理器编译,并开启之(`/MP`) - 然后开始运行。如果提示缺少一些 DLL,可以把之前保存的 `.dll` 文件(如果有的话)放在与 `.exe` 相同的目录下。该目录为**与 `.sln` 相同目录的**(不是与 `.vcxproj` 相同目录的)`x64\Debug` 和 `x64\Release` - 如果 x64 的 Debug 和 x64 的 Release 均生成成功,那么找一台没配过的电脑再试一次 - 随便写点 AI 代码,重新生成解决方案,确认成功后发布选手包 From c97af777f6828a25d69457279a0c64ed37ec3538 Mon Sep 17 00:00:00 2001 From: Timothy Liu Date: Sat, 20 May 2023 14:07:18 +0800 Subject: [PATCH 3/4] docs(capi): :memo: fix docs for multiprofessor compilation --- CAPI/cpp/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CAPI/cpp/README.md b/CAPI/cpp/README.md index 0087ee8..a5069b0 100644 --- a/CAPI/cpp/README.md +++ b/CAPI/cpp/README.md @@ -74,7 +74,7 @@ C++ 通信组件与选手接口 - 将之前提取的 Debug 和 Release 的 `.lib` 分别放在项目中的单独的文件夹里(THUAI6 使用的是 `CAPI\cpp\lib\debug` 和 `CAPI\cpp\lib\release`),并[使用 `.gitignore` 忽略掉](https://github.com/eesast/THUAI6/blob/c8e1fbe299c67a6e101fa02e85bcc971acd0f48b/CAPI/cpp/.gitignore#L502) - 在项目属性的“链接器”的首页的“附加库目录”中分别配置 Debug 和 Release 的 [`.lib` 文件的相应路径](https://github.com/eesast/THUAI6/blob/c8e1fbe299c67a6e101fa02e85bcc971acd0f48b/CAPI/cpp/API/API.vcxproj#L166) - 在项目属性中的“链接器”的“输入”的“附加依赖库”中分别配置 Debug 和 Release [所需要链接的库的文件名](https://github.com/eesast/THUAI6/blob/c8e1fbe299c67a6e101fa02e85bcc971acd0f48b/CAPI/cpp/API/API.vcxproj#L165)。注意 Debug 和 Release 链接的库可能并不完全相同,建议在 cmd 中使用 `dir /b` 将其自动列举并复制。还需要注意需要手动指定链接一些 Windows 自带的 `lib`,例如 `Ws2_32.lib`、`Crypt32.lib`、`Iphlpapi.lib` 等。如果生成过程中不通过,表示找不到一些函数,则在 Google 中搜索该函数,如果发现是 Windows 系统的 API 函数则会搜到[微软官方文档](https://learn.microsoft.com) 的对应链接的页面,则在页面最下方会表明它所在的 `.lib`(例如 [`CreateProcessA` 的页面](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa#requirements)),加进去即可 - - 然后进行生成解决方案。如果感觉编译的速度过慢,可以在项目属性的 `C/C++` 的“所有选项”中搜索多处理器编译,并开启之(`/MP`) + - 然后进行生成解决方案。如果感觉编译的速度过慢,可以在项目属性的 `C/C++` 的“所有选项”中搜索多处理器编译,并[开启(`/MP`)](https://github.com/eesast/THUAI6/blob/ad4db599f97449786e6c910940bf4f69224d5408/CAPI/cpp/API/API.vcxproj#L162) - 然后开始运行。如果提示缺少一些 DLL,可以把之前保存的 `.dll` 文件(如果有的话)放在与 `.exe` 相同的目录下。该目录为**与 `.sln` 相同目录的**(不是与 `.vcxproj` 相同目录的)`x64\Debug` 和 `x64\Release` - 如果 x64 的 Debug 和 x64 的 Release 均生成成功,那么找一台没配过的电脑再试一次 - 随便写点 AI 代码,重新生成解决方案,确认成功后发布选手包 From ef03a1a86540ec4ce45851a7d91c9f4d8f5ef987 Mon Sep 17 00:00:00 2001 From: Timothy Liu Date: Sat, 20 May 2023 14:16:20 +0800 Subject: [PATCH 4/4] perf(capi): :zap: use move semantics to optimize std::string --- CAPI/cpp/API/include/utils.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CAPI/cpp/API/include/utils.hpp b/CAPI/cpp/API/include/utils.hpp index eefa957..5887965 100644 --- a/CAPI/cpp/API/include/utils.hpp +++ b/CAPI/cpp/API/include/utils.hpp @@ -472,9 +472,9 @@ namespace THUAI62Proto { protobuf::SendMsg sendMsg; if (binary) - sendMsg.set_binary_message(msg); + sendMsg.set_binary_message(std::move(msg)); else - sendMsg.set_text_message(msg); + sendMsg.set_text_message(std::move(msg)); sendMsg.set_to_player_id(toID); sendMsg.set_player_id(id); return sendMsg;