Skip to content

Commit

Permalink
perf: linux 子进程不使用 wstring
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Apr 6, 2024
1 parent 4521d86 commit 2af5a8c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
45 changes: 30 additions & 15 deletions source/MaaUtils/IOStream/ChildPipeIOStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,45 @@

MAA_NS_BEGIN

#ifdef _WIN32
std::vector<std::wstring> conv_args(const std::vector<std::string>& args)
{
std::vector<std::wstring> wargs;
for (const auto& arg : args) {
wargs.emplace_back(to_u16(arg));
}
return wargs;
}
#else
std::vector<std::string> conv_args(const std::vector<std::string>& args)
{
return args;
}
#endif

ChildPipeIOStream::ChildPipeIOStream(
const std::filesystem::path& exec,
const std::vector<std::string>& args)
: ChildPipeIOStream(exec, to_wargs(args))
: ChildPipeIOStream(exec, conv_args(args), false)
{
}

ChildPipeIOStream::ChildPipeIOStream(
const std::filesystem::path& exec,
const std::vector<std::wstring>& wargs)
: ChildPipeIOStream(exec, wargs, false)
{
}

ChildPipeIOStream::ChildPipeIOStream(
const std::filesystem::path& exec,
const std::vector<os_string>& args,
bool)
: exec_(exec)
, wargs_(wargs)
, args_(args)
, child_(
exec_,
wargs_,
args_,
boost::process::std_out > pin_,
boost::process::std_err > boost::process::null,
boost::process::std_in < pout_
Expand All @@ -29,7 +53,7 @@ ChildPipeIOStream::ChildPipeIOStream(
#endif
)
{
LogTrace << VAR(exec_) << VAR(wargs_) << VAR(child_.id());
LogTrace << VAR(exec_) << VAR(args_) << VAR(child_.id());
}

ChildPipeIOStream::~ChildPipeIOStream()
Expand All @@ -40,7 +64,7 @@ ChildPipeIOStream::~ChildPipeIOStream()
bool ChildPipeIOStream::write(std::string_view data)
{
if (!pout_.good()) {
LogError << "pout is not good" << VAR(exec_) << VAR(wargs_) << VAR(child_.id());
LogError << "pout is not good" << VAR(exec_) << VAR(args_) << VAR(child_.id());
return false;
}

Expand All @@ -66,7 +90,7 @@ bool ChildPipeIOStream::release()
int code = child_.exit_code();

if (code != 0) {
LogWarn << "child exit with" << code << VAR(exec_) << VAR(wargs_) << VAR(child_.id());
LogWarn << "child exit with" << code << VAR(exec_) << VAR(args_) << VAR(child_.id());
return false;
}

Expand All @@ -91,13 +115,4 @@ std::string ChildPipeIOStream::read_once(size_t max_count)
return std::string(buffer_.get(), read);
}

std::vector<std::wstring> ChildPipeIOStream::to_wargs(const std::vector<std::string>& args)
{
std::vector<std::wstring> wargs;
for (const auto& arg : args) {
wargs.emplace_back(to_u16(arg));
}
return wargs;
}

MAA_NS_END
10 changes: 8 additions & 2 deletions source/include/Utils/IOStream/ChildPipeIOStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class MAA_UTILS_API ChildPipeIOStream : public IOStream
{
public:
ChildPipeIOStream(const std::filesystem::path& exec, const std::vector<std::string>& args);

#ifdef _WIN32
ChildPipeIOStream(const std::filesystem::path& exec, const std::vector<std::wstring>& wargs);
#endif

// NonCopyButMovable
// https://stackoverflow.com/questions/29289956/c11-virtual-destructors-and-auto-generation-of-move-special-functions
Expand All @@ -32,10 +35,13 @@ class MAA_UTILS_API ChildPipeIOStream : public IOStream
virtual std::string read_once(size_t max_count) override;

private:
static std::vector<std::wstring> to_wargs(const std::vector<std::string>& args);
using os_string = std::filesystem::path::string_type;

ChildPipeIOStream(const std::filesystem::path& exec, const std::vector<os_string>& args, bool);

private:
std::filesystem::path exec_;
std::vector<std::wstring> wargs_;
std::vector<os_string> args_;

boost::process::ipstream pin_;
boost::process::opstream pout_;
Expand Down

0 comments on commit 2af5a8c

Please sign in to comment.