From fc353ba6b01facad6ec1a8eb55a2e11e1483540c Mon Sep 17 00:00:00 2001 From: Andrii Grynenko Date: Thu, 4 Feb 2016 13:58:25 -0800 Subject: [PATCH] Improve RequestContext::getStaticContext() perf Summary: Avoid using folly::ThreadLocal on the fast-path. This uses the fact that it is a static object. Reviewed By: yfeldblum Differential Revision: D2873374 fb-gh-sync-id: 978108da0c9e8576fda6849058b0262478c2841e --- folly/Makefile.am | 1 + folly/Singleton.cpp | 3 -- folly/Singleton.h | 70 ++++++++++++++++++++++++++++++- folly/SingletonThreadLocal.h | 81 ++++++++++++++++++++++++++++++++++++ folly/io/async/Request.h | 14 +++---- 5 files changed, 156 insertions(+), 13 deletions(-) create mode 100644 folly/SingletonThreadLocal.h diff --git a/folly/Makefile.am b/folly/Makefile.am index d989ac36..a88acae8 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -274,6 +274,7 @@ nobase_follyinclude_HEADERS = \ SharedMutex.h \ Singleton.h \ Singleton-inl.h \ + SingletonThreadLocal.h \ SmallLocks.h \ small_vector.h \ SocketAddress.h \ diff --git a/folly/Singleton.cpp b/folly/Singleton.cpp index c2a8afde..66321bfd 100644 --- a/folly/Singleton.cpp +++ b/folly/Singleton.cpp @@ -96,7 +96,6 @@ void SingletonVault::addEagerInitSingleton(detail::SingletonHolderBase* entry) { } void SingletonVault::registrationComplete() { - RequestContext::saveContext(); std::atexit([](){ SingletonVault::singleton()->destroyInstances(); }); RWSpinLock::WriteHolder wh(&stateMutex_); @@ -214,8 +213,6 @@ void SingletonVault::reenableInstances() { } void SingletonVault::scheduleDestroyInstances() { - RequestContext::saveContext(); - class SingletonVaultDestructor { public: ~SingletonVaultDestructor() { diff --git a/folly/Singleton.h b/folly/Singleton.h index 174f3ca4..732a6112 100644 --- a/folly/Singleton.h +++ b/folly/Singleton.h @@ -109,7 +109,6 @@ #include #include #include -#include #include #include @@ -609,6 +608,75 @@ class Singleton { } }; +template +class LeakySingleton { + public: + using CreateFunc = std::function; + + LeakySingleton() : LeakySingleton([] { return new T(); }) {} + + explicit LeakySingleton(CreateFunc createFunc) { + auto& entry = entryInstance(); + if (entry.state != State::NotRegistered) { + LOG(FATAL) << "Double registration of singletons of the same " + << "underlying type; check for multiple definitions " + << "of type folly::LeakySingleton<" + entry.type_.name() + ">"; + } + entry.createFunc = createFunc; + entry.state = State::Dead; + } + + static T& get() { return instance(); } + + private: + enum class State { NotRegistered, Dead, Living }; + + struct Entry { + Entry() {} + Entry(const Entry&) = delete; + Entry& operator=(const Entry&) = delete; + + std::atomic state{State::NotRegistered}; + T* ptr{nullptr}; + CreateFunc createFunc; + std::mutex mutex; + detail::TypeDescriptor type_{typeid(T), typeid(Tag)}; + }; + + static Entry& entryInstance() { + static auto entry = new Entry(); + return *entry; + } + + static T& instance() { + auto& entry = entryInstance(); + if (UNLIKELY(entry.state != State::Living)) { + createInstance(); + } + + return *entry.ptr; + } + + static void createInstance() { + auto& entry = entryInstance(); + + std::lock_guard lg(entry.mutex); + if (entry.state == State::Living) { + return; + } + + if (entry.state == State::NotRegistered) { + auto ptr = SingletonVault::stackTraceGetter().load(); + LOG(FATAL) << "Creating instance for unregistered singleton: " + << entry.type_.name() << "\n" + << "Stacktrace:" + << "\n" << (ptr ? (*ptr)() : "(not available)"); + } + + entry.ptr = entry.createFunc(); + entry.state = State::Living; + } +}; } #include diff --git a/folly/SingletonThreadLocal.h b/folly/SingletonThreadLocal.h new file mode 100644 index 00000000..e073fecd --- /dev/null +++ b/folly/SingletonThreadLocal.h @@ -0,0 +1,81 @@ +/* + * Copyright 2016 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +namespace folly { + +template +class SingletonThreadLocal { + public: + using CreateFunc = std::function; + + 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(createFunc())); + }); + }) {} + + static T& get() { +#ifdef FOLLY_TLS + *localPtr() = nullptr; + if (UNLIKELY(*localPtr() == nullptr)) { + *localPtr() = &(**SingletonT::get()); + } + + return **localPtr(); +#else + return **SingletonT::get(); +#endif + } + + private: +#ifdef FOLLY_TLS + static T** localPtr() { + static FOLLY_TLS T* localPtr = nullptr; + return &localPtr; + } +#endif + + class Wrapper { + public: + explicit Wrapper(std::unique_ptr t) : t_(std::move(t)) {} + + ~Wrapper() { +#ifdef FOLLY_TLS + *localPtr() = nullptr; +#endif + } + + T& operator*() { return *t_; } + + private: + std::unique_ptr t_; + }; + + using ThreadLocalT = ThreadLocal; + using SingletonT = LeakySingleton; + + SingletonT singleton_; +}; +} diff --git a/folly/io/async/Request.h b/folly/io/async/Request.h index 24245e5c..24361d48 100644 --- a/folly/io/async/Request.h +++ b/folly/io/async/Request.h @@ -25,6 +25,7 @@ #include #include #include +#include namespace folly { @@ -129,15 +130,10 @@ class RequestContext { } private: - // Used to solve static destruction ordering issue. Any static object - // that uses RequestContext must call this function in its constructor. - // - // See below link for more details. - // http://stackoverflow.com/questions/335369/ - // finding-c-static-initialization-order-problems#335746 - static std::shared_ptr &getStaticContext() { - static folly::ThreadLocal > context; - return *context; + static std::shared_ptr& getStaticContext() { + using SingletonT = SingletonThreadLocal>; + static SingletonT singleton; + return singleton.get(); } folly::RWSpinLock lock; -- 2.34.1