Skip to content
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

Fix GC memory leak #4510

Merged
merged 3 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 12 additions & 16 deletions packages/lexical/src/LexicalGC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function $garbageCollectDetachedDeepChildNodes(
let child = node.getFirstChild();

while (child !== null) {
const nextChild = child.getNextSibling();
const childKey = child.__key;
if (child !== undefined && child.__parent === parentKey) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to PR changes, but wondering why would we have undefined check here, it would throw one line above when accessing __key, and we have null check in while loop

if ($isElementNode(child)) {
Expand All @@ -66,7 +67,7 @@ function $garbageCollectDetachedDeepChildNodes(
}
nodeMap.delete(childKey);
}
child = child.isAttached() ? child.getNextSibling() : null;
child = nextChild;
}
}

Expand All @@ -79,21 +80,8 @@ export function $garbageCollectDetachedNodes(
const prevNodeMap = prevEditorState._nodeMap;
const nodeMap = editorState._nodeMap;

for (const nodeKey of dirtyLeaves) {
const node = nodeMap.get(nodeKey);

if (node !== undefined && !node.isAttached()) {
if (!prevNodeMap.has(nodeKey)) {
dirtyLeaves.delete(nodeKey);
}

nodeMap.delete(nodeKey);
}
}

for (const [nodeKey] of dirtyElements) {
const node = nodeMap.get(nodeKey);

if (node !== undefined) {
// Garbage collect node and its children if they exist
if (!node.isAttached()) {
Expand All @@ -106,15 +94,23 @@ export function $garbageCollectDetachedNodes(
dirtyElements,
);
}

// If we have created a node and it was dereferenced, then also
// remove it from out dirty nodes Set.
if (!prevNodeMap.has(nodeKey)) {
dirtyElements.delete(nodeKey);
}

nodeMap.delete(nodeKey);
}
}
}

for (const nodeKey of dirtyLeaves) {
const node = nodeMap.get(nodeKey);
if (node !== undefined && !node.isAttached()) {
if (!prevNodeMap.has(nodeKey)) {
dirtyLeaves.delete(nodeKey);
}
nodeMap.delete(nodeKey);
}
}
}
107 changes: 107 additions & 0 deletions packages/lexical/src/nodes/__tests__/unit/LexicalGC.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import {
$createParagraphNode,
$createTextNode,
$getNodeByKey,
$getRoot,
} from 'lexical';

import {
$createTestElementNode,
initializeUnitTest,
} from '../../../__tests__/utils';

describe('LexicalGC tests', () => {
initializeUnitTest((testEnv) => {
test('RootNode.clear() with a child and subchild', async () => {
const {editor} = testEnv;
await editor.update(() => {
$getRoot().append(
$createParagraphNode().append($createTextNode('foo')),
);
});
expect(editor.getEditorState()._nodeMap.size).toBe(3);
await editor.update(() => {
$getRoot().clear();
});
expect(editor.getEditorState()._nodeMap.size).toBe(1);
});

test('RootNode.clear() with a child and three subchildren', async () => {
const {editor} = testEnv;
await editor.update(() => {
const text1 = $createTextNode('foo');
const text2 = $createTextNode('bar').toggleUnmergeable();
const text3 = $createTextNode('zzz').toggleUnmergeable();
const paragraph = $createParagraphNode();
paragraph.append(text1, text2, text3);
$getRoot().append(paragraph);
});
expect(editor.getEditorState()._nodeMap.size).toBe(5);
await editor.update(() => {
$getRoot().clear();
});
expect(editor.getEditorState()._nodeMap.size).toBe(1);
});

for (let i = 0; i < 3; i++) {
test(`RootNode.clear() with a child and three subchildren, subchild ${i} deleted first`, async () => {
const {editor} = testEnv;
await editor.update(() => {
const text1 = $createTextNode('foo'); // 1
const text2 = $createTextNode('bar').toggleUnmergeable(); // 2
const text3 = $createTextNode('zzz').toggleUnmergeable(); // 3
const paragraph = $createParagraphNode(); // 4
paragraph.append(text1, text2, text3);
$getRoot().append(paragraph);
});
expect(editor.getEditorState()._nodeMap.size).toBe(5);
await editor.update(() => {
const root = $getRoot();
const subchild = root.getFirstChild().getChildAtIndex(i);
expect(subchild.getTextContent()).toBe(['foo', 'bar', 'zzz'][i]);
subchild.remove();
root.clear();
});
expect(editor.getEditorState()._nodeMap.size).toBe(1);
});
}

for (let i = 0; i < 7; i++) {
/**
* R
* P
* T TE T
* T T
*/
test(`RootNode.clear() with a complex tree, element ${i} node first`, async () => {
const {editor} = testEnv;
await editor.update(() => {
const testElement = $createTestElementNode(); // 1
const testElementText1 = $createTextNode('te1').toggleUnmergeable(); // 2
const testElementText2 = $createTextNode('te2').toggleUnmergeable(); // 3
const text1 = $createTextNode('a').toggleUnmergeable(); // 4
const text2 = $createTextNode('b').toggleUnmergeable(); // 5
const paragraph = $createParagraphNode(); // 6
testElement.append(testElementText1, testElementText2);
paragraph.append(text1, testElement, text2);
$getRoot().append(paragraph);
});
expect(editor.getEditorState()._nodeMap.size).toBe(7);
await editor.update(() => {
const node = $getNodeByKey('1');
node.remove();
$getRoot().clear();
});
expect(editor.getEditorState()._nodeMap.size).toBe(1);
});
}
});
});