+
Skip to content

[16.8.6] Aggressive cleanup to assist garbage collection #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: 16.8.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/react-dom/src/client/ReactDOMComponentTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import {HostComponent, HostText} from 'shared/ReactWorkTags';
import invariant from 'shared/invariant';

const randomKey = Math.random()
.toString(36)
.slice(2);
const internalInstanceKey = '__reactInternalInstance$' + randomKey;
const internalEventHandlersKey = '__reactEventHandlers$' + randomKey;
// const randomKey = Math.random()
// .toString(36)
// .slice(2);
const internalInstanceKey = '__reactInternalInstance$';
const internalEventHandlersKey = '__reactEventHandlers$';

export function precacheFiberNode(hostInst, node) {
node[internalInstanceKey] = hostInst;
Expand Down
33 changes: 22 additions & 11 deletions packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,11 +724,20 @@ function commitUnmount(current: Fiber): void {
if (typeof instance.componentWillUnmount === 'function') {
safelyCallComponentWillUnmount(current, instance);
}
return;
break;
}
case HostComponent: {
safelyDetachRef(current);
return;
// HACK: detach fiber references from DOM
current.stateNode.__reactEventHandlers$ = null;
current.stateNode.__reactInternalInstance$ = null;
break;
}
case HostText: {
// HACK: detach fiber references from DOM
current.stateNode.__reactEventHandlers$ = null;
current.stateNode.__reactInternalInstance$ = null;
break;
}
case HostPortal: {
// TODO: this is recursive.
Expand All @@ -739,9 +748,15 @@ function commitUnmount(current: Fiber): void {
} else if (supportsPersistence) {
emptyPortalContainer(current);
}
return;
break;
}
}

// Remove reference for GC
current.stateNode = null;
if (current.alternate != null) {
current.alternate.stateNode = null;
}
}

function commitNestedUnmounts(root: Fiber): void {
Expand Down Expand Up @@ -1038,19 +1053,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 (
Expand Down
15 changes: 14 additions & 1 deletion packages/react-reconciler/src/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,10 @@ function commitPassiveEffects(root: FiberRoot, firstEffect: Fiber): void {
captureCommitPhaseError(effect, error);
}
}
effect = effect.nextEffect;
const nextNextEffect = effect.nextEffect;
// Remove nextEffect pointer to assist GC
effect.nextEffect = null;
effect = nextNextEffect;
} while (effect !== null);
if (__DEV__) {
resetCurrentFiber();
Expand Down Expand Up @@ -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;
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载