Commit c7d8ba07 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

A cache for InlineExecutor singleton

Summary: [Folly] A cache for `InlineExecutor` singleton, which can be inlined into callers with little extra code compared to calling the backing function directly.

Reviewed By: marshallcline

Differential Revision: D8339793

fbshipit-source-id: d2be0128e617cfefe66809eb44ce40e1a8ae2dfb
parent 3dd9d51b
...@@ -20,9 +20,12 @@ ...@@ -20,9 +20,12 @@
namespace folly { namespace folly {
InlineExecutor& InlineExecutor::instance() { InlineExecutor& InlineExecutor::instance_slow() noexcept {
static auto instance = Indestructible<InlineExecutor>{}; static auto instance = Indestructible<InlineExecutor>{};
cache.store(&*instance, std::memory_order_release);
return *instance; return *instance;
} }
std::atomic<InlineExecutor*> InlineExecutor::cache;
} // namespace folly } // namespace folly
...@@ -15,6 +15,11 @@ ...@@ -15,6 +15,11 @@
*/ */
#pragma once #pragma once
#include <atomic>
#include <folly/CPortability.h>
#include <folly/CppAttributes.h>
#include <folly/Executor.h> #include <folly/Executor.h>
namespace folly { namespace folly {
...@@ -24,11 +29,20 @@ namespace folly { ...@@ -24,11 +29,20 @@ namespace folly {
/// QueuedImmediateExecutor. /// QueuedImmediateExecutor.
class InlineExecutor : public Executor { class InlineExecutor : public Executor {
public: public:
static InlineExecutor& instance(); FOLLY_ATTR_VISIBILITY_HIDDEN FOLLY_ALWAYS_INLINE static InlineExecutor&
instance() noexcept {
auto const value = cache.load(std::memory_order_acquire);
return value ? *value : instance_slow();
}
void add(Func f) override { void add(Func f) override {
f(); f();
} }
private:
FOLLY_COLD static InlineExecutor& instance_slow() noexcept;
static std::atomic<InlineExecutor*> cache;
}; };
} // namespace folly } // namespace folly
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment