Skip to content

Commit

Permalink
improve clearing perf by not checking react ownership in singletons a…
Browse files Browse the repository at this point in the history
…nd resource ownership in container
  • Loading branch information
gnoff committed Oct 21, 2022
1 parent 5d7bac0 commit 2d9d0ca
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
15 changes: 6 additions & 9 deletions packages/react-dom-bindings/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,9 @@ export function clearContainer(container: Container): void {
function clearContainerChildren(container: Node) {
let node;
let nextNode: ?Node = container.firstChild;
if (nextNode && nextNode.nodeType === DOCUMENT_TYPE_NODE) {
nextNode = nextNode.nextSibling;
}
while (nextNode) {
node = nextNode;
nextNode = nextNode.nextSibling;
Expand All @@ -770,11 +773,7 @@ function clearContainerChildren(container: Node) {
}
}
}
if (node.nodeType === DOCUMENT_TYPE_NODE || isMarkedResource(node)) {
// we retain document nodes and resources
} else {
container.removeChild(node);
}
container.removeChild(node);
}
return;
}
Expand Down Expand Up @@ -1674,10 +1673,8 @@ export function clearSingleton(instance: Instance): void {
while (node) {
const nextNode = node.nextSibling;
const nodeName = node.nodeName;
if (getInstanceFromNodeDOMTree(node) || isMarkedResource(node)) {
// retain nodes owned by React
} else if (
nodeName === 'TITLE' ||
if (
isMarkedResource(node) ||
nodeName === 'HEAD' ||
nodeName === 'BODY' ||
nodeName === 'STYLE' ||
Expand Down
33 changes: 26 additions & 7 deletions packages/react-dom/src/__tests__/ReactDOMFloat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,32 @@ describe('ReactDOMFloat', () => {
return readText(text);
}

// @gate enableFloat
it('can render resources before singletons', async () => {
const root = ReactDOMClient.createRoot(document);
root.render(
<>
<title>foo</title>
<html>
<head>
<link rel="foo" href="foo" />
</head>
<body>hello world</body>
</html>
</>,
);
expect(Scheduler).toFlushWithoutYielding();
expect(getMeaningfulChildren(document)).toEqual(
<html>
<head>
<title>foo</title>
<link rel="foo" href="foo" />
</head>
<body>hello world</body>
</html>,
);
});

// @gate enableFloat
it('can acquire a resource after releasing it in the same commit', async () => {
const root = ReactDOMClient.createRoot(container);
Expand Down Expand Up @@ -1208,7 +1234,6 @@ describe('ReactDOMFloat', () => {
<html>
<head>
<link rel="stylesheet" href="aresource" data-precedence="foo" />
<link rel="preload" href="aresource" as="style" />
</head>
<body>
<div>hello world</div>
Expand Down Expand Up @@ -1372,7 +1397,6 @@ describe('ReactDOMFloat', () => {
<html>
<head>
<link rel="stylesheet" href="foo" data-precedence="foo" />
<link rel="preload" href="foo" as="style" />
</head>
<body>hello world</body>
</html>,
Expand Down Expand Up @@ -1425,7 +1449,6 @@ describe('ReactDOMFloat', () => {
<link rel="stylesheet" href="foo" data-precedence="foo" />
<link rel="stylesheet" href="bar" data-precedence="bar" />
<link rel="stylesheet" href="qux" data-precedence="qux" />
<link rel="preload" href="qux" as="style" />
</head>
<body>client</body>
</html>,
Expand Down Expand Up @@ -1503,8 +1526,6 @@ describe('ReactDOMFloat', () => {
<head>
<link rel="stylesheet" href="foo" data-precedence="foo" />
<link rel="stylesheet" href="bar" data-precedence="bar" />
<link rel="preload" href="foo" as="style" />
<link rel="preload" href="bar" as="style" />
</head>
<body>hello</body>
</html>,
Expand All @@ -1530,8 +1551,6 @@ describe('ReactDOMFloat', () => {
<link rel="stylesheet" href="foo" data-precedence="foo" />
<link rel="stylesheet" href="bar" data-precedence="bar" />
<link rel="stylesheet" href="baz" data-precedence="baz" />
<link rel="preload" href="foo" as="style" />
<link rel="preload" href="bar" as="style" />
<link rel="preload" href="baz" as="style" />
</head>
<body>hello</body>
Expand Down

0 comments on commit 2d9d0ca

Please sign in to comment.