Skip to content

Commit a41c35f

Browse files
committed
Unify startEffectTimer and recordEffectDuration
We infer if this was layout or passive duration when we propagate the number in the phase. We can also just use the same start timer as for render since they don't overlap.
1 parent 202fdec commit a41c35f

File tree

2 files changed

+30
-59
lines changed

2 files changed

+30
-59
lines changed

packages/react-reconciler/src/ReactFiberCommitEffects.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ import {NoFlags} from './ReactFiberFlags';
3131
import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber';
3232
import {resolveClassComponentProps} from './ReactFiberClassComponent';
3333
import {
34-
recordLayoutEffectDuration,
35-
startLayoutEffectTimer,
36-
recordPassiveEffectDuration,
37-
startPassiveEffectTimer,
34+
recordEffectDuration,
35+
startEffectTimer,
3836
isCurrentUpdateNested,
3937
} from './ReactProfilerTimer';
4038
import {NoMode, ProfileMode} from './ReactTypeOfMode';
@@ -91,9 +89,9 @@ export function commitHookLayoutEffects(
9189
// e.g. a destroy function in one component should never override a ref set
9290
// by a create function in another component during the same commit.
9391
if (shouldProfile(finishedWork)) {
94-
startLayoutEffectTimer();
92+
startEffectTimer();
9593
commitHookEffectListMount(hookFlags, finishedWork);
96-
recordLayoutEffectDuration(finishedWork);
94+
recordEffectDuration(finishedWork);
9795
} else {
9896
commitHookEffectListMount(hookFlags, finishedWork);
9997
}
@@ -110,13 +108,13 @@ export function commitHookLayoutUnmountEffects(
110108
// e.g. a destroy function in one component should never override a ref set
111109
// by a create function in another component during the same commit.
112110
if (shouldProfile(finishedWork)) {
113-
startLayoutEffectTimer();
111+
startEffectTimer();
114112
commitHookEffectListUnmount(
115113
hookFlags,
116114
finishedWork,
117115
nearestMountedAncestor,
118116
);
119-
recordLayoutEffectDuration(finishedWork);
117+
recordEffectDuration(finishedWork);
120118
} else {
121119
commitHookEffectListUnmount(
122120
hookFlags,
@@ -292,9 +290,9 @@ export function commitHookPassiveMountEffects(
292290
hookFlags: HookFlags,
293291
) {
294292
if (shouldProfile(finishedWork)) {
295-
startPassiveEffectTimer();
293+
startEffectTimer();
296294
commitHookEffectListMount(hookFlags, finishedWork);
297-
recordPassiveEffectDuration(finishedWork);
295+
recordEffectDuration(finishedWork);
298296
} else {
299297
commitHookEffectListMount(hookFlags, finishedWork);
300298
}
@@ -306,13 +304,13 @@ export function commitHookPassiveUnmountEffects(
306304
hookFlags: HookFlags,
307305
) {
308306
if (shouldProfile(finishedWork)) {
309-
startPassiveEffectTimer();
307+
startEffectTimer();
310308
commitHookEffectListUnmount(
311309
hookFlags,
312310
finishedWork,
313311
nearestMountedAncestor,
314312
);
315-
recordPassiveEffectDuration(finishedWork);
313+
recordEffectDuration(finishedWork);
316314
} else {
317315
commitHookEffectListUnmount(
318316
hookFlags,
@@ -360,7 +358,7 @@ export function commitClassLayoutLifecycles(
360358
}
361359
}
362360
if (shouldProfile(finishedWork)) {
363-
startLayoutEffectTimer();
361+
startEffectTimer();
364362
if (__DEV__) {
365363
runWithFiberInDEV(
366364
finishedWork,
@@ -375,7 +373,7 @@ export function commitClassLayoutLifecycles(
375373
captureCommitPhaseError(finishedWork, finishedWork.return, error);
376374
}
377375
}
378-
recordLayoutEffectDuration(finishedWork);
376+
recordEffectDuration(finishedWork);
379377
} else {
380378
if (__DEV__) {
381379
runWithFiberInDEV(
@@ -431,7 +429,7 @@ export function commitClassLayoutLifecycles(
431429
}
432430
}
433431
if (shouldProfile(finishedWork)) {
434-
startLayoutEffectTimer();
432+
startEffectTimer();
435433
if (__DEV__) {
436434
runWithFiberInDEV(
437435
finishedWork,
@@ -453,7 +451,7 @@ export function commitClassLayoutLifecycles(
453451
captureCommitPhaseError(finishedWork, finishedWork.return, error);
454452
}
455453
}
456-
recordLayoutEffectDuration(finishedWork);
454+
recordEffectDuration(finishedWork);
457455
} else {
458456
if (__DEV__) {
459457
runWithFiberInDEV(
@@ -706,7 +704,7 @@ export function safelyCallComponentWillUnmount(
706704
);
707705
instance.state = current.memoizedState;
708706
if (shouldProfile(current)) {
709-
startLayoutEffectTimer();
707+
startEffectTimer();
710708
if (__DEV__) {
711709
runWithFiberInDEV(
712710
current,
@@ -722,7 +720,7 @@ export function safelyCallComponentWillUnmount(
722720
captureCommitPhaseError(current, nearestMountedAncestor, error);
723721
}
724722
}
725-
recordLayoutEffectDuration(current);
723+
recordEffectDuration(current);
726724
} else {
727725
if (__DEV__) {
728726
runWithFiberInDEV(
@@ -763,10 +761,10 @@ function commitAttachRef(finishedWork: Fiber) {
763761
if (typeof ref === 'function') {
764762
if (shouldProfile(finishedWork)) {
765763
try {
766-
startLayoutEffectTimer();
764+
startEffectTimer();
767765
finishedWork.refCleanup = ref(instanceToUse);
768766
} finally {
769-
recordLayoutEffectDuration(finishedWork);
767+
recordEffectDuration(finishedWork);
770768
}
771769
} else {
772770
finishedWork.refCleanup = ref(instanceToUse);
@@ -820,14 +818,14 @@ export function safelyDetachRef(
820818
try {
821819
if (shouldProfile(current)) {
822820
try {
823-
startLayoutEffectTimer();
821+
startEffectTimer();
824822
if (__DEV__) {
825823
runWithFiberInDEV(current, refCleanup);
826824
} else {
827825
refCleanup();
828826
}
829827
} finally {
830-
recordLayoutEffectDuration(current);
828+
recordEffectDuration(current);
831829
}
832830
} else {
833831
if (__DEV__) {
@@ -850,14 +848,14 @@ export function safelyDetachRef(
850848
try {
851849
if (shouldProfile(current)) {
852850
try {
853-
startLayoutEffectTimer();
851+
startEffectTimer();
854852
if (__DEV__) {
855853
(runWithFiberInDEV(current, ref, null): void);
856854
} else {
857855
ref(null);
858856
}
859857
} finally {
860-
recordLayoutEffectDuration(current);
858+
recordEffectDuration(current);
861859
}
862860
} else {
863861
if (__DEV__) {

packages/react-reconciler/src/ReactProfilerTimer.js

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ export type ProfilerTimer = {
3737
let completeTime: number = -0;
3838
let commitTime: number = -0;
3939
let profilerStartTime: number = -1.1;
40-
let layoutEffectStartTime: number = -1.1;
41-
let passiveEffectStartTime: number = -1.1;
4240
let profilerEffectDuration: number = -0;
4341

4442
function pushNestedEffectDurations(): number {
@@ -181,50 +179,27 @@ function stopProfilerTimerIfRunningAndRecordIncompleteDuration(
181179
}
182180
}
183181

184-
function recordLayoutEffectDuration(fiber: Fiber): void {
182+
function recordEffectDuration(fiber: Fiber): void {
185183
if (!enableProfilerTimer || !enableProfilerCommitHooks) {
186184
return;
187185
}
188186

189-
if (layoutEffectStartTime >= 0) {
190-
const elapsedTime = now() - layoutEffectStartTime;
191-
192-
layoutEffectStartTime = -1;
193-
194-
// Store duration on the next nearest Profiler ancestor
195-
// Or the root (for the DevTools Profiler to read)
196-
profilerEffectDuration += elapsedTime;
197-
}
198-
}
199-
200-
function recordPassiveEffectDuration(fiber: Fiber): void {
201-
if (!enableProfilerTimer || !enableProfilerCommitHooks) {
202-
return;
203-
}
204-
205-
if (passiveEffectStartTime >= 0) {
206-
const elapsedTime = now() - passiveEffectStartTime;
187+
if (profilerStartTime >= 0) {
188+
const elapsedTime = now() - profilerStartTime;
207189

208-
passiveEffectStartTime = -1;
190+
profilerStartTime = -1;
209191

210192
// Store duration on the next nearest Profiler ancestor
211193
// Or the root (for the DevTools Profiler to read)
212194
profilerEffectDuration += elapsedTime;
213195
}
214196
}
215197

216-
function startLayoutEffectTimer(): void {
217-
if (!enableProfilerTimer || !enableProfilerCommitHooks) {
218-
return;
219-
}
220-
layoutEffectStartTime = now();
221-
}
222-
223-
function startPassiveEffectTimer(): void {
198+
function startEffectTimer(): void {
224199
if (!enableProfilerTimer || !enableProfilerCommitHooks) {
225200
return;
226201
}
227-
passiveEffectStartTime = now();
202+
profilerStartTime = now();
228203
}
229204

230205
function transferActualDuration(fiber: Fiber): void {
@@ -246,11 +221,9 @@ export {
246221
recordCommitTime,
247222
isCurrentUpdateNested,
248223
markNestedUpdateScheduled,
249-
recordLayoutEffectDuration,
250-
recordPassiveEffectDuration,
224+
recordEffectDuration,
251225
resetNestedUpdateFlag,
252-
startLayoutEffectTimer,
253-
startPassiveEffectTimer,
226+
startEffectTimer,
254227
startProfilerTimer,
255228
stopProfilerTimerIfRunning,
256229
stopProfilerTimerIfRunningAndRecordDuration,

0 commit comments

Comments
 (0)