Commit 38f07639 authored by Marshall Cline's avatar Marshall Cline Committed by Facebook Github Bot

Make CoreAndCallbackReference move-ctor faster, release memory eagerly/sooner

Summary:
Avoid swap() in move-ctor:
  - slightly faster
  - frees resources eagerly/sooner vs. later

Side benefit: added `noexcept` to a few appropriate methods.

Reviewed By: yfeldblum

Differential Revision: D7723078

fbshipit-source-id: f2d08b5ce094c42cb114deda7a6841052d1b785a
parent 78443cdc
...@@ -301,11 +301,8 @@ class Core final { ...@@ -301,11 +301,8 @@ class Core final {
public: public:
explicit CoreAndCallbackReference(Core* core) noexcept : core_(core) {} explicit CoreAndCallbackReference(Core* core) noexcept : core_(core) {}
~CoreAndCallbackReference() { ~CoreAndCallbackReference() noexcept {
if (core_) { detach();
core_->derefCallback();
core_->detachOne();
}
} }
CoreAndCallbackReference(CoreAndCallbackReference const& o) = delete; CoreAndCallbackReference(CoreAndCallbackReference const& o) = delete;
...@@ -313,7 +310,8 @@ class Core final { ...@@ -313,7 +310,8 @@ class Core final {
delete; delete;
CoreAndCallbackReference(CoreAndCallbackReference&& o) noexcept { CoreAndCallbackReference(CoreAndCallbackReference&& o) noexcept {
std::swap(core_, o.core_); detach();
core_ = exchange(o.core_, nullptr);
} }
Core* getCore() const noexcept { Core* getCore() const noexcept {
...@@ -321,6 +319,13 @@ class Core final { ...@@ -321,6 +319,13 @@ class Core final {
} }
private: private:
void detach() noexcept {
if (core_) {
core_->derefCallback();
core_->detachOne();
}
}
Core* core_{nullptr}; Core* core_{nullptr};
}; };
...@@ -398,7 +403,7 @@ class Core final { ...@@ -398,7 +403,7 @@ class Core final {
} }
} }
void detachOne() { void detachOne() noexcept {
auto a = attached_--; auto a = attached_--;
assert(a >= 1); assert(a >= 1);
if (a == 1) { if (a == 1) {
...@@ -406,7 +411,7 @@ class Core final { ...@@ -406,7 +411,7 @@ class Core final {
} }
} }
void derefCallback() { void derefCallback() noexcept {
if (--callbackReferences_ == 0) { if (--callbackReferences_ == 0) {
callback_ = {}; callback_ = {};
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment