mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/38706 Fabric has a mechanism to resolve conflicts when there are race conditions committing new versions in different threads. This mechanism forces commits to be done in order, retrying commits when they didn't have time to finish before another concurrent commit. {F1061612667} This mechanism has an important drawback. If we have a continuous stream of short commits (e.g.: animations via Reanimated) and we want to do a large commit (e.g.: coming from React), the large commit could exhaust all attempts to finish and ultimately fail. Every time we tried to commit the large one, another small one would have had a chance to finish, forcing another retry. {F1061614717} In most of the cases where this happens, we didn't even need to fail the commits in the first place! **The only reason why we retry commits is so we make sure we are propagating the last state** (when state propagation is enabled in that commit). We don't reconcile the contents of the commits themselves (the tree we retry is exactly the same, if we exclude state). We can reduce the number of reconciliation failures (and completely remove them in the case of animations) if instead of checking if the base revision for a commit changed, we checked if the version of the state we propagated changed. We would have 3 scenarios when doing that: 1. Commit creating state vs. commit reusing state. If the commit creating the state went first, we would have to retry the commit reusing the state (to make sure we're reusing the new state). If the commit reusing the state went first, we wouldn't have conflicts, and we would just apply the new state on top of it. {F1061617730} 2. Commit reusing state vs. another commit reusing state. In this case the order doesn't matter and we don't need to trigger a conflict. Commits would be updated in the order in which they are applied. {F1061618406} 3. Commit creating state vs. commit creating state. In this case, we are not propagating state, so retrying the commits wouldn't do anything. We can ignore the conflicts in this case too. {F1061618689} Changelog: [internal] Reviewed By: sammy-SC Differential Revision: D47915384 fbshipit-source-id: bb4510c59bf32ca342c02f3bdb7029b545f56d99 Co-authored-by: Samuel Susla <samuel.susla@gmail.com> Co-authored-by: David Vacca Co-authored-by: Tomek Zawadzki <tomasz.zawadzki@swmansion.com> Co-authored-by: Krzysztof Piaskowy <krzysztof.piaskowy@swmansion.com>
156 lines
4.7 KiB
C++
156 lines
4.7 KiB
C++
/*
|
|
* Copyright (c) Meta Platforms, Inc. and 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 <react/renderer/components/root/RootComponentDescriptor.h>
|
|
#include <react/renderer/components/root/RootShadowNode.h>
|
|
#include <react/renderer/core/LayoutConstraints.h>
|
|
#include <react/renderer/core/ReactPrimitives.h>
|
|
#include <react/renderer/core/ShadowNode.h>
|
|
#include <react/renderer/mounting/MountingCoordinator.h>
|
|
#include <react/renderer/mounting/ShadowTreeDelegate.h>
|
|
#include <react/renderer/mounting/ShadowTreeRevision.h>
|
|
#include <react/utils/ContextContainer.h>
|
|
#include "MountingOverrideDelegate.h"
|
|
|
|
namespace facebook::react {
|
|
|
|
using ShadowTreeCommitTransaction = std::function<RootShadowNode::Unshared(
|
|
RootShadowNode const &oldRootShadowNode)>;
|
|
|
|
/*
|
|
* Represents the shadow tree and its lifecycle.
|
|
*/
|
|
class ShadowTree final {
|
|
public:
|
|
using Unique = std::unique_ptr<ShadowTree>;
|
|
|
|
/*
|
|
* Represents a result of a `commit` operation.
|
|
*/
|
|
enum class CommitStatus {
|
|
Succeeded,
|
|
Failed,
|
|
Cancelled,
|
|
};
|
|
|
|
/*
|
|
* Represents commits' side-effects propagation mode.
|
|
*/
|
|
enum class CommitMode {
|
|
// Commits' side-effects are observable via `MountingCoordinator`.
|
|
// The rendering pipeline fully works end-to-end.
|
|
Normal,
|
|
|
|
// Commits' side-effects are *not* observable via `MountingCoordinator`.
|
|
// The mounting phase is skipped in the rendering pipeline.
|
|
Suspended,
|
|
};
|
|
|
|
struct CommitOptions {
|
|
// When set to true, Shadow Node state from current revision will be applied
|
|
// to the new revision. For more details see
|
|
// https://reactnative.dev/architecture/render-pipeline#react-native-renderer-state-updates
|
|
bool enableStateReconciliation{false};
|
|
|
|
// Indicates if mounting will be triggered synchronously and React will
|
|
// not get a chance to interrupt painting.
|
|
// This should be set to `false` when a commit is coming from React. It
|
|
// will then let React run layout effects and apply updates before paint.
|
|
// For all other commits, should be true.
|
|
bool mountSynchronously{true};
|
|
|
|
// Called during `tryCommit` phase. Returning true indicates current commit
|
|
// should yield to the next commit.
|
|
std::function<bool()> shouldYield;
|
|
};
|
|
|
|
/*
|
|
* Creates a new shadow tree instance.
|
|
*/
|
|
ShadowTree(
|
|
SurfaceId surfaceId,
|
|
LayoutConstraints const &layoutConstraints,
|
|
LayoutContext const &layoutContext,
|
|
ShadowTreeDelegate const &delegate,
|
|
ContextContainer const &contextContainer);
|
|
|
|
~ShadowTree();
|
|
|
|
/*
|
|
* Returns the `SurfaceId` associated with the shadow tree.
|
|
*/
|
|
SurfaceId getSurfaceId() const;
|
|
|
|
/*
|
|
* Sets and gets the commit mode.
|
|
* Changing commit mode from `Suspended` to `Normal` will flush all suspended
|
|
* changes to `MountingCoordinator`.
|
|
*/
|
|
void setCommitMode(CommitMode commitMode) const;
|
|
CommitMode getCommitMode() const;
|
|
|
|
/*
|
|
* Performs commit calling `transaction` function with a `oldRootShadowNode`
|
|
* and expecting a `newRootShadowNode` as a return value.
|
|
* The `transaction` function can cancel commit returning `nullptr`.
|
|
*/
|
|
CommitStatus tryCommit(
|
|
const ShadowTreeCommitTransaction &transaction,
|
|
const CommitOptions &commitOptions) const;
|
|
|
|
/*
|
|
* Calls `tryCommit` in a loop until it finishes successfully.
|
|
*/
|
|
CommitStatus commit(
|
|
const ShadowTreeCommitTransaction &transaction,
|
|
const CommitOptions &commitOptions) const;
|
|
|
|
/*
|
|
* Returns a `ShadowTreeRevision` representing the momentary state of
|
|
* the `ShadowTree`.
|
|
*/
|
|
ShadowTreeRevision getCurrentRevision() const;
|
|
|
|
/*
|
|
* Commit an empty tree (a new `RootShadowNode` with no children).
|
|
*/
|
|
void commitEmptyTree() const;
|
|
|
|
/**
|
|
* Forces the ShadowTree to ping its delegate that an update is available.
|
|
* Useful for animations on Android.
|
|
* @return
|
|
*/
|
|
void notifyDelegatesOfUpdates() const;
|
|
|
|
MountingCoordinator::Shared getMountingCoordinator() const;
|
|
|
|
private:
|
|
constexpr static ShadowTreeRevision::Number INITIAL_REVISION{0};
|
|
|
|
void mount(ShadowTreeRevision revision, bool mountSynchronously) const;
|
|
|
|
void emitLayoutEvents(
|
|
std::vector<LayoutableShadowNode const *> &affectedLayoutableNodes) const;
|
|
|
|
SurfaceId const surfaceId_;
|
|
ShadowTreeDelegate const &delegate_;
|
|
mutable std::shared_mutex commitMutex_;
|
|
mutable CommitMode commitMode_{
|
|
CommitMode::Normal}; // Protected by `commitMutex_`.
|
|
mutable ShadowTreeRevision currentRevision_; // Protected by `commitMutex_`.
|
|
mutable ShadowTreeRevision::Number
|
|
lastRevisionNumberWithNewState_; // Protected by `commitMutex_`.
|
|
MountingCoordinator::Shared mountingCoordinator_;
|
|
};
|
|
|
|
} // namespace facebook::react
|