From 8b0158c17c8cdf5bbda54f6154363976f716302b Mon Sep 17 00:00:00 2001 From: PragmaTwice Date: Tue, 21 Mar 2023 16:25:04 +0800 Subject: [PATCH 1/4] Refactor Status to avoid large stack size in happy path --- src/common/status.h | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/common/status.h b/src/common/status.h index 7899f999ff9..c384cff22f6 100644 --- a/src/common/status.h +++ b/src/common/status.h @@ -66,28 +66,39 @@ class [[nodiscard]] Status { BlockingCmd, }; - Status() : Status(cOK) {} + Status() : impl_{nullptr} {} - Status(Code code, std::string msg = {}) : code_(code), msg_(std::move(msg)) {} // NOLINT + Status(Code code, std::string msg = {}) : impl_{new Impl{code, std::move(msg)}} { // NOLINT + CHECK(code != cOK); + } + + Status(const Status& s) : impl_{s.impl_ ? new Impl{s.impl_->code_, s.impl_->msg_} : nullptr} {} + Status(Status&&) = default; + + Status& operator=(const Status& s) { + Status tmp = s; + return *this = std::move(tmp); + } + Status& operator=(Status&& s) = default; template bool Is() const { - return code_ == code; + return impl_ && impl_->code_ == code; } - bool IsOK() const { return Is(); } + bool IsOK() const { return !impl_; } explicit operator bool() const { return IsOK(); } - Code GetCode() const { return code_; } + Code GetCode() const { return impl_ ? impl_->code_ : cOK; } std::string Msg() const& { if (*this) return ok_msg; - return msg_; + return impl_->msg_; } std::string Msg() && { if (*this) return ok_msg; - return std::move(msg_); + return std::move(impl_->msg_); } static Status OK() { return {}; } @@ -99,7 +110,7 @@ class [[nodiscard]] Status { if (*this) { return {}; } - return {code_, fmt::format("{}: {}", prefix, msg_)}; + return {impl_->code_, fmt::format("{}: {}", prefix, impl_->msg_)}; } void GetValue() {} @@ -107,8 +118,12 @@ class [[nodiscard]] Status { static constexpr const char* ok_msg = "ok"; private: - Code code_; - std::string msg_; + struct Impl { + Code code_; + std::string msg_; + }; + + std::unique_ptr impl_; static constexpr Code cOK = static_cast(0); @@ -195,9 +210,9 @@ struct [[nodiscard]] StatusOr { using Code = Status::Code; - StatusOr(Status s) : code_(s.code_) { // NOLINT + StatusOr(Status s) : code_(s.GetCode()) { // NOLINT CHECK(!s); - new (&error_) error_type(std::move(s.msg_)); + new (&error_) error_type(std::move(s.impl_->msg_)); } StatusOr(Code code, std::string msg = {}) : code_(code) { // NOLINT From 24606ace1322b2fd743e9e7807ac8bd9356844a5 Mon Sep 17 00:00:00 2001 From: PragmaTwice Date: Tue, 21 Mar 2023 16:42:27 +0800 Subject: [PATCH 2/4] fix --- src/common/status.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/common/status.h b/src/common/status.h index c384cff22f6..8d480b3af0a 100644 --- a/src/common/status.h +++ b/src/common/status.h @@ -81,6 +81,8 @@ class [[nodiscard]] Status { } Status& operator=(Status&& s) = default; + ~Status() = default; + template bool Is() const { return impl_ && impl_->code_ == code; From b6bd0b3bb6287d9a5505d9f02efc60b4c8c71c7b Mon Sep 17 00:00:00 2001 From: PragmaTwice Date: Wed, 22 Mar 2023 21:27:09 +0800 Subject: [PATCH 3/4] fix --- src/common/status.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/common/status.h b/src/common/status.h index 8d480b3af0a..afd6323802d 100644 --- a/src/common/status.h +++ b/src/common/status.h @@ -30,6 +30,12 @@ #include #include +template +struct StatusImpl { + Code code_; + std::string msg_; +}; + class [[nodiscard]] Status { public: enum Code : unsigned char { @@ -120,10 +126,7 @@ class [[nodiscard]] Status { static constexpr const char* ok_msg = "ok"; private: - struct Impl { - Code code_; - std::string msg_; - }; + using Impl = StatusImpl; std::unique_ptr impl_; From fcc672a50b23d2cc137e68c0bdd5921307774e25 Mon Sep 17 00:00:00 2001 From: PragmaTwice Date: Tue, 4 Apr 2023 21:33:15 +0800 Subject: [PATCH 4/4] Revert "fix" This reverts commit b6bd0b3bb6287d9a5505d9f02efc60b4c8c71c7b. --- src/common/status.h | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/common/status.h b/src/common/status.h index afd6323802d..8d480b3af0a 100644 --- a/src/common/status.h +++ b/src/common/status.h @@ -30,12 +30,6 @@ #include #include -template -struct StatusImpl { - Code code_; - std::string msg_; -}; - class [[nodiscard]] Status { public: enum Code : unsigned char { @@ -126,7 +120,10 @@ class [[nodiscard]] Status { static constexpr const char* ok_msg = "ok"; private: - using Impl = StatusImpl; + struct Impl { + Code code_; + std::string msg_; + }; std::unique_ptr impl_;