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

[Fizz] Optimize end tags chunks #27522

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Changes from 1 commit
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
31 changes: 16 additions & 15 deletions packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -2509,7 +2509,7 @@ function pushStyleImpl(
target.push(stringToChunk(escapeTextForBrowser('' + child)));
}
pushInnerHTML(target, innerHTML, children);
target.push(endTag1, stringToChunk('style'), endTag2);
target.push(endChunkForTag('style'));
Copy link
Collaborator

Choose a reason for hiding this comment

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

might as well precompute these

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not well, it is already cached so the computation that causes the memory allocation is only executed once

Copy link
Contributor Author

Choose a reason for hiding this comment

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

return null;
}

Expand Down Expand Up @@ -2824,7 +2824,7 @@ function pushTitleImpl(
target.push(stringToChunk(escapeTextForBrowser('' + child)));
}
pushInnerHTML(target, innerHTML, children);
target.push(endTag1, stringToChunk('title'), endTag2);
target.push(endChunkForTag('title'));
return null;
}

Expand Down Expand Up @@ -3081,7 +3081,7 @@ function pushScriptImpl(
if (typeof children === 'string') {
target.push(stringToChunk(encodeHTMLTextNode(children)));
}
target.push(endTag1, stringToChunk('script'), endTag2);
target.push(endChunkForTag('script'));
return null;
}

Expand Down Expand Up @@ -3484,8 +3484,15 @@ export function pushStartInstance(
return pushStartGenericElement(target, props, type);
}

const endTag1 = stringToPrecomputedChunk('</');
const endTag2 = stringToPrecomputedChunk('>');
const endTagCache = new Map<string, PrecomputedChunk>();
function endChunkForTag(tag: string): PrecomputedChunk {
let chunk = endTagCache.get(tag);
if (chunk === undefined) {
chunk = stringToPrecomputedChunk('</' + tag + '>');
endTagCache.set(tag, chunk);
}
return chunk;
}

export function pushEndInstance(
target: Array<Chunk | PrecomputedChunk>,
Expand Down Expand Up @@ -3547,7 +3554,7 @@ export function pushEndInstance(
}
break;
}
target.push(endTag1, stringToChunk(type), endTag2);
target.push(endChunkForTag(type));
}

function writeBootstrap(
Expand Down Expand Up @@ -4502,9 +4509,7 @@ export function writePreamble(
// if the main content contained the </head> it would also have provided a
// <head>. This means that all the content inside <html> is either <body> or
// invalid HTML
writeChunk(destination, endTag1);
writeChunk(destination, stringToChunk('head'));
writeChunk(destination, endTag2);
writeChunk(destination, endChunkForTag('head'));
}
}

Expand Down Expand Up @@ -4577,14 +4582,10 @@ export function writePostamble(
resumableState: ResumableState,
): void {
if (resumableState.hasBody) {
writeChunk(destination, endTag1);
writeChunk(destination, stringToChunk('body'));
writeChunk(destination, endTag2);
writeChunk(destination, endChunkForTag('body'));
}
if (resumableState.hasHtml) {
writeChunk(destination, endTag1);
writeChunk(destination, stringToChunk('html'));
writeChunk(destination, endTag2);
writeChunk(destination, endChunkForTag('html'));
}
}

Expand Down