Skip to content

Commit

Permalink
optimize StatWrapperBase::tcStat()
Browse files Browse the repository at this point in the history
Summary:
* Use `ThreadLocalPtr<T>` v.s. `ThreadLocal<shared_ptr<T>>` to remove one level indirection on access.
* Allows splitting the fast-path v.s. slow-path more finely and inline-expanding only the fast-path.

Reviewed By: Gownta

Differential Revision: D66591950

fbshipit-source-id: 20ab12deb05c7e297df368eb8e6b0de92bcb813a
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Dec 3, 2024
1 parent ebc0a7b commit fe4d178
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions fb303/ThreadCachedServiceData.h
Original file line number Diff line number Diff line change
Expand Up @@ -816,24 +816,23 @@ class StatWrapperBase {

// Accessor does not guarantee underlying stat object has been initialized.
TLStatT* getTcStatUnsafe() const {
return tlStat_->get();
return tlStat_.get();
}

protected:
std::string key_;

folly::ThreadLocal<std::shared_ptr<TLStatT>> tlStat_;
folly::ThreadLocalPtr<TLStatT> tlStat_;

virtual std::shared_ptr<TLStatT> getStatSafe(const std::string& key) = 0;

TLStatT* tcStat() {
TLStatT* cached = tlStat_->get();
if (cached) {
return cached;
}

auto tlStat = getStatSafe(key_);
*tlStat_ = tlStat;
FOLLY_ALWAYS_INLINE TLStatT* tcStat() {
TLStatT* cached = tlStat_.get();
return FOLLY_LIKELY(!!cached) ? cached : tcStatSlow();
}
FOLLY_NOINLINE TLStatT* tcStatSlow() {
auto const tlStat = getStatSafe(key_);
tlStat_.reset(tlStat);
return tlStat.get();
}
};
Expand Down

0 comments on commit fe4d178

Please sign in to comment.