Skip to content

Commit

Permalink
[Fiber] Replace setCurrentDebugFiberInDEV with runWithFiberInDEV (#29221
Browse files Browse the repository at this point in the history
)

Stacked on #29044.

To work with `console.createTask(...).run(...)` we need to be able to
run a function in the scope of the task.

The main concern with this, other than general performance, is that it
might add more stack frames on very deep stacks that hit the stack
limit. Such as with the commit phase where we recursively go down the
tree. These callbacks aren't really necessary in the recursive part but
only in the shallow invocation of the commit phase for each tag. So we
could refactor the commit phase so that only the shallow part at each
level is covered this way.
  • Loading branch information
sebmarkbage authored May 25, 2024
1 parent d6cfa0f commit b078c81
Show file tree
Hide file tree
Showing 7 changed files with 342 additions and 223 deletions.
44 changes: 21 additions & 23 deletions packages/react-reconciler/src/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ import {createThenableState, trackUsedThenable} from './ReactFiberThenable';
import {readContextDuringReconciliation} from './ReactFiberNewContext';
import {callLazyInitInDEV} from './ReactFiberCallUserSpace';

import {
getCurrentFiber as getCurrentDebugFiberInDEV,
setCurrentFiber as setCurrentDebugFiberInDEV,
} from './ReactCurrentFiber';
import {runWithFiberInDEV} from './ReactCurrentFiber';

// This tracks the thenables that are unwrapped during reconcilation.
let thenableState: ThenableState | null = null;
Expand Down Expand Up @@ -182,15 +179,14 @@ if (__DEV__) {
const fiber = createFiberFromElement((child: any), returnFiber.mode, 0);
fiber.return = returnFiber;

const prevDebugFiber = getCurrentDebugFiberInDEV();
setCurrentDebugFiberInDEV(fiber);
console.error(
'Each child in a list should have a unique "key" prop.' +
'%s%s See https://react.dev/link/warning-keys for more information.',
currentComponentErrorInfo,
childOwnerAppendix,
);
setCurrentDebugFiberInDEV(prevDebugFiber);
runWithFiberInDEV(fiber, () => {
console.error(
'Each child in a list should have a unique "key" prop.' +
'%s%s See https://react.dev/link/warning-keys for more information.',
currentComponentErrorInfo,
childOwnerAppendix,
);
});
};
}

Expand All @@ -213,14 +209,17 @@ function validateFragmentProps(
fiber = createFiberFromElement(element, returnFiber.mode, 0);
fiber.return = returnFiber;
}
const prevDebugFiber = getCurrentDebugFiberInDEV();
setCurrentDebugFiberInDEV(fiber);
console.error(
'Invalid prop `%s` supplied to `React.Fragment`. ' +
'React.Fragment can only have `key` and `children` props.',
runWithFiberInDEV(
fiber,
erroredKey => {
console.error(
'Invalid prop `%s` supplied to `React.Fragment`. ' +
'React.Fragment can only have `key` and `children` props.',
erroredKey,
);
},
key,
);
setCurrentDebugFiberInDEV(prevDebugFiber);
break;
}
}
Expand All @@ -232,10 +231,9 @@ function validateFragmentProps(
fiber = createFiberFromElement(element, returnFiber.mode, 0);
fiber.return = returnFiber;
}
const prevDebugFiber = getCurrentDebugFiberInDEV();
setCurrentDebugFiberInDEV(fiber);
console.error('Invalid attribute `ref` supplied to `React.Fragment`.');
setCurrentDebugFiberInDEV(prevDebugFiber);
runWithFiberInDEV(fiber, () => {
console.error('Invalid attribute `ref` supplied to `React.Fragment`.');
});
}
}
}
Expand Down
34 changes: 20 additions & 14 deletions packages/react-reconciler/src/ReactCurrentFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,29 @@ function getCurrentFiberStackInDev(): string {
return '';
}

export function resetCurrentDebugFiberInDEV() {
if (__DEV__) {
resetCurrentFiber();
}
}

export function setCurrentDebugFiberInDEV(fiber: Fiber | null) {
export function runWithFiberInDEV<A0, A1, A2, A3, A4, T>(
fiber: null | Fiber,
callback: (A0, A1, A2, A3, A4) => T,
arg0: A0,
arg1: A1,
arg2: A2,
arg3: A3,
arg4: A4,
): T {
if (__DEV__) {
const previousFiber = current;
setCurrentFiber(fiber);
try {
return callback(arg0, arg1, arg2, arg3, arg4);
} finally {
current = previousFiber;
}
}
// These errors should never make it into a build so we don't need to encode them in codes.json
// eslint-disable-next-line react-internal/prod-error-codes
throw new Error(
'runWithFiberInDEV should never be called in production. This is a bug in React.',
);
}

export function resetCurrentFiber() {
Expand All @@ -90,13 +103,6 @@ export function setCurrentFiber(fiber: Fiber | null) {
current = fiber;
}

export function getCurrentFiber(): Fiber | null {
if (__DEV__) {
return current;
}
return null;
}

export function setIsRendering(rendering: boolean) {
if (__DEV__) {
isRendering = rendering;
Expand Down
Loading

0 comments on commit b078c81

Please sign in to comment.