@samitouri / QOS-React-2 / commits / bb1357b381

Wrap try-catch directly around each user function

(This is the same as f9e6aef, but for the passive mount phase rather than the mutation phase.) This moves the try-catch from around each fiber's passive mount phase to direclty around each user function (effect function, callback, etc). We already do this when unmounting because if one unmount function errors, we still need to call all the others so they can clean up their resources. Previously we didn't bother to do this for anything but unmount, because if a mount effect throws, we're going to delete that whole tree anyway. But now that we're switching from an iterative loop to a recursive one, we don't want every call frame on the stack to have a try-catch, since the error handling requires additional memory. Wrapping every user function is a bit tedious, but it's better for performance. Many of them already had try blocks around them already.

Andrew Clark committed Jul 12, 2022 at 14:45 UTC bb1357b38170e81cfbf2398cc6fefcf7a1ef2ccb
2 files changed +34 -26
packages/react-reconciler/src/ReactFiberCommitWork.new.js
+17 -13
@@ -2885,16 +2885,12 @@ function commitPassiveMountEffects_complete(
2885 const fiber = nextEffect;
2886
2887 setCurrentDebugFiberInDEV(fiber);
2888 - try {
2889 - commitPassiveMountOnFiber(
2890 - root,
2891 - fiber,
2892 - committedLanes,
2893 - committedTransitions,
2894 - );
2895 - } catch (error) {
2896 - captureCommitPhaseError(fiber, fiber.return, error);
2897 - }
2888 + commitPassiveMountOnFiber(
2889 + root,
2890 + fiber,
2891 + committedLanes,
2892 + committedTransitions,
2893 + );
2894 resetCurrentDebugFiberInDEV();
2895
2896 if (fiber === subtreeRoot) {
@@ -2936,11 +2932,19 @@ function commitPassiveMountOnFiber(
2932 HookPassive | HookHasEffect,
2933 finishedWork,
2934 );
2939 - } finally {
2940 - recordPassiveEffectDuration(finishedWork);
2935 + } catch (error) {
2936 + captureCommitPhaseError(finishedWork, finishedWork.return, error);
2937 }
2938 + recordPassiveEffectDuration(finishedWork);
2939 } else {
2943 - commitHookEffectListMount(HookPassive | HookHasEffect, finishedWork);
2940 + try {
2941 + commitHookEffectListMount(
2942 + HookPassive | HookHasEffect,
2943 + finishedWork,
2944 + );
2945 + } catch (error) {
2946 + captureCommitPhaseError(finishedWork, finishedWork.return, error);
2947 + }
2948 }
2949 }
2950 break;
packages/react-reconciler/src/ReactFiberCommitWork.old.js
+17 -13
@@ -2885,16 +2885,12 @@ function commitPassiveMountEffects_complete(
2885 const fiber = nextEffect;
2886
2887 setCurrentDebugFiberInDEV(fiber);
2888 - try {
2889 - commitPassiveMountOnFiber(
2890 - root,
2891 - fiber,
2892 - committedLanes,
2893 - committedTransitions,
2894 - );
2895 - } catch (error) {
2896 - captureCommitPhaseError(fiber, fiber.return, error);
2897 - }
2888 + commitPassiveMountOnFiber(
2889 + root,
2890 + fiber,
2891 + committedLanes,
2892 + committedTransitions,
2893 + );
2894 resetCurrentDebugFiberInDEV();
2895
2896 if (fiber === subtreeRoot) {
@@ -2936,11 +2932,19 @@ function commitPassiveMountOnFiber(
2932 HookPassive | HookHasEffect,
2933 finishedWork,
2934 );
2939 - } finally {
2940 - recordPassiveEffectDuration(finishedWork);
2935 + } catch (error) {
2936 + captureCommitPhaseError(finishedWork, finishedWork.return, error);
2937 }
2938 + recordPassiveEffectDuration(finishedWork);
2939 } else {
2943 - commitHookEffectListMount(HookPassive | HookHasEffect, finishedWork);
2940 + try {
2941 + commitHookEffectListMount(
2942 + HookPassive | HookHasEffect,
2943 + finishedWork,
2944 + );
2945 + } catch (error) {
2946 + captureCommitPhaseError(finishedWork, finishedWork.return, error);
2947 + }
2948 }
2949 }
2950 break;