mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Summary: # Summary In previous diffs earlier in 2020, we made changes to detect and optimize reordering of views when the order of views changed underneath the same parent. However, until now we have ignored reparenting and there's evidence of issues because of that. Because Fabric flattens views more aggressively, reparenting is also marginally more likely to happen. This diff introduces a very general Reparenting detection. It will work with view flattening/unflattening, as well as tree grafting - subtrees moved to entirely different parts of the tree, not just a single parent disappearing or reappearing because of flattening/unflattening. There is also another consideration: previously, we were generating strictly too many Create+Delete operations that were redundant and could cause consistency issues, crashes, or bugs on platforms that do not handle that gracefully - especially since the ordering of the Create+Delete is not guaranteed (a reparented view could be created "first" and then the differ could later issue a "delete" for the same view). Intuition behind how it works: we know the cases where we can detect reparenting: it's when nodes are *not* matched up with another node from the other tree, and we're either trying to delete an entire subtree, or create an entire subtree. For perf reasons, we generate whatever set of operations comes first (say, we generate all the Delete and Remove instructions) and take note in the `ReparentingMetadata` data-structure that Delete and/or Remove have been performed for each tag (if ordering is different, we do the same for Create+Insert if those come first). Then if we later detect a corresponding subtree creation/deletion, we don't generate those mutations and we mark the previous mutations for deletion. This incurs some map lookup cost, but this is only wasteful for commits where a large tree is deleted and a large tree is created, without reparenting. We may be able to improve perf further for certain edge-cases in the future. # Why can't we solve this in JS? Two things: 1. We certainly can avoid reparenting situations in JS, but it's trickier than before because of Fabric's view flattening logic - product engineers would have to think much harder about how to prevent reparenting in the general case. 2. In the case of specific views like BottomSheet that may crash if they're reparented, the solution is to make sure that the BottomSheet and the first child of the BottomSheet is never memoized, so that lifecycle functions and render are called more often; and that in every render, the BottomSheet manually clones its child, so that when the Views are recreated, the child of the BottomSheet has a tag and is an entirely different instance. This is certainly possible to do but feels like an onerous requirement for product teams, and it could be challenging to track down every specific BottomSheet that is memoized and/or hoist them higher in the view hierarchy so they're not reparented as often. Reviewed By: shergin Differential Revision: D23123575 fbshipit-source-id: 2fa7e1f026f87b6f0c60cad469a3ba85cdc234de
147 lines
4.9 KiB
C++
147 lines
4.9 KiB
C++
/*
|
|
* 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.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <memory>
|
|
#include <mutex>
|
|
|
|
#include <ReactCommon/RuntimeExecutor.h>
|
|
#include <react/config/ReactNativeConfig.h>
|
|
#include <react/renderer/componentregistry/ComponentDescriptorFactory.h>
|
|
#include <react/renderer/components/root/RootComponentDescriptor.h>
|
|
#include <react/renderer/core/ComponentDescriptor.h>
|
|
#include <react/renderer/core/LayoutConstraints.h>
|
|
#include <react/renderer/mounting/MountingOverrideDelegate.h>
|
|
#include <react/renderer/scheduler/SchedulerDelegate.h>
|
|
#include <react/renderer/scheduler/SchedulerToolbox.h>
|
|
#include <react/renderer/uimanager/UIManagerAnimationDelegate.h>
|
|
#include <react/renderer/uimanager/UIManagerBinding.h>
|
|
#include <react/renderer/uimanager/UIManagerDelegate.h>
|
|
#include <react/utils/ContextContainer.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
/*
|
|
* Scheduler coordinates Shadow Tree updates and event flows.
|
|
*/
|
|
class Scheduler final : public UIManagerDelegate {
|
|
public:
|
|
Scheduler(
|
|
SchedulerToolbox schedulerToolbox,
|
|
UIManagerAnimationDelegate *animationDelegate,
|
|
SchedulerDelegate *delegate);
|
|
~Scheduler();
|
|
|
|
#pragma mark - Surface Management
|
|
|
|
void startSurface(
|
|
SurfaceId surfaceId,
|
|
const std::string &moduleName,
|
|
const folly::dynamic &initialProps,
|
|
const LayoutConstraints &layoutConstraints = {},
|
|
const LayoutContext &layoutContext = {},
|
|
std::weak_ptr<MountingOverrideDelegate const> mountingOverrideDelegate =
|
|
{}) const;
|
|
|
|
void renderTemplateToSurface(
|
|
SurfaceId surfaceId,
|
|
const std::string &uiTemplate);
|
|
|
|
void stopSurface(SurfaceId surfaceId) const;
|
|
|
|
Size measureSurface(
|
|
SurfaceId surfaceId,
|
|
const LayoutConstraints &layoutConstraints,
|
|
const LayoutContext &layoutContext) const;
|
|
|
|
/*
|
|
* Applies given `layoutConstraints` and `layoutContext` to a Surface.
|
|
* The user interface will be relaid out as a result. The operation will be
|
|
* performed synchronously (including mounting) if the method is called
|
|
* on the main thread.
|
|
* Can be called from any thread.
|
|
*/
|
|
void constraintSurfaceLayout(
|
|
SurfaceId surfaceId,
|
|
const LayoutConstraints &layoutConstraints,
|
|
const LayoutContext &layoutContext) const;
|
|
|
|
/*
|
|
* This is broken. Please do not use.
|
|
* `ComponentDescriptor`s are not designed to be used outside of `UIManager`,
|
|
* there is no any garantees about their lifetime.
|
|
*/
|
|
ComponentDescriptor const *
|
|
findComponentDescriptorByHandle_DO_NOT_USE_THIS_IS_BROKEN(
|
|
ComponentHandle handle) const;
|
|
|
|
MountingCoordinator::Shared findMountingCoordinator(
|
|
SurfaceId surfaceId) const;
|
|
|
|
#pragma mark - Delegate
|
|
|
|
/*
|
|
* Sets and gets the Scheduler's delegate.
|
|
* If you requesting a ComponentDescriptor and unsure that it's there, you are
|
|
* doing something wrong.
|
|
*/
|
|
void setDelegate(SchedulerDelegate *delegate);
|
|
SchedulerDelegate *getDelegate() const;
|
|
|
|
#pragma mark - UIManagerAnimationDelegate
|
|
// This is not needed on iOS or any platform that has a "pull" instead of
|
|
// "push" MountingCoordinator model. This just tells the delegate an update
|
|
// is available and that it should `pullTransaction`; we may want to rename
|
|
// this to be more generic and not animation-specific.
|
|
void animationTick() const;
|
|
|
|
#pragma mark - UIManagerDelegate
|
|
|
|
void uiManagerDidFinishTransaction(
|
|
MountingCoordinator::Shared const &mountingCoordinator) override;
|
|
void uiManagerDidCreateShadowNode(
|
|
const ShadowNode::Shared &shadowNode) override;
|
|
void uiManagerDidDispatchCommand(
|
|
const ShadowNode::Shared &shadowNode,
|
|
std::string const &commandName,
|
|
folly::dynamic const args) override;
|
|
void uiManagerDidSetJSResponder(
|
|
SurfaceId surfaceId,
|
|
const ShadowNode::Shared &shadowView,
|
|
bool blockNativeResponder) override;
|
|
void uiManagerDidClearJSResponder() override;
|
|
|
|
private:
|
|
SchedulerDelegate *delegate_;
|
|
SharedComponentDescriptorRegistry componentDescriptorRegistry_;
|
|
std::unique_ptr<const RootComponentDescriptor> rootComponentDescriptor_;
|
|
RuntimeExecutor runtimeExecutor_;
|
|
std::shared_ptr<UIManager> uiManager_;
|
|
std::shared_ptr<const ReactNativeConfig> reactNativeConfig_;
|
|
|
|
/*
|
|
* At some point, we have to have an owning shared pointer to something that
|
|
* will become an `EventDispatcher` a moment later. That's why we have it as a
|
|
* pointer to an optional: we construct the pointer first, share that with
|
|
* parts that need to have ownership (and only ownership) of that, and then
|
|
* fill the optional.
|
|
*/
|
|
std::shared_ptr<better::optional<EventDispatcher const>> eventDispatcher_;
|
|
|
|
/*
|
|
* Temporary flags.
|
|
*/
|
|
bool enableReparentingDetection_{false};
|
|
bool enableNewStateReconciliation_{false};
|
|
bool removeOutstandingSurfacesOnDestruction_{false};
|
|
};
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|