Skip to content

Commit

Permalink
lift more of Core to CoreBase
Browse files Browse the repository at this point in the history
Summary: Reduces the sizes of function template instantiations.

Reviewed By: Gownta

Differential Revision: D55427323

fbshipit-source-id: 2e21c81d6ad354e22000f37fc4ceaeed5a147d45
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Mar 31, 2024
1 parent e887076 commit ff659d3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
25 changes: 25 additions & 0 deletions folly/futures/detail/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,31 @@ void CoreBase::derefCallback() noexcept {
}
}

bool CoreBase::destroyDerived() noexcept {
DCHECK(attached_ == 0);
auto state = state_.load(std::memory_order_relaxed);
switch (state) {
case State::OnlyResult:
[[fallthrough]];

case State::Done:
return true;

case State::Proxy:
proxy_->detachFuture();
[[fallthrough]];

case State::Empty:
return false;

case State::Start:
case State::OnlyCallback:
case State::OnlyCallbackAllowInline:
default:
terminate_with<std::logic_error>("~Core unexpected state");
}
}

#if FOLLY_USE_EXTERN_FUTURE_UNIT
template class Core<folly::Unit>;
#endif
Expand Down
56 changes: 22 additions & 34 deletions folly/futures/detail/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,24 @@ class CoreBase {

void derefCallback() noexcept;

template <typename Self>
FOLLY_ERASE static Self& walkProxyChainImpl(Self& self) noexcept {
DCHECK(self.hasResult());
auto core = &self;
while (core->state_.load(std::memory_order_relaxed) == State::Proxy) {
core = core->proxy_;
}
return *core;
}
FOLLY_ERASE CoreBase& walkProxyChain() noexcept {
return walkProxyChainImpl(*this);
}
FOLLY_ERASE CoreBase const& walkProxyChain() const noexcept {
return walkProxyChainImpl(*this);
}

bool destroyDerived() noexcept;

Callback callback_;
std::atomic<State> state_;
std::atomic<unsigned char> attached_;
Expand Down Expand Up @@ -590,20 +608,10 @@ class Core final : private ResultHolder<T>, public CoreBase {
/// possibly moved-out, depending on what the callback did; some but not
/// all callbacks modify (possibly move-out) the result.)
Try<T>& getTry() {
DCHECK(hasResult());
auto core = this;
while (core->state_.load(std::memory_order_relaxed) == State::Proxy) {
core = static_cast<Core*>(core->proxy_);
}
return core->result_;
return static_cast<decltype(*this)&>(walkProxyChain()).result_;
}
Try<T> const& getTry() const {
DCHECK(hasResult());
auto core = this;
while (core->state_.load(std::memory_order_relaxed) == State::Proxy) {
core = static_cast<Core const*>(core->proxy_);
}
return core->result_;
return static_cast<decltype(*this)&>(walkProxyChain()).result_;
}

/// Call only from consumer thread.
Expand Down Expand Up @@ -682,28 +690,8 @@ class Core final : private ResultHolder<T>, public CoreBase {
}

~Core() override {
DCHECK(attached_ == 0);
auto state = state_.load(std::memory_order_relaxed);
switch (state) {
case State::OnlyResult:
[[fallthrough]];

case State::Done:
this->result_.~Result();
break;

case State::Proxy:
proxy_->detachFuture();
break;

case State::Empty:
break;

case State::Start:
case State::OnlyCallback:
case State::OnlyCallbackAllowInline:
default:
terminate_with<std::logic_error>("~Core unexpected state");
if (destroyDerived()) {
this->result_.~Result();
}
}

Expand Down

0 comments on commit ff659d3

Please sign in to comment.