Skip to content

Commit

Permalink
GH-44491: [C++] Static Status draft
Browse files Browse the repository at this point in the history
  • Loading branch information
bkietz committed Oct 21, 2024
1 parent 375b21f commit 0634658
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
10 changes: 9 additions & 1 deletion cpp/src/arrow/status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ Status::Status(StatusCode code, std::string msg, std::shared_ptr<StatusDetail> d
}

void Status::CopyFrom(const Status& s) {
delete state_;
DeleteState();
if (s.state_ == nullptr) {
state_ = nullptr;
} else if (s.state_->is_static) {
state_ = s.state_;
} else {
state_ = new State(*s.state_);
}
Expand Down Expand Up @@ -155,6 +157,12 @@ void Status::Warn(const std::string& message) const {
ARROW_LOG(WARNING) << message << ": " << ToString();
}

Status Status::MakeStatic(StatusCode code, std::string msg, std::shared_ptr<StatusDetail> detail) {
Status st{code, std::move(msg), std::move(detail)};
st.state_->is_static = true;
return st;
}

#ifdef ARROW_EXTRA_ERROR_CONTEXT
void Status::AddContextLine(const char* filename, int line, const char* expr) {
ARROW_CHECK(!ok()) << "Cannot add context line to ok status";
Expand Down
18 changes: 10 additions & 8 deletions cpp/src/arrow/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,7 @@ class ARROW_EXPORT [[nodiscard]] Status : public util::EqualityComparable<Status
public:
// Create a success status.
constexpr Status() noexcept : state_(NULLPTR) {}
~Status() noexcept {
// ARROW-2400: On certain compilers, splitting off the slow path improves
// performance significantly.
if (ARROW_PREDICT_FALSE(state_ != NULL)) {
DeleteState();
}
}
~Status() noexcept { DeleteState(); }

Status(StatusCode code, const std::string& msg);
/// \brief Pluggable constructor for use by sub-systems. detail cannot be null.
Expand Down Expand Up @@ -363,17 +357,25 @@ class ARROW_EXPORT [[nodiscard]] Status : public util::EqualityComparable<Status
void AddContextLine(const char* filename, int line, const char* expr);
#endif

// Construct a Status which is not destroyed and is cheap to copy.
static Status MakeStatic(StatusCode code, std::string msg,
std::shared_ptr<StatusDetail> detail);
private:
struct State {
StatusCode code;
std::string msg;
std::shared_ptr<StatusDetail> detail;
bool is_static = false;
};
// OK status has a `NULL` state_. Otherwise, `state_` points to
// a `State` structure containing the error code and message(s)
State* state_;

void DeleteState() {
// ARROW-2400: On certain compilers, splitting off the slow path improves
// performance significantly.
if (ARROW_PREDICT_TRUE(state_ == NULL)) return;
if (ARROW_PREDICT_FALSE(state_->is_static)) return;
delete state_;
state_ = NULLPTR;
}
Expand All @@ -382,7 +384,7 @@ class ARROW_EXPORT [[nodiscard]] Status : public util::EqualityComparable<Status
};

void Status::MoveFrom(Status& s) {
delete state_;
DeleteState();
state_ = s.state_;
s.state_ = NULLPTR;
}
Expand Down

0 comments on commit 0634658

Please sign in to comment.