From 3e0109050b85eed86eaee3b9affeb512b1e0da9a Mon Sep 17 00:00:00 2001 From: Andrii Grynenko Date: Thu, 15 Jun 2017 14:37:41 -0700 Subject: [PATCH] Fix Observable to not trigger unneccessary refresh if the value didn't change Differential Revision: D5251218 fbshipit-source-id: 1ceb37dd727e8ac2fd842e2c437cdaa9017221c8 --- folly/experimental/observer/Observable-inl.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/folly/experimental/observer/Observable-inl.h b/folly/experimental/observer/Observable-inl.h index 231991e6..14d3339a 100644 --- a/folly/experimental/observer/Observable-inl.h +++ b/folly/experimental/observer/Observable-inl.h @@ -49,7 +49,10 @@ class ObserverCreator::Context { // callbacks (getting new value from observable and storing it into value_ // is not atomic). std::lock_guard lg(updateMutex_); - updateValue(); + if (!updateValue()) { + // Value didn't change, so we can skip the version update. + return; + } bool expected = false; if (updateRequested_.compare_exchange_strong(expected, true)) { @@ -63,12 +66,14 @@ class ObserverCreator::Context { } private: - void updateValue() { + bool updateValue() { auto newValue = Traits::get(observable_); + auto newValuePtr = newValue.get(); if (!newValue) { throw std::logic_error("Observable returned nullptr."); } value_.swap(newValue); + return newValuePtr != newValue.get(); } folly::Synchronized> value_; -- 2.34.1