folly: speed up fastpath of ThreadLocal::get()
authorLucian Grijincu <lucian@fb.com>
Wed, 3 Apr 2013 04:29:02 +0000 (21:29 -0700)
committerJordan DeLong <jdelong@fb.com>
Sun, 21 Apr 2013 20:20:51 +0000 (13:20 -0700)
Summary:
A smaller function makes it more likely it will be inlined
(it wasn't before, is now).

Test Plan: n/a

Reviewed By: tudorb@fb.com

FB internal diff: D759996

folly/ThreadLocal.h

index c1181935465e72cd894359ca49bfbcb5bc8b2dfa..0663417f1f7fe0d7f46e54f299fa09284216cd0c 100644 (file)
@@ -79,11 +79,12 @@ class ThreadLocal {
 
   T* get() const {
     T* ptr = tlp_.get();
-    if (UNLIKELY(ptr == nullptr)) {
-      ptr = new T();
-      tlp_.reset(ptr);
+    if (LIKELY(ptr != nullptr)) {
+      return ptr;
     }
-    return ptr;
+
+    // separated new item creation out to speed up the fast path.
+    return makeTlp();
   }
 
   T* operator->() const {
@@ -112,6 +113,12 @@ class ThreadLocal {
   ThreadLocal(const ThreadLocal&) = delete;
   ThreadLocal& operator=(const ThreadLocal&) = delete;
 
+  T* makeTlp() const {
+    T* ptr = new T();
+    tlp_.reset(ptr);
+    return ptr;
+  }
+
   mutable ThreadLocalPtr<T,Tag> tlp_;
 };