Repository: facebook/react
Pull request: [Fiber] Profiler - Use two separate functions instead of branch by flag

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.

Changed files:
filename: packages/react-reconciler/src/ReactFiberWorkLoop.js
patch: @@ -225,7 +225,8 @@ import {
   recordCommitTime,
   resetNestedUpdateFlag,
   startProfilerTimer,
-  stopProfilerTimerIfRunningAndRecordDelta,
+  stopProfilerTimerIfRunningAndRecordDuration,
+  stopProfilerTimerIfRunningAndRecordIncompleteDuration,
   syncNestedUpdateFlag,
 } from './ReactProfilerTimer';
 
@@ -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) {
@@ -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(
@@ -2660,7 +2661,7 @@ function replayBeginWork(unitOfWork: Fiber): null | Fiber {
     }
   }
   if (isProfilingMode) {
-    stopProfilerTimerIfRunningAndRecordDelta(unitOfWork, true);
+    stopProfilerTimerIfRunningAndRecordDuration(unitOfWork);
   }
 
   return next;
@@ -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) {
@@ -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;

filename: packages/react-reconciler/src/ReactProfilerTimer.js
patch: @@ -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,
   ...
 };
@@ -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;
@@ -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;
   }
 }
@@ -233,7 +244,8 @@ export {
   startPassiveEffectTimer,
   startProfilerTimer,
   stopProfilerTimerIfRunning,
-  stopProfilerTimerIfRunningAndRecordDelta,
+  stopProfilerTimerIfRunningAndRecordDuration,
+  stopProfilerTimerIfRunningAndRecordIncompleteDuration,
   syncNestedUpdateFlag,
   transferActualDuration,
 };