Skip to content

Commit

Permalink
Status class optimizations (microsoft#824)
Browse files Browse the repository at this point in the history
optimize onnxruntime::common::Status to reduce code size
  • Loading branch information
tracysh authored Apr 12, 2019
1 parent b6936e7 commit c55e2de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
13 changes: 11 additions & 2 deletions include/onnxruntime/core/common/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Status {

Status(StatusCategory category, int code, const std::string& msg);

Status(StatusCategory category, int code, const char* msg);

Status(StatusCategory category, int code);

Status(const Status& other)
Expand All @@ -72,7 +74,9 @@ class Status {
Status& operator=(Status&& other) = default;
~Status() = default;

bool IsOK() const noexcept;
bool IsOK() const {
return (state_ == nullptr);
}

int Code() const noexcept;

Expand All @@ -90,7 +94,9 @@ class Status {
return !(*this == other);
}

static const Status& OK() noexcept;
static Status OK() {
return Status();
}

private:
static const std::string& EmptyString() noexcept;
Expand All @@ -99,6 +105,9 @@ class Status {
State(StatusCategory cat0, int code0, const std::string& msg0)
: category(cat0), code(code0), msg(msg0) {}

State(StatusCategory cat0, int code0, const char* msg0)
: category(cat0), code(code0), msg(msg0) {}

const StatusCategory category;
const int code;
const std::string msg;
Expand Down
17 changes: 7 additions & 10 deletions onnxruntime/core/common/status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ Status::Status(StatusCategory category, int code, const std::string& msg) {
state_ = std::make_unique<State>(category, code, msg);
}

Status::Status(StatusCategory category, int code)
: Status(category, code, EmptyString()) {
Status::Status(StatusCategory category, int code, const char* msg) {
// state_ will be allocated here causing the status to be treated as a failure
ORT_ENFORCE(code != static_cast<int>(MLStatus::OK));

state_ = std::make_unique<State>(category, code, msg);
}

bool Status::IsOK() const noexcept {
return (state_ == nullptr);
Status::Status(StatusCategory category, int code)
: Status(category, code, "") {
}

StatusCategory Status::Category() const noexcept {
Expand Down Expand Up @@ -58,8 +61,6 @@ std::string Status::ToString() const {
result += "[ONNXRuntimeError]";
result += " : ";
result += std::to_string(Code());
std::string msg;

result += " : ";
result += MLStatusToString(static_cast<MLStatus>(Code()));
result += " : ";
Expand All @@ -76,10 +77,6 @@ std::string Status::ToString() const {
#pragma warning(push)
#pragma warning(disable : 26426)
#endif
const Status& Status::OK() noexcept {
static Status s_ok;
return s_ok;
}

const std::string& Status::EmptyString() noexcept {
static std::string s_empty;
Expand Down

0 comments on commit c55e2de

Please sign in to comment.