From ee31dcdd50b912e63633d493921851d7928c7c03 Mon Sep 17 00:00:00 2001 From: Lucian Grijincu Date: Tue, 2 Apr 2013 21:29:02 -0700 Subject: [PATCH] folly: speed up fastpath of ThreadLocal::get() 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 | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/folly/ThreadLocal.h b/folly/ThreadLocal.h index c1181935..0663417f 100644 --- a/folly/ThreadLocal.h +++ b/folly/ThreadLocal.h @@ -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 tlp_; }; -- 2.34.1