Skip to content

Commit

Permalink
[Fiber] Profiler - Use two separate functions instead of branch by fl…
Browse files Browse the repository at this point in the history
…ag (#30957)

Nit: I don't trust flags in hot code. While it can take somewhat longer
to compile two functions and JIT them. After that they don't need to
check branches. Also makes it clearer the purpose.
  • Loading branch information
sebmarkbage authored Sep 14, 2024
1 parent 6774caa commit 3d95c43
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
13 changes: 7 additions & 6 deletions packages/react-reconciler/src/ReactFiberWorkLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ import {
recordCommitTime,
resetNestedUpdateFlag,
startProfilerTimer,
stopProfilerTimerIfRunningAndRecordDelta,
stopProfilerTimerIfRunningAndRecordDuration,
stopProfilerTimerIfRunningAndRecordIncompleteDuration,
syncNestedUpdateFlag,
} from './ReactProfilerTimer';

Expand Down Expand Up @@ -1844,7 +1845,7 @@ function handleThrow(root: FiberRoot, thrownValue: any): void {
// Record the time spent rendering before an error was thrown. This
// avoids inaccurate Profiler durations in the case of a
// suspended render.
stopProfilerTimerIfRunningAndRecordDelta(erroredWork, true);
stopProfilerTimerIfRunningAndRecordDuration(erroredWork);
}

if (enableSchedulingProfiler) {
Expand Down Expand Up @@ -2516,7 +2517,7 @@ function performUnitOfWork(unitOfWork: Fiber): void {
} else {
next = beginWork(current, unitOfWork, entangledRenderLanes);
}
stopProfilerTimerIfRunningAndRecordDelta(unitOfWork, true);
stopProfilerTimerIfRunningAndRecordDuration(unitOfWork);
} else {
if (__DEV__) {
next = runWithFiberInDEV(
Expand Down Expand Up @@ -2660,7 +2661,7 @@ function replayBeginWork(unitOfWork: Fiber): null | Fiber {
}
}
if (isProfilingMode) {
stopProfilerTimerIfRunningAndRecordDelta(unitOfWork, true);
stopProfilerTimerIfRunningAndRecordDuration(unitOfWork);
}

return next;
Expand Down Expand Up @@ -2851,7 +2852,7 @@ function completeUnitOfWork(unitOfWork: Fiber): void {
next = completeWork(current, completedWork, entangledRenderLanes);
}
// Update render duration assuming we didn't error.
stopProfilerTimerIfRunningAndRecordDelta(completedWork, false);
stopProfilerTimerIfRunningAndRecordIncompleteDuration(completedWork);
}

if (next !== null) {
Expand Down Expand Up @@ -2909,7 +2910,7 @@ function unwindUnitOfWork(unitOfWork: Fiber, skipSiblings: boolean): void {

if (enableProfilerTimer && (incompleteWork.mode & ProfileMode) !== NoMode) {
// Record the render duration for the fiber that errored.
stopProfilerTimerIfRunningAndRecordDelta(incompleteWork, false);
stopProfilerTimerIfRunningAndRecordIncompleteDuration(incompleteWork);

// Include the time spent working on failed children before continuing.
let actualDuration = incompleteWork.actualDuration;
Expand Down
26 changes: 19 additions & 7 deletions packages/react-reconciler/src/ReactProfilerTimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export type ProfilerTimer = {
recordCommitTime(): void,
startProfilerTimer(fiber: Fiber): void,
stopProfilerTimerIfRunning(fiber: Fiber): void,
stopProfilerTimerIfRunningAndRecordDelta(fiber: Fiber): void,
stopProfilerTimerIfRunningAndRecordDuration(fiber: Fiber): void,
stopProfilerTimerIfRunningAndRecordIncompleteDuration(fiber: Fiber): void,
syncNestedUpdateFlag(): void,
...
};
Expand Down Expand Up @@ -112,9 +113,21 @@ function stopProfilerTimerIfRunning(fiber: Fiber): void {
profilerStartTime = -1;
}

function stopProfilerTimerIfRunningAndRecordDelta(
function stopProfilerTimerIfRunningAndRecordDuration(fiber: Fiber): void {
if (!enableProfilerTimer) {
return;
}

if (profilerStartTime >= 0) {
const elapsedTime = now() - profilerStartTime;
fiber.actualDuration += elapsedTime;
fiber.selfBaseDuration = elapsedTime;
profilerStartTime = -1;
}
}

function stopProfilerTimerIfRunningAndRecordIncompleteDuration(
fiber: Fiber,
overrideBaseTime: boolean,
): void {
if (!enableProfilerTimer) {
return;
Expand All @@ -123,9 +136,7 @@ function stopProfilerTimerIfRunningAndRecordDelta(
if (profilerStartTime >= 0) {
const elapsedTime = now() - profilerStartTime;
fiber.actualDuration += elapsedTime;
if (overrideBaseTime) {
fiber.selfBaseDuration = elapsedTime;
}
// We don't update the selfBaseDuration here because we errored.
profilerStartTime = -1;
}
}
Expand Down Expand Up @@ -233,7 +244,8 @@ export {
startPassiveEffectTimer,
startProfilerTimer,
stopProfilerTimerIfRunning,
stopProfilerTimerIfRunningAndRecordDelta,
stopProfilerTimerIfRunningAndRecordDuration,
stopProfilerTimerIfRunningAndRecordIncompleteDuration,
syncNestedUpdateFlag,
transferActualDuration,
};

0 comments on commit 3d95c43

Please sign in to comment.