Improve SingletonThreadLocal performance
[folly.git] / folly / SingletonThreadLocal.h
index 692495645d8baf74ec288fc763c47c0e4c8696ab..3add4fefc3facce0a5c4a0fa6e66fe90e88f3a42 100644 (file)
@@ -43,29 +43,29 @@ class SingletonThreadLocal {
 
   SingletonThreadLocal() : SingletonThreadLocal([]() { return new T(); }) {}
 
 
   SingletonThreadLocal() : SingletonThreadLocal([]() { return new T(); }) {}
 
-  explicit SingletonThreadLocal(CreateFunc createFunc)
-      : singleton_([createFunc = std::move(createFunc)]() mutable {
-          return new ThreadLocalT([createFunc =
-                                       std::move(createFunc)]() mutable {
-            return new Wrapper(std::unique_ptr<T>(createFunc()));
+  template <typename Create>
+  FOLLY_NOINLINE explicit SingletonThreadLocal(Create create)
+      : singleton_([create = std::move(create)]() mutable {
+          return new ThreadLocalT([create = std::move(create)]() mutable {
+            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)) {
-      *localPtr() = &(**SingletonT::get());
-    }
-
-    return **localPtr();
+    return *localPtr() ? **localPtr() : *(*localPtr() = &getSlow());
 #else
     return **SingletonT::get();
 #endif
   }
 
  private:
 #else
     return **SingletonT::get();
 #endif
   }
 
  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;
     return &localPtr;
   }
     static FOLLY_TLS T* localPtr = nullptr;
     return &localPtr;
   }