From 6746259314362d89f3d1d1dbfdf00c9fe18de202 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Tue, 16 Jan 2018 09:32:21 -0800 Subject: [PATCH] Use thread-local in RequestContext::getStaticContext Summary: [Folly] Use thread-local in `RequestContext::getStaticContext`. `folly::SingletonThreadLocal` uses `folly::ThreadLocal`. However, `static FOLLY_TLS` (`static __thread`) is always faster than `folly::ThreadLocal` for thread-local singletons for which iteration is not required. Reviewed By: djwatson Differential Revision: D6725091 fbshipit-source-id: 9979f39677284b1051cb109b461097495d77ca17 --- folly/io/async/Request.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/folly/io/async/Request.cpp b/folly/io/async/Request.cpp index f8cca8b9..644c208f 100644 --- a/folly/io/async/Request.cpp +++ b/folly/io/async/Request.cpp @@ -141,10 +141,16 @@ std::shared_ptr RequestContext::setContext( } std::shared_ptr& RequestContext::getStaticContext() { - using SingletonT = SingletonThreadLocal>; - static SingletonT singleton; - + using T = std::shared_ptr; +#ifdef FOLLY_TLS + alignas(alignof(T)) static FOLLY_TLS unsigned char storage[sizeof(T)]; + static FOLLY_TLS T* singleton; + return singleton ? *singleton : *(singleton = new (storage) T()); +#else + struct PrivateTag {}; + static SingletonThreadLocal singleton; return singleton.get(); +#endif } RequestContext* RequestContext::get() { -- 2.34.1