From 11bf3268e10f15395a14e69771b369f4a9c90a57 Mon Sep 17 00:00:00 2001 From: paulshen Date: Wed, 10 Apr 2019 17:24:07 -0700 Subject: [PATCH] https://github.com/facebook/react/pull/15157 --- .../src/ReactFiberCommitWork.js | 18 ++++++++++-------- .../src/ReactFiberScheduler.js | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/packages/react-reconciler/src/ReactFiberCommitWork.js b/packages/react-reconciler/src/ReactFiberCommitWork.js index b1a0293a6c6e..36ce3db5e08c 100644 --- a/packages/react-reconciler/src/ReactFiberCommitWork.js +++ b/packages/react-reconciler/src/ReactFiberCommitWork.js @@ -742,6 +742,12 @@ function commitUnmount(current: Fiber): void { return; } } + + // Remove reference for GC + current.stateNode = null; + if (current.alternate != null) { + current.alternate.stateNode = null; + } } function commitNestedUnmounts(root: Fiber): void { @@ -1038,19 +1044,15 @@ function unmountHostComponents(current): void { } if (node.tag === HostComponent || node.tag === HostText) { + // Save stateNode reference so commitUnmount can clear it. + const stateNode: Instance | TextInstance = node.stateNode; commitNestedUnmounts(node); // After all the children have unmounted, it is now safe to remove the // node from the tree. if (currentParentIsContainer) { - removeChildFromContainer( - ((currentParent: any): Container), - (node.stateNode: Instance | TextInstance), - ); + removeChildFromContainer(((currentParent: any): Container), stateNode); } else { - removeChild( - ((currentParent: any): Instance), - (node.stateNode: Instance | TextInstance), - ); + removeChild(((currentParent: any): Instance), stateNode); } // Don't visit children because we already visited them. } else if ( diff --git a/packages/react-reconciler/src/ReactFiberScheduler.js b/packages/react-reconciler/src/ReactFiberScheduler.js index 9fe67f90aa7f..d23ccd45cfbd 100644 --- a/packages/react-reconciler/src/ReactFiberScheduler.js +++ b/packages/react-reconciler/src/ReactFiberScheduler.js @@ -696,7 +696,10 @@ function commitRoot(root: FiberRoot, finishedWork: Fiber): void { captureCommitPhaseError(nextEffect, error); // Clean-up if (nextEffect !== null) { - nextEffect = nextEffect.nextEffect; + const nextNextEffect = nextEffect.nextEffect; + // Remove nextEffect pointer to assist GC + nextEffect.nextEffect = null; + nextEffect = nextNextEffect; } } } @@ -811,6 +814,16 @@ function commitRoot(root: FiberRoot, finishedWork: Fiber): void { return schedulePassiveEffects(callback); }); passiveEffectCallback = callback; + } else { + // We are done with the effect chain at this point so let's clear the + // nextEffect pointers to assist with GC. If we have passive effects, we'll + // clear this in commitPassiveEffects. + nextEffect = firstEffect; + while (nextEffect !== null) { + const nextNextEffect = nextEffect.nextEffect; + nextEffect.nextEffect = null; + nextEffect = nextNextEffect; + } } isCommitting = false;