@samitouri / QOS-React-1 / commits / 94e4acaa14

[Fiber] Set profiler values to doubles (#30942)

At some point this trick was added to initialize the value first to NaN and then replace them with zeros and negative ones. This is to address the issue noted in https://github.com/facebook/react/issues/14365 where these hidden classes can be initialized to SMIs at first and then deopt when we realize they're actually doubles. However, this fix has been long broken and has deopted the profiling build for years because closure compiler optimizes out the first write. I'm not sure because I haven't A/B-tested this in the JIT yet but I think we can use negative zero and -1.1 as the initial values instead since they're not simple integers. Negative zero `===` zero (but not Object.is) so is the same as far as our code is concerned. The negative value is just `< 0` comparisons.

Sebastian Markbåge committed Sep 13, 2024 at 12:27 UTC 94e4acaa1477e65cac02ba86058cde0afe4c8f1f
2 files changed +17 -24
packages/react-reconciler/src/ReactFiber.js
+11 -18
@@ -187,18 +187,11 @@ function FiberNode(
187 // Learn more about this here:
188 // https://github.com/facebook/react/issues/14365
189 // https://bugs.chromium.org/p/v8/issues/detail?id=8538
190 - this.actualDuration = Number.NaN;
191 - this.actualStartTime = Number.NaN;
192 - this.selfBaseDuration = Number.NaN;
193 - this.treeBaseDuration = Number.NaN;
194 -
195 - // It's okay to replace the initial doubles with smis after initialization.
196 - // This won't trigger the performance cliff mentioned above,
197 - // and it simplifies other profiler code (including DevTools).
198 - this.actualDuration = 0;
199 - this.actualStartTime = -1;
200 - this.selfBaseDuration = 0;
201 - this.treeBaseDuration = 0;
190 +
191 + this.actualDuration = -0;
192 + this.actualStartTime = -1.1;
193 + this.selfBaseDuration = -0;
194 + this.treeBaseDuration = -0;
195 }
196
197 if (__DEV__) {
@@ -286,10 +279,10 @@ function createFiberImplObject(
279 };
280
281 if (enableProfilerTimer) {
289 - fiber.actualDuration = 0;
290 - fiber.actualStartTime = -1;
291 - fiber.selfBaseDuration = 0;
292 - fiber.treeBaseDuration = 0;
282 + fiber.actualDuration = -0;
283 + fiber.actualStartTime = -1.1;
284 + fiber.selfBaseDuration = -0;
285 + fiber.treeBaseDuration = -0;
286 }
287
288 if (__DEV__) {
@@ -382,8 +375,8 @@ export function createWorkInProgress(current: Fiber, pendingProps: any): Fiber {
375 // This prevents time from endlessly accumulating in new commits.
376 // This has the downside of resetting values for different priority renders,
377 // But works for yielding (the common case) and should support resuming.
385 - workInProgress.actualDuration = 0;
386 - workInProgress.actualStartTime = -1;
378 + workInProgress.actualDuration = -0;
379 + workInProgress.actualStartTime = -1.1;
380 }
381 }
382
packages/react-reconciler/src/ReactFiberBeginWork.js
+6 -6
@@ -2430,10 +2430,10 @@ function mountSuspenseFallbackChildren(
2430 // final amounts. This seems counterintuitive, since we're intentionally
2431 // not measuring part of the render phase, but this makes it match what we
2432 // do in Concurrent Mode.
2433 - primaryChildFragment.actualDuration = 0;
2434 - primaryChildFragment.actualStartTime = -1;
2435 - primaryChildFragment.selfBaseDuration = 0;
2436 - primaryChildFragment.treeBaseDuration = 0;
2433 + primaryChildFragment.actualDuration = -0;
2434 + primaryChildFragment.actualStartTime = -1.1;
2435 + primaryChildFragment.selfBaseDuration = -0;
2436 + primaryChildFragment.treeBaseDuration = -0;
2437 }
2438
2439 fallbackChildFragment = createFiberFromFragment(
@@ -2560,8 +2560,8 @@ function updateSuspenseFallbackChildren(
2560 // final amounts. This seems counterintuitive, since we're intentionally
2561 // not measuring part of the render phase, but this makes it match what we
2562 // do in Concurrent Mode.
2563 - primaryChildFragment.actualDuration = 0;
2564 - primaryChildFragment.actualStartTime = -1;
2563 + primaryChildFragment.actualDuration = -0;
2564 + primaryChildFragment.actualStartTime = -1.1;
2565 primaryChildFragment.selfBaseDuration =
2566 currentPrimaryChildFragment.selfBaseDuration;
2567 primaryChildFragment.treeBaseDuration =