Skip to content

Commit 3d95c43

Browse files
authored
[Fiber] Profiler - Use two separate functions instead of branch by flag (#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.
1 parent 6774caa commit 3d95c43

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

packages/react-reconciler/src/ReactFiberWorkLoop.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ import {
225225
recordCommitTime,
226226
resetNestedUpdateFlag,
227227
startProfilerTimer,
228-
stopProfilerTimerIfRunningAndRecordDelta,
228+
stopProfilerTimerIfRunningAndRecordDuration,
229+
stopProfilerTimerIfRunningAndRecordIncompleteDuration,
229230
syncNestedUpdateFlag,
230231
} from './ReactProfilerTimer';
231232

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

18501851
if (enableSchedulingProfiler) {
@@ -2516,7 +2517,7 @@ function performUnitOfWork(unitOfWork: Fiber): void {
25162517
} else {
25172518
next = beginWork(current, unitOfWork, entangledRenderLanes);
25182519
}
2519-
stopProfilerTimerIfRunningAndRecordDelta(unitOfWork, true);
2520+
stopProfilerTimerIfRunningAndRecordDuration(unitOfWork);
25202521
} else {
25212522
if (__DEV__) {
25222523
next = runWithFiberInDEV(
@@ -2660,7 +2661,7 @@ function replayBeginWork(unitOfWork: Fiber): null | Fiber {
26602661
}
26612662
}
26622663
if (isProfilingMode) {
2663-
stopProfilerTimerIfRunningAndRecordDelta(unitOfWork, true);
2664+
stopProfilerTimerIfRunningAndRecordDuration(unitOfWork);
26642665
}
26652666

26662667
return next;
@@ -2851,7 +2852,7 @@ function completeUnitOfWork(unitOfWork: Fiber): void {
28512852
next = completeWork(current, completedWork, entangledRenderLanes);
28522853
}
28532854
// Update render duration assuming we didn't error.
2854-
stopProfilerTimerIfRunningAndRecordDelta(completedWork, false);
2855+
stopProfilerTimerIfRunningAndRecordIncompleteDuration(completedWork);
28552856
}
28562857

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

29102911
if (enableProfilerTimer && (incompleteWork.mode & ProfileMode) !== NoMode) {
29112912
// Record the render duration for the fiber that errored.
2912-
stopProfilerTimerIfRunningAndRecordDelta(incompleteWork, false);
2913+
stopProfilerTimerIfRunningAndRecordIncompleteDuration(incompleteWork);
29132914

29142915
// Include the time spent working on failed children before continuing.
29152916
let actualDuration = incompleteWork.actualDuration;

packages/react-reconciler/src/ReactProfilerTimer.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export type ProfilerTimer = {
2929
recordCommitTime(): void,
3030
startProfilerTimer(fiber: Fiber): void,
3131
stopProfilerTimerIfRunning(fiber: Fiber): void,
32-
stopProfilerTimerIfRunningAndRecordDelta(fiber: Fiber): void,
32+
stopProfilerTimerIfRunningAndRecordDuration(fiber: Fiber): void,
33+
stopProfilerTimerIfRunningAndRecordIncompleteDuration(fiber: Fiber): void,
3334
syncNestedUpdateFlag(): void,
3435
...
3536
};
@@ -112,9 +113,21 @@ function stopProfilerTimerIfRunning(fiber: Fiber): void {
112113
profilerStartTime = -1;
113114
}
114115

115-
function stopProfilerTimerIfRunningAndRecordDelta(
116+
function stopProfilerTimerIfRunningAndRecordDuration(fiber: Fiber): void {
117+
if (!enableProfilerTimer) {
118+
return;
119+
}
120+
121+
if (profilerStartTime >= 0) {
122+
const elapsedTime = now() - profilerStartTime;
123+
fiber.actualDuration += elapsedTime;
124+
fiber.selfBaseDuration = elapsedTime;
125+
profilerStartTime = -1;
126+
}
127+
}
128+
129+
function stopProfilerTimerIfRunningAndRecordIncompleteDuration(
116130
fiber: Fiber,
117-
overrideBaseTime: boolean,
118131
): void {
119132
if (!enableProfilerTimer) {
120133
return;
@@ -123,9 +136,7 @@ function stopProfilerTimerIfRunningAndRecordDelta(
123136
if (profilerStartTime >= 0) {
124137
const elapsedTime = now() - profilerStartTime;
125138
fiber.actualDuration += elapsedTime;
126-
if (overrideBaseTime) {
127-
fiber.selfBaseDuration = elapsedTime;
128-
}
139+
// We don't update the selfBaseDuration here because we errored.
129140
profilerStartTime = -1;
130141
}
131142
}
@@ -233,7 +244,8 @@ export {
233244
startPassiveEffectTimer,
234245
startProfilerTimer,
235246
stopProfilerTimerIfRunning,
236-
stopProfilerTimerIfRunningAndRecordDelta,
247+
stopProfilerTimerIfRunningAndRecordDuration,
248+
stopProfilerTimerIfRunningAndRecordIncompleteDuration,
237249
syncNestedUpdateFlag,
238250
transferActualDuration,
239251
};

0 commit comments

Comments
 (0)