From 8b226291e17d1c14167b084883fe73e6010cfd3f Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Tue, 2 Apr 2024 14:04:02 -0400 Subject: [PATCH] Gate root.tag checks This field is unnecessary when legacy mode is disabled. --- .../react-reconciler/src/ReactFiberRoot.js | 22 ++++++++++++------- .../src/ReactFiberRootScheduler.js | 12 +++++++--- .../react-reconciler/src/ReactFiberThrow.js | 2 +- .../src/ReactFiberWorkLoop.js | 17 +++++++++----- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/packages/react-reconciler/src/ReactFiberRoot.js b/packages/react-reconciler/src/ReactFiberRoot.js index ce63425a542f6..e69989e61624d 100644 --- a/packages/react-reconciler/src/ReactFiberRoot.js +++ b/packages/react-reconciler/src/ReactFiberRoot.js @@ -33,6 +33,7 @@ import { enableProfilerTimer, enableUpdaterTracking, enableTransitionTracing, + disableLegacyMode, } from 'shared/ReactFeatureFlags'; import {initializeUpdateQueue} from './ReactFiberClassUpdateQueue'; import {LegacyRoot, ConcurrentRoot} from './ReactRootTags'; @@ -56,7 +57,7 @@ function FiberRootNode( onRecoverableError: any, formState: ReactFormState | null, ) { - this.tag = tag; + this.tag = disableLegacyMode ? ConcurrentRoot : tag; this.containerInfo = containerInfo; this.pendingChildren = null; this.current = null; @@ -123,13 +124,18 @@ function FiberRootNode( } if (__DEV__) { - switch (tag) { - case ConcurrentRoot: - this._debugRootType = hydrate ? 'hydrateRoot()' : 'createRoot()'; - break; - case LegacyRoot: - this._debugRootType = hydrate ? 'hydrate()' : 'render()'; - break; + if (disableLegacyMode) { + // TODO: This varies by each renderer. + this._debugRootType = hydrate ? 'hydrateRoot()' : 'createRoot()'; + } else { + switch (tag) { + case ConcurrentRoot: + this._debugRootType = hydrate ? 'hydrateRoot()' : 'createRoot()'; + break; + case LegacyRoot: + this._debugRootType = hydrate ? 'hydrate()' : 'render()'; + break; + } } } } diff --git a/packages/react-reconciler/src/ReactFiberRootScheduler.js b/packages/react-reconciler/src/ReactFiberRootScheduler.js index d03131ce1751a..12f7660596664 100644 --- a/packages/react-reconciler/src/ReactFiberRootScheduler.js +++ b/packages/react-reconciler/src/ReactFiberRootScheduler.js @@ -12,7 +12,10 @@ import type {Lane} from './ReactFiberLane'; import type {PriorityLevel} from 'scheduler/src/SchedulerPriorities'; import type {BatchConfigTransition} from './ReactFiberTracingMarkerComponent'; -import {enableDeferRootSchedulingToMicrotask} from 'shared/ReactFeatureFlags'; +import { + disableLegacyMode, + enableDeferRootSchedulingToMicrotask, +} from 'shared/ReactFeatureFlags'; import { NoLane, NoLanes, @@ -131,6 +134,7 @@ export function ensureRootIsScheduled(root: FiberRoot): void { if ( __DEV__ && + !disableLegacyMode && ReactCurrentActQueue.isBatchingLegacy && root.tag === LegacyRoot ) { @@ -148,7 +152,9 @@ export function flushSyncWorkOnAllRoots() { export function flushSyncWorkOnLegacyRootsOnly() { // This is allowed to be called synchronously, but the caller should check // the execution context first. - flushSyncWorkAcrossRoots_impl(true); + if (!disableLegacyMode) { + flushSyncWorkAcrossRoots_impl(true); + } } function flushSyncWorkAcrossRoots_impl(onlyLegacy: boolean) { @@ -171,7 +177,7 @@ function flushSyncWorkAcrossRoots_impl(onlyLegacy: boolean) { didPerformSomeWork = false; let root = firstScheduledRoot; while (root !== null) { - if (onlyLegacy && root.tag !== LegacyRoot) { + if (onlyLegacy && (disableLegacyMode || root.tag !== LegacyRoot)) { // Skip non-legacy roots. } else { const workInProgressRoot = getWorkInProgressRoot(); diff --git a/packages/react-reconciler/src/ReactFiberThrow.js b/packages/react-reconciler/src/ReactFiberThrow.js index 77fd54097df1e..ea55bdb2f82c3 100644 --- a/packages/react-reconciler/src/ReactFiberThrow.js +++ b/packages/react-reconciler/src/ReactFiberThrow.js @@ -497,7 +497,7 @@ function throwException( // No boundary was found. Unless this is a sync update, this is OK. // We can suspend and wait for more data to arrive. - if (root.tag === ConcurrentRoot) { + if (disableLegacyMode || root.tag === ConcurrentRoot) { // In a concurrent root, suspending without a Suspense boundary is // allowed. It will suspend indefinitely without committing. // diff --git a/packages/react-reconciler/src/ReactFiberWorkLoop.js b/packages/react-reconciler/src/ReactFiberWorkLoop.js index 65a972078579e..09b9f579adfc5 100644 --- a/packages/react-reconciler/src/ReactFiberWorkLoop.js +++ b/packages/react-reconciler/src/ReactFiberWorkLoop.js @@ -1369,7 +1369,10 @@ export function performSyncWorkOnRoot(root: FiberRoot, lanes: Lanes): null { } let exitStatus = renderRootSync(root, lanes); - if (root.tag !== LegacyRoot && exitStatus === RootErrored) { + if ( + (disableLegacyMode || root.tag !== LegacyRoot) && + exitStatus === RootErrored + ) { // If something threw an error, try rendering one more time. We'll render // synchronously to block concurrent data mutations, and we'll includes // all pending updates are included. If it still fails after the second @@ -1517,6 +1520,7 @@ export function flushSync(fn: (() => R) | void): R | void { // next event, not at the end of the previous one. if ( rootWithPendingPassiveEffects !== null && + !disableLegacyMode && rootWithPendingPassiveEffects.tag === LegacyRoot && (executionContext & (RenderContext | CommitContext)) === NoContext ) { @@ -3043,7 +3047,10 @@ function commitRootImpl( // TODO: We can optimize this by not scheduling the callback earlier. Since we // currently schedule the callback in multiple places, will wait until those // are consolidated. - if (includesSyncLane(pendingPassiveEffectsLanes) && root.tag !== LegacyRoot) { + if ( + includesSyncLane(pendingPassiveEffectsLanes) && + (disableLegacyMode || root.tag !== LegacyRoot) + ) { flushPassiveEffects(); } @@ -3724,11 +3731,11 @@ function commitDoubleInvokeEffectsInDEV( hasPassiveEffects: boolean, ) { if (__DEV__) { - if (useModernStrictMode && root.tag !== LegacyRoot) { + if (useModernStrictMode && (disableLegacyMode || root.tag !== LegacyRoot)) { let doubleInvokeEffects = true; if ( - root.tag === ConcurrentRoot && + (disableLegacyMode || root.tag === ConcurrentRoot) && !(root.current.mode & (StrictLegacyMode | StrictEffectsMode)) ) { doubleInvokeEffects = false; @@ -4000,7 +4007,7 @@ function warnIfUpdatesNotWrappedWithActDEV(fiber: Fiber): void { function warnIfSuspenseResolutionNotWrappedWithActDEV(root: FiberRoot): void { if (__DEV__) { if ( - root.tag !== LegacyRoot && + (disableLegacyMode || root.tag !== LegacyRoot) && isConcurrentActEnvironment() && ReactCurrentActQueue.current === null ) {