From: Lucian Grijincu Date: Wed, 3 Apr 2013 04:29:02 +0000 (-0700) Subject: folly: speed up fastpath of ThreadLocal::get() X-Git-Tag: v0.22.0~1015 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=ee31dcdd50b912e63633d493921851d7928c7c03;p=folly.git 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 --- 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_; };