mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Schedule JS revision merge on the main thread (#54837)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/54837 Changelog: [General][Changed] - The JS revision merge now is scheduled on the main thread Changes the Shadow Tree branching to schedule JS revision merge on the main thread instead of running it immediately at the end of the event loop. This will prevent shadow tree exhaustion when committing to the main branch, since all commits there will be happening on the same thread. Reviewed By: javache, rubennorte Differential Revision: D88736123 fbshipit-source-id: da9fc04bc80bd76749768fd3bfdbaa6a9c9ac368
This commit is contained in:
1 parent
45b4817414
commit
47e5e6c394
14 files changed
+101
-4
No files matched your search
@@ -31,6 +31,8 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
- (void)schedulerShouldRenderTransactions:
|
||||
(std::shared_ptr<const facebook::react::MountingCoordinator>)mountingCoordinator;
|
||||
|
||||
- (void)schedulerShouldMergeReactRevision:(facebook::react::SurfaceId)surfaceId;
|
||||
|
||||
- (void)schedulerDidDispatchCommand:(const facebook::react::ShadowView &)shadowView
|
||||
commandName:(const std::string &)commandName
|
||||
args:(const folly::dynamic &)args;
|
||||
|
||||
@@ -37,6 +37,12 @@ class SchedulerDelegateProxy : public SchedulerDelegate {
|
||||
[scheduler.delegate schedulerShouldRenderTransactions:mountingCoordinator];
|
||||
}
|
||||
|
||||
void schedulerShouldMergeReactRevision(SurfaceId surfaceId) override
|
||||
{
|
||||
RCTScheduler *scheduler = (__bridge RCTScheduler *)scheduler_;
|
||||
[scheduler.delegate schedulerShouldMergeReactRevision:surfaceId];
|
||||
}
|
||||
|
||||
void schedulerDidRequestPreliminaryViewAllocation(const ShadowNode &shadowNode) override
|
||||
{
|
||||
// Does nothing.
|
||||
|
||||
@@ -307,6 +307,15 @@ using namespace facebook::react;
|
||||
[_mountingManager scheduleTransaction:mountingCoordinator];
|
||||
}
|
||||
|
||||
- (void)schedulerShouldMergeReactRevision:(SurfaceId)surfaceId
|
||||
{
|
||||
auto scheduler = [self scheduler];
|
||||
RCTExecuteOnMainQueue(^{
|
||||
scheduler.uiManager->getShadowTreeRegistry().visit(
|
||||
surfaceId, [](const ShadowTree &shadowTree) { shadowTree.mergeReactRevision(); });
|
||||
});
|
||||
}
|
||||
|
||||
- (void)schedulerDidDispatchCommand:(const ShadowView &)shadowView
|
||||
commandName:(const std::string &)commandName
|
||||
args:(const folly::dynamic &)args
|
||||
|
||||
+19
@@ -964,6 +964,25 @@ public class FabricUIManager
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@AnyThread
|
||||
@ThreadConfined(ANY)
|
||||
private void scheduleReactRevisionMerge(int surfaceId) {
|
||||
if (UiThreadUtil.isOnUiThread()) {
|
||||
if (mBinding != null) {
|
||||
mBinding.mergeReactRevision(surfaceId);
|
||||
}
|
||||
} else {
|
||||
UiThreadUtil.runOnUiThread(
|
||||
() -> {
|
||||
FabricUIManagerBinding binding = mBinding;
|
||||
if (binding != null) {
|
||||
binding.mergeReactRevision(surfaceId);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method initiates preloading of an image specified by ImageSource. It can later be consumed
|
||||
* by an ImageView.
|
||||
|
||||
+2
@@ -89,6 +89,8 @@ internal class FabricUIManagerBinding : HybridClassBase() {
|
||||
animationBackendChoreographer: AnimationBackendChoreographer
|
||||
)
|
||||
|
||||
external fun mergeReactRevision(surfaceId: Int)
|
||||
|
||||
fun register(
|
||||
runtimeExecutor: RuntimeExecutor,
|
||||
runtimeScheduler: RuntimeScheduler,
|
||||
|
||||
@@ -1206,4 +1206,11 @@ void FabricMountingManager::synchronouslyUpdateViewOnUIThread(
|
||||
synchronouslyUpdateViewOnUIThreadJNI(javaUIManager_, viewTag, propsMap);
|
||||
}
|
||||
|
||||
void FabricMountingManager::scheduleReactRevisionMerge(SurfaceId surfaceId) {
|
||||
static const auto scheduleReactRevisionMerge =
|
||||
JFabricUIManager::javaClassStatic()->getMethod<void(int32_t)>(
|
||||
"scheduleReactRevisionMerge");
|
||||
scheduleReactRevisionMerge(javaUIManager_, surfaceId);
|
||||
}
|
||||
|
||||
} // namespace facebook::react
|
||||
@@ -54,6 +54,8 @@ class FabricMountingManager final {
|
||||
|
||||
void synchronouslyUpdateViewOnUIThread(Tag viewTag, const folly::dynamic &props);
|
||||
|
||||
void scheduleReactRevisionMerge(SurfaceId surfaceId);
|
||||
|
||||
private:
|
||||
bool isOnMainThread();
|
||||
|
||||
|
||||
+19
@@ -701,6 +701,23 @@ void FabricUIManagerBinding::schedulerShouldRenderTransactions(
|
||||
}
|
||||
}
|
||||
|
||||
void FabricUIManagerBinding::schedulerShouldMergeReactRevision(
|
||||
SurfaceId surfaceId) {
|
||||
std::shared_lock lock(installMutex_);
|
||||
auto mountingManager =
|
||||
getMountingManager("schedulerShouldMergeReactRevision");
|
||||
if (mountingManager) {
|
||||
mountingManager->scheduleReactRevisionMerge(surfaceId);
|
||||
}
|
||||
}
|
||||
|
||||
void FabricUIManagerBinding::mergeReactRevision(SurfaceId surfaceId) {
|
||||
std::shared_lock lock(installMutex_);
|
||||
scheduler_->getUIManager()->getShadowTreeRegistry().visit(
|
||||
surfaceId,
|
||||
[](const ShadowTree& shadowTree) { shadowTree.mergeReactRevision(); });
|
||||
}
|
||||
|
||||
void FabricUIManagerBinding::schedulerDidRequestPreliminaryViewAllocation(
|
||||
const ShadowNode& shadowNode) {
|
||||
using namespace std::literals::string_view_literals;
|
||||
@@ -838,6 +855,8 @@ void FabricUIManagerBinding::registerNatives() {
|
||||
makeNativeMethod(
|
||||
"setAnimationBackendChoreographer",
|
||||
FabricUIManagerBinding::setAnimationBackendChoreographer),
|
||||
makeNativeMethod(
|
||||
"mergeReactRevision", FabricUIManagerBinding::mergeReactRevision),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -97,6 +97,10 @@ class FabricUIManagerBinding : public jni::HybridClass<FabricUIManagerBinding>,
|
||||
void schedulerShouldRenderTransactions(
|
||||
const std::shared_ptr<const MountingCoordinator> &mountingCoordinator) override;
|
||||
|
||||
void schedulerShouldMergeReactRevision(SurfaceId surfaceId) override;
|
||||
|
||||
void mergeReactRevision(SurfaceId surfaceId);
|
||||
|
||||
void schedulerDidRequestPreliminaryViewAllocation(const ShadowNode &shadowNode) override;
|
||||
|
||||
void schedulerDidDispatchCommand(
|
||||
|
||||
@@ -377,8 +377,9 @@ void Scheduler::uiManagerDidFinishReactCommit(const ShadowTree& shadowTree) {
|
||||
}
|
||||
|
||||
void Scheduler::uiManagerDidPromoteReactRevision(const ShadowTree& shadowTree) {
|
||||
// Replaced by scheduling on the UI thread in the following diff.
|
||||
shadowTree.mergeReactRevision();
|
||||
if (delegate_ != nullptr) {
|
||||
delegate_->schedulerShouldMergeReactRevision(shadowTree.getSurfaceId());
|
||||
}
|
||||
}
|
||||
|
||||
void Scheduler::uiManagerDidStartSurface(const ShadowTree& shadowTree) {
|
||||
|
||||
@@ -38,6 +38,12 @@ class SchedulerDelegate {
|
||||
virtual void schedulerShouldRenderTransactions(
|
||||
const std::shared_ptr<const MountingCoordinator> &mountingCoordinator) = 0;
|
||||
|
||||
/*
|
||||
* Called at the end of the event loop if there were commits to the JS
|
||||
* during the pass and JS branch should be "merged" to the main revision.
|
||||
*/
|
||||
virtual void schedulerShouldMergeReactRevision(SurfaceId surfaceId) = 0;
|
||||
|
||||
/*
|
||||
* Called right after a new ShadowNode was created.
|
||||
*/
|
||||
|
||||
+13
@@ -6,6 +6,7 @@
|
||||
*/
|
||||
|
||||
#include "SchedulerDelegateImpl.h"
|
||||
#include <react/renderer/uimanager/UIManager.h>
|
||||
|
||||
namespace facebook::react {
|
||||
|
||||
@@ -13,6 +14,11 @@ SchedulerDelegateImpl::SchedulerDelegateImpl(
|
||||
std::shared_ptr<IMountingManager> mountingManager) noexcept
|
||||
: mountingManager_(std::move(mountingManager)) {}
|
||||
|
||||
void SchedulerDelegateImpl::setUIManager(
|
||||
std::shared_ptr<UIManager> uiManager) noexcept {
|
||||
uiManager_ = uiManager;
|
||||
}
|
||||
|
||||
void SchedulerDelegateImpl::schedulerDidFinishTransaction(
|
||||
const std::shared_ptr<const MountingCoordinator>& /*mountingCoordinator*/) {
|
||||
// no-op, we will flush the transaction from schedulerShouldRenderTransactions
|
||||
@@ -30,6 +36,13 @@ void SchedulerDelegateImpl::schedulerShouldRenderTransactions(
|
||||
}
|
||||
}
|
||||
|
||||
void SchedulerDelegateImpl::schedulerShouldMergeReactRevision(
|
||||
SurfaceId surfaceId) {
|
||||
uiManager_->getShadowTreeRegistry().visit(
|
||||
surfaceId,
|
||||
[](const ShadowTree& shadowTree) { shadowTree.mergeReactRevision(); });
|
||||
}
|
||||
|
||||
void SchedulerDelegateImpl::schedulerDidRequestPreliminaryViewAllocation(
|
||||
const ShadowNode& shadowNode) {}
|
||||
|
||||
|
||||
+5
@@ -25,12 +25,16 @@ class SchedulerDelegateImpl : public SchedulerDelegate {
|
||||
SchedulerDelegateImpl(const SchedulerDelegateImpl &) = delete;
|
||||
SchedulerDelegateImpl &operator=(const SchedulerDelegateImpl &) = delete;
|
||||
|
||||
void setUIManager(std::shared_ptr<UIManager> uiManager) noexcept;
|
||||
|
||||
private:
|
||||
void schedulerDidFinishTransaction(const std::shared_ptr<const MountingCoordinator> &mountingCoordinator) override;
|
||||
|
||||
void schedulerShouldRenderTransactions(
|
||||
const std::shared_ptr<const MountingCoordinator> &mountingCoordinator) override;
|
||||
|
||||
void schedulerShouldMergeReactRevision(SurfaceId surfaceId) override;
|
||||
|
||||
void schedulerDidRequestPreliminaryViewAllocation(const ShadowNode &shadowNode) override;
|
||||
|
||||
void schedulerDidDispatchCommand(
|
||||
@@ -48,6 +52,7 @@ class SchedulerDelegateImpl : public SchedulerDelegate {
|
||||
void schedulerDidUpdateShadowTree(const std::unordered_map<Tag, folly::dynamic> &tagToProps) override;
|
||||
|
||||
std::shared_ptr<IMountingManager> mountingManager_;
|
||||
std::shared_ptr<UIManager> uiManager_;
|
||||
};
|
||||
|
||||
}; // namespace facebook::react
|
||||
@@ -229,12 +229,14 @@ void ReactHost::createReactInstance() {
|
||||
};
|
||||
toolbox.animationChoreographer = reactInstanceData_->animationChoreographer;
|
||||
|
||||
schedulerDelegate_ = std::make_unique<SchedulerDelegateImpl>(
|
||||
auto schedulerDelegate = std::make_unique<SchedulerDelegateImpl>(
|
||||
reactInstanceData_->mountingManager);
|
||||
scheduler_ =
|
||||
std::make_unique<Scheduler>(toolbox, nullptr, schedulerDelegate_.get());
|
||||
std::make_unique<Scheduler>(toolbox, nullptr, schedulerDelegate.get());
|
||||
|
||||
surfaceManager_ = std::make_unique<SurfaceManager>(*scheduler_);
|
||||
schedulerDelegate->setUIManager(scheduler_->getUIManager());
|
||||
schedulerDelegate_ = std::move(schedulerDelegate);
|
||||
|
||||
reactInstanceData_->mountingManager->setSchedulerTaskExecutor(
|
||||
[this](SchedulerTask&& task) { runOnScheduler(std::move(task)); });
|
||||
|
||||
Reference in new issue
Block a user