lingbin commented on a change in pull request #2969: Improve implementation of 
Status
URL: https://github.com/apache/incubator-doris/pull/2969#discussion_r382666077
 
 

 ##########
 File path: be/src/common/status.h
 ##########
 @@ -17,114 +17,136 @@ namespace doris {
 
 class Status {
 public:
-    Status(): _state(nullptr) {}
+    Status() : _state(nullptr) { }
     ~Status() noexcept { delete[] _state; }
 
-    // copy c'tor makes copy of error detail so Status can be returned by value
-    Status(const Status& s)
-            : _state(s._state == nullptr ? nullptr : copy_state(s._state))  {
-    }
+    // Here _state == nullptr(Status::OK) is the most common case, so use this 
as
+    // a fast path. This check can avoid one function call in most cases, 
because
+    // _copy_state() is not an inline function.
+    Status(const Status& s) : _state(s._state == nullptr ? nullptr : 
_copy_state(s._state))  { }
 
     // same as copy c'tor
     Status& operator=(const Status& s) {
         // The following condition catches both aliasing (when this == &s),
         // and the common case where both s and *this are OK.
         if (_state != s._state) {
             delete[] _state;
-            _state = (s._state == nullptr) ? nullptr : copy_state(s._state);
+            _state = (s._state == nullptr) ? nullptr : _copy_state(s._state);
         }
         return *this;
     }
 
-    // move c'tor
     Status(Status&& s) noexcept : _state(s._state) {
         s._state = nullptr;
     }
 
-    // move assign
     Status& operator=(Status&& s) noexcept {
-        std::swap(_state, s._state);
+        if (_state != s._state) {
 
 Review comment:
   > In general, a move assignment operator should:
   > 1. Destroy visible resources (though maybe save implementation detail 
resources).
   > 2. Move assign all bases and members.
   > 3. If the move assignment of bases and members didn't make the rhs 
resource-less, then make it so.
   
   Taken from 
[https://stackoverflow.com/questions/6687388/why-do-some-people-use-swap-for-move-assignments](https://stackoverflow.com/questions/6687388/why-do-some-people-use-swap-for-move-assignments)
 
   
   Another example is, somebody may still use the the `Status` object after 
move. 
   ```
   Status src = Status::RuntimeError();
   Status dest = Status::IOError("xxx")
   dest = std::move(src);
   // here, if we use `std::move` in `Status::operator=`
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to