-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent redundant dispatches onto RuntimeExecutor queue in Asynchrono…
…usEventBeat::induce Summary: Changelog: [internal] Current implementation of `AsynchronousEventBeat` dispatches lambdas through `RuntimeExecutor` regardless if it has done so previously. So if `AsynchronousEventBeat::induce` is called 30 times, it will dispatch 30 lambdas. In `AsynchronousEventBeatV2`, we make sure only single lambda is dispatched to `RuntimeExecutor` at a time. Reviewed By: mdvacca Differential Revision: D27940300 fbshipit-source-id: 2bad25c86315c1712b4a1da8c1d4702734cec70f
- Loading branch information
1 parent
09cb12c
commit 1b59263
Showing
3 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
ReactCommon/react/renderer/scheduler/AsynchronousEventBeatV2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include "AsynchronousEventBeatV2.h" | ||
|
||
#include <react/debug/react_native_assert.h> | ||
|
||
namespace facebook::react { | ||
|
||
AsynchronousEventBeatV2::AsynchronousEventBeatV2( | ||
RunLoopObserver::Unique uiRunLoopObserver, | ||
RuntimeExecutor runtimeExecutor) | ||
: EventBeat({}), | ||
uiRunLoopObserver_(std::move(uiRunLoopObserver)), | ||
runtimeExecutor_(std::move(runtimeExecutor)) { | ||
uiRunLoopObserver_->setDelegate(this); | ||
uiRunLoopObserver_->enable(); | ||
} | ||
|
||
void AsynchronousEventBeatV2::activityDidChange( | ||
RunLoopObserver::Delegate const *delegate, | ||
RunLoopObserver::Activity) const noexcept { | ||
react_native_assert(delegate == this); | ||
induce(); | ||
} | ||
|
||
void AsynchronousEventBeatV2::induce() const { | ||
if (!isRequested_ || isBeatCallbackScheduled_) { | ||
return; | ||
} | ||
|
||
isRequested_ = false; | ||
|
||
// Here we know that `this` object exists because the caller has a strong | ||
// pointer to `owner`. To ensure the object will exist inside | ||
// `runtimeExecutor_` callback, we need to copy the pointer there. | ||
auto weakOwner = uiRunLoopObserver_->getOwner(); | ||
|
||
isBeatCallbackScheduled_ = true; | ||
|
||
runtimeExecutor_([this, weakOwner](jsi::Runtime &runtime) { | ||
isBeatCallbackScheduled_ = false; | ||
|
||
auto owner = weakOwner.lock(); | ||
if (!owner) { | ||
return; | ||
} | ||
|
||
if (beatCallback_) { | ||
beatCallback_(runtime); | ||
} | ||
}); | ||
} | ||
} // namespace facebook::react |
41 changes: 41 additions & 0 deletions
41
ReactCommon/react/renderer/scheduler/AsynchronousEventBeatV2.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include <ReactCommon/RuntimeExecutor.h> | ||
#include <react/renderer/core/EventBeat.h> | ||
#include <react/utils/RunLoopObserver.h> | ||
|
||
namespace facebook::react { | ||
|
||
/* | ||
* Event beat associated with JavaScript runtime. | ||
* The beat is called on `RuntimeExecutor`'s thread induced by the UI thread | ||
* event loop. | ||
*/ | ||
class AsynchronousEventBeatV2 : public EventBeat, | ||
public RunLoopObserver::Delegate { | ||
public: | ||
AsynchronousEventBeatV2( | ||
RunLoopObserver::Unique uiRunLoopObserver, | ||
RuntimeExecutor runtimeExecutor); | ||
|
||
void induce() const override; | ||
|
||
#pragma mark - RunLoopObserver::Delegate | ||
|
||
void activityDidChange( | ||
RunLoopObserver::Delegate const *delegate, | ||
RunLoopObserver::Activity activity) const noexcept override; | ||
|
||
private: | ||
RunLoopObserver::Unique uiRunLoopObserver_; | ||
RuntimeExecutor runtimeExecutor_; | ||
|
||
mutable std::atomic<bool> isBeatCallbackScheduled_{false}; | ||
}; | ||
|
||
} // namespace facebook::react |