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

[Fiber] Profiler - Use two separate functions instead of branch by flag #30957

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};
Loading