lingbin commented on a change in pull request #2969: Fixed the move-assigment of `Status` URL: https://github.com/apache/incubator-doris/pull/2969#discussion_r382889942
########## 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: No, I do not think the standard required that. But for `dest = std::move(src)`, the following two points are determined: 1. the `dest` resource should **not** be assigned to `src` after move-assignment. 2. The move-assignment of move-constructor should have the **same behavior**. And for `Status`, it only has one member, `_state`, which is **pointer type**. when the `src::_state` was moved to `dest`, it must be cleared in `src`, otherwise, **the pointer will be referenced by both the `src` and `dest`, which will be wrong.** ---------------------------------------------------------------- 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