Commit 38c1a117 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

no shared_ptr in EventBaseLocal

Summary: [folly] no `shared_ptr` storage in `EventBaseLocal` since its refcount operations are unnecessary. Pare it down to `unique_ptr` with a straightforward fun-ptr deleter.

Reviewed By: iahs

Differential Revision: D26246619

fbshipit-source-id: 3d0214ccb6c4730ca7b573065b4468a0dce85d40
parent 619ff1d1
......@@ -35,6 +35,7 @@
#include <folly/Executor.h>
#include <folly/Function.h>
#include <folly/Memory.h>
#include <folly/Portability.h>
#include <folly/ScopeGuard.h>
#include <folly/Synchronized.h>
......@@ -948,7 +949,7 @@ class EventBase : public TimeoutManager,
friend class detail::EventBaseLocalBase;
template <typename T>
friend class EventBaseLocal;
std::unordered_map<std::size_t, std::shared_ptr<void>> localStorage_;
std::unordered_map<std::size_t, erased_unique_ptr> localStorage_;
std::unordered_set<detail::EventBaseLocalBaseBase*> localStorageToDtor_;
folly::once_flag virtualEventBaseInitFlag_;
......
......@@ -20,6 +20,7 @@
#include <thread>
#include <folly/MapUtil.h>
#include <folly/Memory.h>
namespace folly {
namespace detail {
......@@ -56,12 +57,17 @@ void EventBaseLocalBase::onEventBaseDestruction(EventBase& evb) {
eventBases_.wlock()->erase(&evb);
}
void EventBaseLocalBase::setVoid(EventBase& evb, std::shared_ptr<void>&& ptr) {
void EventBaseLocalBase::setVoid(
EventBase& evb, void* ptr, void (*dtor)(void*)) {
// construct the unique-ptr eagerly, just in case anything between this and
// the emplace below could throw
auto erased = erased_unique_ptr{ptr, dtor};
evb.dcheckIsInEventBaseThread();
auto alreadyExists = evb.localStorage_.find(key_) != evb.localStorage_.end();
evb.localStorage_.emplace(key_, std::move(ptr));
evb.localStorage_.emplace(key_, std::move(erased));
if (!alreadyExists) {
eventBases_.wlock()->insert(&evb);
......
......@@ -38,7 +38,7 @@ class EventBaseLocalBase : public EventBaseLocalBaseBase {
void onEventBaseDestruction(EventBase& evb) override;
protected:
void setVoid(EventBase& evb, std::shared_ptr<void>&& ptr);
void setVoid(EventBase& evb, void* ptr, void (*dtor)(void*));
void* getVoid(EventBase& evb);
folly::Synchronized<std::unordered_set<EventBase*>> eventBases_;
......@@ -81,16 +81,14 @@ class EventBaseLocal : public detail::EventBaseLocalBase {
void emplace(EventBase& evb, T* ptr) {
DCHECK(ptr != nullptr);
std::shared_ptr<T> smartPtr(ptr);
setVoid(evb, std::move(smartPtr));
setVoid(evb, ptr, detail::thunk::ruin<T>);
}
template <typename... Args>
T& emplace(EventBase& evb, Args&&... args) {
auto smartPtr = std::make_shared<T>(std::forward<Args>(args)...);
auto& ref = *smartPtr;
setVoid(evb, std::move(smartPtr));
return ref;
auto ptr = new T(static_cast<Args&&>(args)...);
setVoid(evb, ptr, detail::thunk::ruin<T>);
return *ptr;
}
template <typename... Args>
......@@ -98,10 +96,9 @@ class EventBaseLocal : public detail::EventBaseLocalBase {
if (auto ptr = getVoid(evb)) {
return *static_cast<T*>(ptr);
}
auto smartPtr = std::make_shared<T>(std::forward<Args>(args)...);
auto& ref = *smartPtr;
setVoid(evb, std::move(smartPtr));
return ref;
auto ptr = new T(static_cast<Args&&>(args)...);
setVoid(evb, ptr, detail::thunk::ruin<T>);
return *ptr;
}
template <typename Func>
......@@ -112,10 +109,9 @@ class EventBaseLocal : public detail::EventBaseLocalBase {
if (auto ptr = getVoid(evb)) {
return *static_cast<T*>(ptr);
}
auto smartPtr = std::shared_ptr<T>(new T(fn()));
auto& ref = *smartPtr;
setVoid(evb, std::move(smartPtr));
return ref;
auto ptr = new T(fn());
setVoid(evb, ptr, detail::thunk::ruin<T>);
return *ptr;
}
};
......
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