Commit 73242d19 authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook GitHub Bot

Replace mutexes in observers with SharexMutex to reduce memory usage

Summary: sizeof(SharedMutex) = 4, sizeof(std::mutex) = 40. This diff makes sure that we use smaller mutexes both in Observer Core and in Observables given that we may end up with many instances of these objects, yet there mutexes are accessed only on the update path.

Reviewed By: praihan

Differential Revision: D32271615

fbshipit-source-id: ba7d04632a677a9eb4ae398d681dadac3b06f033
parent de5021e9
...@@ -43,7 +43,7 @@ class ObserverCreatorContext { ...@@ -43,7 +43,7 @@ class ObserverCreatorContext {
} }
std::shared_ptr<const T> get() { std::shared_ptr<const T> get() {
auto state = state_.lock(); auto state = state_.wlock();
state->updateRequested = false; state->updateRequested = false;
return state->value; return state->value;
} }
...@@ -62,10 +62,10 @@ class ObserverCreatorContext { ...@@ -62,10 +62,10 @@ class ObserverCreatorContext {
// important to not hold state_ lock while running it to avoid possible // important to not hold state_ lock while running it to avoid possible
// lock inversion with another code path that needs state_ lock (e.g. // lock inversion with another code path that needs state_ lock (e.g.
// get()). // get()).
std::lock_guard<std::mutex> updateLockGuard(updateLock_); std::lock_guard<SharedMutex> updateLockGuard(updateLock_);
auto newValue = Traits::get(observable_); auto newValue = Traits::get(observable_);
auto state = state_.lock(); auto state = state_.wlock();
if (!state->updateValue(std::move(newValue))) { if (!state->updateValue(std::move(newValue))) {
// Value didn't change, so we can skip the version update. // Value didn't change, so we can skip the version update.
return nullptr; return nullptr;
...@@ -88,7 +88,7 @@ class ObserverCreatorContext { ...@@ -88,7 +88,7 @@ class ObserverCreatorContext {
} }
private: private:
std::mutex updateLock_; SharedMutex updateLock_;
struct State { struct State {
bool updateValue(std::shared_ptr<const T> newValue) { bool updateValue(std::shared_ptr<const T> newValue) {
auto newValuePtr = newValue.get(); auto newValuePtr = newValue.get();
...@@ -102,7 +102,7 @@ class ObserverCreatorContext { ...@@ -102,7 +102,7 @@ class ObserverCreatorContext {
std::shared_ptr<const T> value; std::shared_ptr<const T> value;
bool updateRequested{false}; bool updateRequested{false};
}; };
folly::Synchronized<State, std::mutex> state_; folly::Synchronized<State> state_;
observer_detail::Core::WeakPtr coreWeak_; observer_detail::Core::WeakPtr coreWeak_;
......
...@@ -54,7 +54,7 @@ size_t Core::refresh(size_t version) { ...@@ -54,7 +54,7 @@ size_t Core::refresh(size_t version) {
} }
{ {
std::lock_guard<std::mutex> lgRefresh(refreshMutex_); std::lock_guard<SharedMutex> lgRefresh(refreshMutex_);
// Recheck in case this code was already refreshed // Recheck in case this code was already refreshed
if (version_ >= version) { if (version_ >= version) {
......
...@@ -111,7 +111,7 @@ class Core : public std::enable_shared_from_this<Core> { ...@@ -111,7 +111,7 @@ class Core : public std::enable_shared_from_this<Core> {
folly::Function<std::shared_ptr<const void>()> creator_; folly::Function<std::shared_ptr<const void>()> creator_;
std::mutex refreshMutex_; SharedMutex refreshMutex_;
bool forceRefresh_{false}; bool forceRefresh_{false};
}; };
......
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