mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-16 06:19:21 +00:00
fix(producer): stop copying the whole document per character when scanning fonts
Studio preview re-runs font injection on every save; scanning a multi-MB bundled document built a per-character array twice, about 0.3s of pure allocation per request. Iterate the strings directly; the requested text subset is unchanged.
This commit is contained in:
@@ -230,4 +230,26 @@ describe("Google Fonts text subsetting", () => {
|
||||
|
||||
expect(text).toContain("A");
|
||||
});
|
||||
|
||||
it("scans a multi-MB document without building a per-character array", async () => {
|
||||
const html = `<!doctype html><html><head><style>
|
||||
p { font-family: "Noto Performance Test", sans-serif; }
|
||||
</style></head><body><p>旅</p><i data-payload="${"xyz".repeat(400_000)}"></i></body></html>`;
|
||||
const realFrom = Array.from;
|
||||
let wholeDocumentCopies = 0;
|
||||
Array.from = ((source: unknown, ...rest: unknown[]) => {
|
||||
if (typeof source === "string" && source.length > 100_000) wholeDocumentCopies += 1;
|
||||
return (realFrom as (...args: unknown[]) => unknown[])(source, ...rest);
|
||||
}) as typeof Array.from;
|
||||
let text: string;
|
||||
try {
|
||||
text = await subsetTextFor(html);
|
||||
} finally {
|
||||
Array.from = realFrom;
|
||||
}
|
||||
|
||||
expect(wholeDocumentCopies).toBe(0);
|
||||
expect(text.startsWith("<!")).toBe(true);
|
||||
for (const character of "xyz旅") expect(text).toContain(character);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1410,8 +1410,12 @@ function extractGoogleFontsText(html: string): string | undefined {
|
||||
|
||||
// Intentional over-approximation: raw html includes base64, scripts, and
|
||||
// class names, but they collapse in the Set and the budget gate catches bloat.
|
||||
// Iterate strings directly: Array.from(html) allocates one element per code point (~0.3s per 5 MB).
|
||||
const sourceCharacters = new Set<string>();
|
||||
for (const character of html) sourceCharacters.add(character);
|
||||
for (const character of decodedBodyText) sourceCharacters.add(character);
|
||||
const uniqueCharacters = new Set<string>();
|
||||
for (const character of new Set([...Array.from(html), ...Array.from(decodedBodyText)])) {
|
||||
for (const character of sourceCharacters) {
|
||||
addCaseClosure(uniqueCharacters, character, locales);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user