[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.
Sebastian Markbåge committed
Sep 13, 2024 at 21:51 UTC
3d95c43b8967d4dda1ec9a22f0d9ea4999fee8b8
2 files changed
+26
-13
packages/react-reconciler/src/ReactFiberWorkLoop.js
+7
-6
@@ -225,7 +225,8 @@ import {
225
recordCommitTime,
226
resetNestedUpdateFlag,
227
startProfilerTimer,
228
- stopProfilerTimerIfRunningAndRecordDelta,
228
+ stopProfilerTimerIfRunningAndRecordDuration,
229
+ stopProfilerTimerIfRunningAndRecordIncompleteDuration,
230
syncNestedUpdateFlag,
231
} from './ReactProfilerTimer';
232
@@ -1844,7 +1845,7 @@ function handleThrow(root: FiberRoot, thrownValue: any): void {
1845
// Record the time spent rendering before an error was thrown. This
1846
// avoids inaccurate Profiler durations in the case of a
1847
// suspended render.
1847
- stopProfilerTimerIfRunningAndRecordDelta(erroredWork, true);
1848
+ stopProfilerTimerIfRunningAndRecordDuration(erroredWork);
1849
}
1850
1851
if (enableSchedulingProfiler) {
@@ -2516,7 +2517,7 @@ function performUnitOfWork(unitOfWork: Fiber): void {
2517
} else {
2518
next = beginWork(current, unitOfWork, entangledRenderLanes);
2519
}
2519
- stopProfilerTimerIfRunningAndRecordDelta(unitOfWork, true);
2520
+ stopProfilerTimerIfRunningAndRecordDuration(unitOfWork);
2521
} else {
2522
if (__DEV__) {
2523
next = runWithFiberInDEV(
@@ -2660,7 +2661,7 @@ function replayBeginWork(unitOfWork: Fiber): null | Fiber {
2661
}
2662
}
2663
if (isProfilingMode) {
2663
- stopProfilerTimerIfRunningAndRecordDelta(unitOfWork, true);
2664
+ stopProfilerTimerIfRunningAndRecordDuration(unitOfWork);
2665
}
2666
2667
return next;
@@ -2851,7 +2852,7 @@ function completeUnitOfWork(unitOfWork: Fiber): void {
2852
next = completeWork(current, completedWork, entangledRenderLanes);
2853
}
2854
// Update render duration assuming we didn't error.
2854
- stopProfilerTimerIfRunningAndRecordDelta(completedWork, false);
2855
+ stopProfilerTimerIfRunningAndRecordIncompleteDuration(completedWork);
2856
}
2857
2858
if (next !== null) {
@@ -2909,7 +2910,7 @@ function unwindUnitOfWork(unitOfWork: Fiber, skipSiblings: boolean): void {
2910
2911
if (enableProfilerTimer && (incompleteWork.mode & ProfileMode) !== NoMode) {
2912
// Record the render duration for the fiber that errored.
2912
- stopProfilerTimerIfRunningAndRecordDelta(incompleteWork, false);
2913
+ stopProfilerTimerIfRunningAndRecordIncompleteDuration(incompleteWork);
2914
2915
// Include the time spent working on failed children before continuing.
2916
let actualDuration = incompleteWork.actualDuration;
packages/react-reconciler/src/ReactProfilerTimer.js
+19
-7
@@ -29,7 +29,8 @@ export type ProfilerTimer = {
29
recordCommitTime(): void,
30
startProfilerTimer(fiber: Fiber): void,
31
stopProfilerTimerIfRunning(fiber: Fiber): void,
32
- stopProfilerTimerIfRunningAndRecordDelta(fiber: Fiber): void,
32
+ stopProfilerTimerIfRunningAndRecordDuration(fiber: Fiber): void,
33
+ stopProfilerTimerIfRunningAndRecordIncompleteDuration(fiber: Fiber): void,
34
syncNestedUpdateFlag(): void,
35
...
36
};
@@ -112,9 +113,21 @@ function stopProfilerTimerIfRunning(fiber: Fiber): void {
113
profilerStartTime = -1;
114
}
115
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(
130
fiber: Fiber,
117
- overrideBaseTime: boolean,
131
): void {
132
if (!enableProfilerTimer) {
133
return;
@@ -123,9 +136,7 @@ function stopProfilerTimerIfRunningAndRecordDelta(
136
if (profilerStartTime >= 0) {
137
const elapsedTime = now() - profilerStartTime;
138
fiber.actualDuration += elapsedTime;
126
- if (overrideBaseTime) {
127
- fiber.selfBaseDuration = elapsedTime;
128
- }
139
+ // We don't update the selfBaseDuration here because we errored.
140
profilerStartTime = -1;
141
}
142
}
@@ -233,7 +244,8 @@ export {
244
startPassiveEffectTimer,
245
startProfilerTimer,
246
stopProfilerTimerIfRunning,
236
- stopProfilerTimerIfRunningAndRecordDelta,
247
+ stopProfilerTimerIfRunningAndRecordDuration,
248
+ stopProfilerTimerIfRunningAndRecordIncompleteDuration,
249
syncNestedUpdateFlag,
250
transferActualDuration,
251
};