diff options
author | Andrew Morrow <acm@10gen.com> | 2013-04-23 21:17:54 -0400 |
---|---|---|
committer | Andrew Morrow <acm@10gen.com> | 2013-05-22 19:52:25 -0400 |
commit | 214697c86ed2b1cad2a485f7ff810199bd69d674 (patch) | |
tree | be1d88b5a61044cf5ad4e017ecbd6b0eaf0ecb74 /src/mongo/base/status.h | |
parent | 86d7443917a1497c9c3294e4b995872e7b85cd7e (diff) | |
download | mongo-214697c86ed2b1cad2a485f7ff810199bd69d674.tar.gz |
SERVER-9757 Improve performance of Status as a return value
- Make OK be represented by a NULL ErrorInfo pointer, making it much faster to construct and
return an OK Status.
- Inline most of the trivial methods of Status, but particuarly the refcounting, so that
returning a Status doesn't require any function calls.
- Remove the no longer necessary machinery around statics.
- Move lifecycle inlines to a -inl.h header for clarity.
Diffstat (limited to 'src/mongo/base/status.h')
-rw-r--r-- | src/mongo/base/status.h | 58 |
1 files changed, 40 insertions, 18 deletions
diff --git a/src/mongo/base/status.h b/src/mongo/base/status.h index cdd3980bae2..4454913e725 100644 --- a/src/mongo/base/status.h +++ b/src/mongo/base/status.h @@ -49,7 +49,9 @@ namespace mongo { class Status { public: // Short-hand for returning an OK status. - static Status OK() { return Status(getOKInfo()); } + static Status OK() { + return Status(); + } /** * Builds an error status given the error code, a textual description of what @@ -82,11 +84,25 @@ namespace mongo { // accessors // - bool isOK() const { return code() == ErrorCodes::OK; } - ErrorCodes::Error code() const { return _error->code; } - const char* codeString() const { return ErrorCodes::errorString(_error->code); } - const std::string& reason() const { return _error->reason; } - int location() const { return _error->location; } + bool isOK() const { + return code() == ErrorCodes::OK; + } + + ErrorCodes::Error code() const { + return _error ? _error->code : ErrorCodes::OK; + } + + const char* codeString() const { + return ErrorCodes::errorString(code()); + } + + std::string reason() const { + return _error ? _error->reason : std::string(); + } + + int location() const { + return _error ? _error->location : 0; + } std::string toString() const; @@ -94,21 +110,24 @@ namespace mongo { // Below interface used for testing code only. // - int refCount() const { return _error->refs.load(); } + AtomicUInt32::WordType refCount() const { + return _error ? _error->refs.load() : 0; + } private: - struct ErrorInfo { - AtomicUInt32 refs; // reference counter - ErrorCodes::Error code; // error code - std::string reason; // description of error cause - int location; // unique location of the triggering line in the code + Status(); - ErrorInfo(ErrorCodes::Error aCode, const std::string& aReason, int aLocation); - }; + struct ErrorInfo { + AtomicUInt32 refs; // reference counter + const ErrorCodes::Error code; // error code + const std::string reason; // description of error cause + const int location; // unique location of the triggering line in the code - static ErrorInfo *getOKInfo(); + static ErrorInfo* create(ErrorCodes::Error code, + const StringData& reason, int location); - explicit Status(ErrorInfo *info); + ErrorInfo(ErrorCodes::Error code, const StringData& reason, int location); + }; ErrorInfo* _error; @@ -121,11 +140,11 @@ namespace mongo { static void unref(ErrorInfo* error); }; - static inline bool operator==(const ErrorCodes::Error lhs, const Status& rhs) { + inline bool operator==(const ErrorCodes::Error lhs, const Status& rhs) { return rhs == lhs; } - static inline bool operator!=(const ErrorCodes::Error lhs, const Status& rhs) { + inline bool operator!=(const ErrorCodes::Error lhs, const Status& rhs) { return rhs != lhs; } @@ -137,3 +156,6 @@ namespace mongo { std::ostream& operator<<(std::ostream& os, ErrorCodes::Error); } // namespace mongo + +#include "mongo/base/status-inl.h" + |