Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] Root event marks #17772

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/react-reconciler/src/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import {Update, Snapshot} from 'shared/ReactSideEffectTags';
import {
debugRenderPhaseSideEffectsForStrictMode,
disableLegacyContext,
enableRootEventMarks,
warnAboutDeprecatedLifecycles,
} from 'shared/ReactFeatureFlags';
import {workScheduled} from 'shared/RootEventsProfiling';
import ReactStrictModeWarnings from './ReactStrictModeWarnings';
import {isMounted} from 'react-reconciler/reflection';
import {get as getInstance, set as setInstance} from 'shared/ReactInstanceMap';
Expand Down Expand Up @@ -201,6 +203,10 @@ const classComponentUpdater = {

enqueueUpdate(fiber, update);
scheduleWork(fiber, expirationTime);

if (enableRootEventMarks) {
workScheduled('state-update', fiber);
}
},
enqueueReplaceState(inst, payload, callback) {
const fiber = getInstance(inst);
Expand All @@ -225,6 +231,10 @@ const classComponentUpdater = {

enqueueUpdate(fiber, update);
scheduleWork(fiber, expirationTime);

if (enableRootEventMarks) {
workScheduled('state-update', fiber);
}
},
enqueueForceUpdate(inst, callback) {
const fiber = getInstance(inst);
Expand All @@ -248,6 +258,10 @@ const classComponentUpdater = {

enqueueUpdate(fiber, update);
scheduleWork(fiber, expirationTime);

if (enableRootEventMarks) {
workScheduled('state-update', fiber);
}
},
};

Expand Down
6 changes: 6 additions & 0 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import type {SuspenseConfig} from './ReactFiberSuspenseConfig';
import type {ReactPriorityLevel} from './SchedulerWithReactIntegration';

import ReactSharedInternals from 'shared/ReactSharedInternals';
import {enableRootEventMarks} from 'shared/ReactFeatureFlags';
import {workScheduled} from 'shared/RootEventsProfiling';

import {NoWork, Sync} from './ReactFiberExpirationTime';
import {readContext} from './ReactFiberNewContext';
Expand Down Expand Up @@ -1356,6 +1358,10 @@ function dispatchAction<S, A>(
}
}
scheduleWork(fiber, expirationTime);

if (enableRootEventMarks) {
workScheduled('state-update', fiber);
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions packages/react-reconciler/src/ReactFiberReconciler.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import {
import getComponentName from 'shared/getComponentName';
import invariant from 'shared/invariant';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {enableRootEventMarks} from 'shared/ReactFeatureFlags';
import {workScheduled} from 'shared/RootEventsProfiling';

import {getPublicInstance} from './ReactFiberHostConfig';
import {
Expand Down Expand Up @@ -226,9 +228,14 @@ export function updateContainer(
parentComponent: ?React$Component<any, any>,
callback: ?Function,
): ExpirationTime {
if (enableRootEventMarks) {
workScheduled('render', null);
}

if (__DEV__) {
onScheduleRoot(container, element);
}

const current = container.current;
const currentTime = requestCurrentTimeForUpdate();
if (__DEV__) {
Expand Down
6 changes: 6 additions & 0 deletions packages/react-reconciler/src/ReactFiberThrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type {Update} from './ReactUpdateQueue';
import type {Thenable} from './ReactFiberWorkLoop';
import type {SuspenseContext} from './ReactFiberSuspenseContext';

import {enableRootEventMarks} from 'shared/ReactFeatureFlags';
import {componentSuspended} from 'shared/RootEventsProfiling';
import getComponentName from 'shared/getComponentName';
import {
ClassComponent,
Expand Down Expand Up @@ -195,6 +197,10 @@ function throwException(
// This is a thenable.
const thenable: Thenable = (value: any);

if (enableRootEventMarks) {
componentSuspended(sourceFiber);
}

if ((sourceFiber.mode & BlockingMode) === NoMode) {
// Reset the memoizedState to what it was before we attempted
// to render it.
Expand Down
84 changes: 77 additions & 7 deletions packages/react-reconciler/src/ReactFiberWorkLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
enableSuspenseServerRenderer,
replayFailedUnitOfWorkWithInvokeGuardedCallback,
enableProfilerTimer,
enableRootEventMarks,
enableSchedulerTracing,
warnAboutUnmockedScheduler,
flushSuspenseFallbacksInTests,
Expand All @@ -30,6 +31,17 @@ import {
} from 'shared/ReactFeatureFlags';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import invariant from 'shared/invariant';
import {
commitStarted,
commitStopped,
layoutEffectsStarted,
layoutEffectsStopped,
passiveEffectsStarted,
passiveEffectsStopped,
renderAbandoned,
renderStarted,
renderStopped,
} from 'shared/RootEventsProfiling';

import {
scheduleCallback,
Expand Down Expand Up @@ -684,6 +696,9 @@ function performConcurrentWorkOnRoot(root, didTimeout) {
const prevDispatcher = pushDispatcher(root);
const prevInteractions = pushInteractions(root);
startWorkLoopTimer(workInProgress);
if (enableRootEventMarks) {
renderStarted();
}
do {
try {
workLoopConcurrent();
Expand Down Expand Up @@ -1012,6 +1027,10 @@ function performSyncWorkOnRoot(root) {
const prevInteractions = pushInteractions(root);
startWorkLoopTimer(workInProgress);

if (enableRootEventMarks) {
renderStarted();
}

do {
try {
workLoopSync();
Expand Down Expand Up @@ -1715,6 +1734,10 @@ function commitRoot(root) {
}

function commitRootImpl(root, renderPriorityLevel) {
if (enableRootEventMarks) {
commitStarted();
}

do {
// `flushPassiveEffects` will call `flushSyncUpdateQueue` at the end, which
// means `flushPassiveEffects` will sometimes result in additional
Expand All @@ -1724,6 +1747,7 @@ function commitRootImpl(root, renderPriorityLevel) {
// flush synchronous work at the end, to avoid factoring hazards like this.
flushPassiveEffects();
} while (rootWithPendingPassiveEffects !== null);

flushRenderPhaseStrictModeWarningsInDEV();

invariant(
Expand All @@ -1734,6 +1758,10 @@ function commitRootImpl(root, renderPriorityLevel) {
const finishedWork = root.finishedWork;
const expirationTime = root.finishedExpirationTime;
if (finishedWork === null) {
if (enableRootEventMarks) {
commitStopped();
}

return null;
}
root.finishedWork = null;
Expand Down Expand Up @@ -2018,6 +2046,10 @@ function commitRootImpl(root, renderPriorityLevel) {
}

if ((executionContext & LegacyUnbatchedContext) !== NoContext) {
if (enableRootEventMarks) {
commitStopped();
}

// This is a legacy edge case. We just committed the initial mount of
// a ReactDOM.render-ed root inside of batchedUpdates. The commit fired
// synchronously, but layout updates should be deferred until the end
Expand All @@ -2027,6 +2059,11 @@ function commitRootImpl(root, renderPriorityLevel) {

// If layout work was scheduled, flush it now.
flushSyncCallbackQueue();

if (enableRootEventMarks) {
commitStopped();
}

return null;
}

Expand Down Expand Up @@ -2138,6 +2175,9 @@ function commitLayoutEffects(
root: FiberRoot,
committedExpirationTime: ExpirationTime,
) {
if (enableProfilerTimer) {
layoutEffectsStarted();
}
// TODO: Should probably move the bulk of this function to commitWork.
while (nextEffect !== null) {
setCurrentDebugFiberInDEV(nextEffect);
Expand All @@ -2163,16 +2203,35 @@ function commitLayoutEffects(
resetCurrentDebugFiberInDEV();
nextEffect = nextEffect.nextEffect;
}
if (enableProfilerTimer) {
layoutEffectsStopped();
}
}

export function flushPassiveEffects() {
if (pendingPassiveEffectsRenderPriority !== NoPriority) {
const priorityLevel =
pendingPassiveEffectsRenderPriority > NormalPriority
? NormalPriority
: pendingPassiveEffectsRenderPriority;
pendingPassiveEffectsRenderPriority = NoPriority;
return runWithPriority(priorityLevel, flushPassiveEffectsImpl);
if (enableRootEventMarks) {
try {
passiveEffectsStarted();
if (pendingPassiveEffectsRenderPriority !== NoPriority) {
const priorityLevel =
pendingPassiveEffectsRenderPriority > NormalPriority
? NormalPriority
: pendingPassiveEffectsRenderPriority;
pendingPassiveEffectsRenderPriority = NoPriority;
return runWithPriority(priorityLevel, flushPassiveEffectsImpl);
}
} finally {
passiveEffectsStopped();
}
} else {
if (pendingPassiveEffectsRenderPriority !== NoPriority) {
const priorityLevel =
pendingPassiveEffectsRenderPriority > NormalPriority
? NormalPriority
: pendingPassiveEffectsRenderPriority;
pendingPassiveEffectsRenderPriority = NoPriority;
return runWithPriority(priorityLevel, flushPassiveEffectsImpl);
}
}
}

Expand Down Expand Up @@ -2545,13 +2604,19 @@ function flushRenderPhaseStrictModeWarningsInDEV() {
function stopFinishedWorkLoopTimer() {
const didCompleteRoot = true;
stopWorkLoopTimer(interruptedBy, didCompleteRoot);
if (enableRootEventMarks) {
renderStopped(didCompleteRoot);
}
interruptedBy = null;
}

function stopInterruptedWorkLoopTimer() {
// TODO: Track which fiber caused the interruption.
const didCompleteRoot = false;
stopWorkLoopTimer(interruptedBy, didCompleteRoot);
if (enableRootEventMarks) {
renderStopped(didCompleteRoot);
}
interruptedBy = null;
}

Expand All @@ -2565,6 +2630,11 @@ function checkForInterruption(
updateExpirationTime > renderExpirationTime
) {
interruptedBy = fiberThatReceivedUpdate;

// TODO (brian) Is this accurate? Should we compare WIP root to fiber's root?
if (enableRootEventMarks) {
renderAbandoned();
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions packages/scheduler/src/RootEventsProfiling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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.
*
* @flow
*/

import {enableRootEventMarks} from 'shared/ReactFeatureFlags';

const supportsUserTiming =
typeof performance !== 'undefined' &&
typeof performance.mark === 'function' &&
typeof performance.clearMarks === 'function' &&
typeof performance.measure === 'function' &&
typeof performance.clearMeasures === 'function';

export type PriorityLabel = 'high' | 'normal' | 'low';
export type WorkType = 'render' | 'state-update';

export function schedulerStarted(priorityLabel: PriorityLabel): void {
if (enableRootEventMarks) {
if (supportsUserTiming) {
performance.mark(`--scheduler-start-${priorityLabel}`);
}
}
}

export function schedulerStopped(priorityLabel: PriorityLabel): void {
if (enableRootEventMarks) {
if (supportsUserTiming) {
performance.mark(`--scheduler-stop-${priorityLabel}`);
}
}
}
4 changes: 2 additions & 2 deletions packages/scheduler/src/Scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function handleTimeout(currentTime) {

function flushWork(hasTimeRemaining, initialTime) {
if (enableProfiling) {
markSchedulerUnsuspended(initialTime);
markSchedulerUnsuspended(initialTime, currentPriorityLevel);
}

// We'll need a host callback the next time work is scheduled.
Expand Down Expand Up @@ -156,7 +156,7 @@ function flushWork(hasTimeRemaining, initialTime) {
isPerformingWork = false;
if (enableProfiling) {
const currentTime = getCurrentTime();
markSchedulerSuspended(currentTime);
markSchedulerSuspended(currentTime, currentPriorityLevel);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/scheduler/src/SchedulerFeatureFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
export const enableSchedulerDebugging = false;
export const enableIsInputPending = false;
export const enableProfiling = __PROFILE__;
export const enableRootEventMarks = __PROFILE__;