Commit 6374f552 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Improve SingletonThreadLocal performance

Summary:
[Folly] Improve `SingletonThreadLocal` performance.

By explicitly controlling inlining behavior.

The ctor is definitionally cold - once per process - so outline.

`get` is hot so inline. Uncached `get` is cold - once per thread - so outline uncached.

Reviewed By: djwatson

Differential Revision: D6736662

fbshipit-source-id: 4cd77c7772b46e2e3c6b2a3dc071b2b06522979e
parent e96129da
...@@ -43,29 +43,29 @@ class SingletonThreadLocal { ...@@ -43,29 +43,29 @@ class SingletonThreadLocal {
SingletonThreadLocal() : SingletonThreadLocal([]() { return new T(); }) {} SingletonThreadLocal() : SingletonThreadLocal([]() { return new T(); }) {}
explicit SingletonThreadLocal(CreateFunc createFunc) template <typename Create>
: singleton_([createFunc = std::move(createFunc)]() mutable { FOLLY_NOINLINE explicit SingletonThreadLocal(Create create)
return new ThreadLocalT([createFunc = : singleton_([create = std::move(create)]() mutable {
std::move(createFunc)]() mutable { return new ThreadLocalT([create = std::move(create)]() mutable {
return new Wrapper(std::unique_ptr<T>(createFunc())); return new Wrapper(std::unique_ptr<T>(create()));
}); });
}) {} }) {}
static T& get() { FOLLY_ALWAYS_INLINE static T& get() {
#ifdef FOLLY_TLS #ifdef FOLLY_TLS
if (UNLIKELY(*localPtr() == nullptr)) { return *localPtr() ? **localPtr() : *(*localPtr() = &getSlow());
*localPtr() = &(**SingletonT::get());
}
return **localPtr();
#else #else
return **SingletonT::get(); return **SingletonT::get();
#endif #endif
} }
private: private:
FOLLY_NOINLINE static T& getSlow() {
return **SingletonT::get();
}
#ifdef FOLLY_TLS #ifdef FOLLY_TLS
static T** localPtr() { FOLLY_ALWAYS_INLINE static T** localPtr() {
static FOLLY_TLS T* localPtr = nullptr; static FOLLY_TLS T* localPtr = nullptr;
return &localPtr; return &localPtr;
} }
......
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