Skip to content

Commit 736ed1f

Browse files
committed
keep the instances of a variable font by recent use, up to 64
1 parent 906da87 commit 736ed1f

5 files changed

Lines changed: 45 additions & 18 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Upgrading from 3.x? Upgrade to the latest 3.x release first, fix the deprecation
137137
### Fixes
138138

139139
- `renderSVG()` bounds the points paths are made of: an arc between far-apart ends is drawn with at most 64 curves a turn (a 139-byte path, `M 1e25 100 A 40 30 20 1 1 160 100`, stroked, ran the process out of memory), and the curves of arcs and the points of all paths, also of each copy drawn through `<use>`, count against 2^25 points plus 4 per pixel of the output in one render and 2^21 in one path. Reading path data and placing text along a `<textPath>` count, too, and SVGs embedded as images share the budget of the render they are in. More throw a `LimitError` (which a text's layout no longer takes for a damaged font). A path drawn through `<use>` is read once a render instead of once a copy.
140-
- SVG text: the glyph outlines of all texts count against the render's points before they are made, 16 points a command (20 texts of 1,000 characters of a complex font ran the process out of memory), so very long texts need a larger output; many `<tspan>`s in one `<text>` take linear time (10,000 took 20 s); a `<textPath>` on a long path renders at a normal size; bitmap emoji (CBDT, sbix) are decoded once a render instead of once a draw (copies through `<use>` took minutes), at most 2048x2048 pixels each (larger ones aren't drawn, also in `renderText()`: one that said 16383x16383 ran the process out of memory), and counted before they are decoded, also damaged ones. `renderText()` counts the work of decoding and scaling its bitmap glyphs against 2^28 pixels plus 64 per pixel of the image (more throw a `LimitError`), keeps at most 2^22 pixels of scaled glyphs in a render and none after it (they were kept with the font: 1 GB a size), keeps rasters of outline and color glyphs with the font only up to 256 KB each, 16 MB a font and 8 text colors a glyph (62 letters at size 6000 kept 774 MB, a new color each render grew without end), counts the glyphs it rasterizes against the same work budget as bitmap glyphs (2000 huge glyphs without advance took 260 s), frees a size's rasters when its sizes are evicted, and keeps at most 16 instances of a variable font (a new weight each render ran out of memory after 8000), and doesn't draw a glyph scaled to more than 4 times the image (a strike at ppem 1 made a 32768x32768 copy for a 10x10 image).
140+
- SVG text: the glyph outlines of all texts count against the render's points before they are made, 16 points a command (20 texts of 1,000 characters of a complex font ran the process out of memory), so very long texts need a larger output; many `<tspan>`s in one `<text>` take linear time (10,000 took 20 s); a `<textPath>` on a long path renders at a normal size; bitmap emoji (CBDT, sbix) are decoded once a render instead of once a draw (copies through `<use>` took minutes), at most 2048x2048 pixels each (larger ones aren't drawn, also in `renderText()`: one that said 16383x16383 ran the process out of memory), and counted before they are decoded, also damaged ones. `renderText()` counts the work of decoding and scaling its bitmap glyphs against 2^28 pixels plus 64 per pixel of the image (more throw a `LimitError`), keeps at most 2^22 pixels of scaled glyphs in a render and none after it (they were kept with the font: 1 GB a size), keeps rasters of outline and color glyphs with the font only up to 256 KB each, 4 MB a font and 8 text colors a glyph (62 letters at size 6000 kept 774 MB, a new color each render grew without end), counts the glyphs it rasterizes against the same work budget as bitmap glyphs (2000 huge glyphs without advance took 260 s), frees a size's rasters when its sizes are evicted, and keeps at most 64 instances of a variable font in `renderText()` (a new weight each render ran out of memory after 8000; `renderSVG()` keeps the instances of one render), and doesn't draw a glyph scaled to more than 4 times the image (a strike at ppem 1 made a 32768x32768 copy for a 10x10 image).
141141
- `renderText()` takes linear time for many combining marks and lines (400,000 lines took minutes).
142142
- Very long text (50,000 characters in SVG, 200,000 combining marks or lines in `renderText()`) no longer overflows the call stack or says the font is damaged.
143143
- Binary data and dates of other realms (iframes, `node:vm`, Jest) are accepted; objects that only claim to be one by `Symbol.toStringTag` aren't, and a detached buffer throws a `TypeError` that says so.

‎src/render/font.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,18 @@ export class Font {
169169
const base = Object.getPrototypeOf(this) instanceof Font ? Object.getPrototypeOf(this) as Font : this;
170170
const key = coords.join(',');
171171
const cached = base.instances?.get(key);
172-
if (cached) return cached;
172+
if (cached) {
173+
// (the least recently used is the one dropped: used again, an instance goes to the back)
174+
base.instances!.delete(key);
175+
base.instances!.set(key, cached);
176+
return cached;
177+
}
173178
const instance = Object.create(base) as Font;
174-
// (at most 16 instances a font, the oldest dropped: a new weight each render kept one for each, with its sizes,
175-
// until the process ran out of memory)
179+
// (at most 64 instances a font (a chart of 5 sizes and 4 weights is 20, with an optical size axis), the least
180+
// recently used dropped: a new weight each render kept one for each, with its sizes, until the process ran out of
181+
// memory)
176182
const instances = base.instances ??= new Map();
177-
if (instances.size >= 16) instances.delete(instances.keys().next().value as string);
183+
if (instances.size >= 64) instances.delete(instances.keys().next().value as string);
178184
instances.set(key, instance);
179185
instance.coords = coords;
180186
instance.outlineCache = new Map();

‎src/render/text.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,14 @@ async function layOut(fonts: Uint8Array[], size: number, text: string, layout: L
343343
return { width, height, coverage, color };
344344
}
345345

346-
// the bytes of glyph rasters kept with a font across renders, at all its sizes: 256 KB a raster (512x512 of coverage)
347-
// and 16 MB a font at most. The others are kept for one render (62 letters at size 6000 kept 774 MB with the font, for
346+
// the bytes of glyph rasters kept with a font (an instance of a variable font) across renders, at all its sizes: 256 KB a
347+
// raster (512x512 of coverage) and 4 MB a font at most (64 instances of a font may be kept). The others are kept for one render (62 letters at size 6000 kept 774 MB with the font, for
348348
// good, and one more each size)
349349
const keptBytes = new WeakMap<Font, number>();
350350
function keptWithFont(g: Glyph, bytes: number): boolean {
351351
if (bytes > 2 ** 18) return false;
352352
const kept = (keptBytes.get(g.font.font) ?? 0) + bytes;
353-
if (kept > 2 ** 24) return false;
353+
if (kept > 2 ** 22) return false;
354354
keptBytes.set(g.font.font, kept);
355355
return true;
356356
}

‎tests/bitmap-glyph-limits.js‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,6 @@ const NS = 'xmlns="http://www.w3.org/2000/svg"';
5757
}],
5858
// glyphs too large to keep, over each other (no advance) 1000 times at size 8000: drawn again each time, 260 s
5959
['2000 huge glyphs without advance', () => Image.renderText(text(2).repeat(1000), {font: font('zero'), size: 8000, width: 10, height: 10, maxPixels: 100})],
60-
// every weight of a variable font was an instance of its own, kept with the font for good (with its sizes and
61-
// glyphs): a new weight each render ran out of memory after 8000
62-
['a variable font at 400 weights', async () => {
63-
const data = fs.readFileSync(`${__dirname}/fonts/resvg/RobotoFlex.subset.ttf`);
64-
collect?.();
65-
const heap = process.memoryUsage().heapUsed;
66-
for (let i = 0; i < 400; i++) await Image.renderText('ABCDEFGHIJKLMNOPQRSTUVWXYZ', {font: data, size: 64, variations: {wght: 100 + i * 2}});
67-
collect?.();
68-
if (collect && process.memoryUsage().heapUsed - heap > 128 * 2 ** 20) panic(`a variable font at 400 weights: kept ${(process.memoryUsage().heapUsed - heap) >> 20} MB of heap`);
69-
}],
7060
]) {
7161
collect?.();
7262
const begin = Date.now(), before = process.memoryUsage().arrayBuffers;

‎tests/font-instances.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const fs = require('fs');
2+
3+
const panic = message => {
4+
console.error(message);
5+
process.exit(1);
6+
};
7+
8+
// the instances of a variable font (one for each weight, and each size for a font with an optical size axis) are kept
9+
// in a bounded number, the least recently used dropped: a chart of 5 sizes and 4 weights is 20 instances, and rebuilt
10+
// each time it took 3 times as long. A new weight each render doesn't grow memory without end
11+
(async () => {
12+
const {Font} = await import('../dist/render/font.js');
13+
const font = new Font(fs.readFileSync(`${__dirname}/fonts/resvg/RobotoFlex.subset.ttf`));
14+
const used = [];
15+
for (let size = 0; size < 5; size++) for (let weight = 0; weight < 4; weight++) used.push(font.withVariations({opsz: 12 + size * 6, wght: 300 + weight * 200}));
16+
if (new Set(used).size !== 20) panic(`20 combinations should be 20 instances, not ${new Set(used).size}`);
17+
for (let round = 0; round < 3; round++) {
18+
let i = 0;
19+
for (let size = 0; size < 5; size++) for (let weight = 0; weight < 4; weight++) {
20+
if (font.withVariations({opsz: 12 + size * 6, wght: 300 + weight * 200}) !== used[i++]) panic(`round ${round}: an instance of 20 was rebuilt`);
21+
}
22+
}
23+
// a hot instance stays while new ones come: one used between every two new weights isn't dropped
24+
const hot = font.withVariations({wght: 900});
25+
for (let i = 0; i < 500; i++) {
26+
font.withVariations({wght: 100 + i * 1.5});
27+
if (font.withVariations({wght: 900}) !== hot) panic(`the instance in use was dropped after ${i} new weights`);
28+
}
29+
// and 500 weights kept no more than a bounded number
30+
if (font.instances.size > 64) panic(`${font.instances.size} instances kept`);
31+
})();

0 commit comments

Comments
 (0)