Skip to content

perf(core): download each font file once per compile - #9048

Merged
jrusso1020 merged 1 commit into
mainfrom
perf/font-file-fetch-once
Nov 29, 2026
Merged

jrusso1020 merged 1 commit into
mainfrom
perf/font-file-fetch-once

Conversation

@jrusso1020

Copy link
Copy Markdown
Collaborator

What

A compile now downloads each font file once. Before, a file that serves several weights was downloaded once per weight. On a cold page using 18 families this drops downloads from 143 to 54 and bytes from 2.39 MB to 0.61 MB.

Why

A variable font names the same file address for every weight it covers. The cached copy was stored per weight and style, so each weight missed the cache and fetched the same bytes again. Cold compiles are the common case on render workers, so this is time and network on every render.

Related work

None.

How

The cached copy is stored under a name made from the file address alone. Faces are resolved one after another, so the second weight finds the first weight's file on disk. What is embedded in the page does not change.

Test plan

  • Unit tests added/updated
  • Manual testing performed
  • Documentation updated (if applicable)
  • Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug

Run: the new test and the whole packages/core suite (364 files, 7,578 tests) pass locally. Not run: the producer and CLI suites, and no real render.

No visible change

The compiled page is the same; only the number of downloads changes.

Claude Fable 5.2

Measurement

One page using 18 families, empty cache, three runs each on the same machine, real network. The result is the download and byte counts. The seconds are n=3 on one devbox and noisy: no speed claim.

Before After
File downloads 143 54
Distinct file addresses 54 54
Bytes downloaded 2.39 MB 0.61 MB
Files in the cache afterwards 143 54
Seconds (three runs) 3.05, 3.08, 2.50 4.03, 1.44, 1.62
Compiled page size 2,638,449 bytes 2,638,449 bytes

The 4.03 s run was the first run of the whole series. One of the three "before" runs received a slightly different answer from the font service and produced a page 2,624 bytes smaller.

Two weights asking for the same address at once

Within one stylesheet this cannot happen: faces are resolved one after another (const dataUri = await ensureWoff2DataUri(...) inside the for loop in deterministicFonts.ts), so the second weight runs after the first has written the file, and there is one download. The new test covers this case. Two separate lookups for the same family running at the same moment can still each download the file: the write is exclusive (flag: "wx"), and the one that loses reads the winner's copy (the EEXIST branch in ensureWoff2DataUri). That is two downloads, one file, and was already the behaviour for two compiles sharing a cache. No test in this PR covers that second case.

Things to know

  • Files cached under the old names are not reused. Each family is downloaded once more after the upgrade, and the old files stay in the cache folder.
  • The cached file name is a 12-hex-digit digest of the address. The weight and style are no longer part of it.

Self-review

  1. Siblings: cachedWoff2Path has one caller, the loop that resolves a stylesheet's faces. The stylesheet lookup cache uses its own key and is untouched.
  2. Failure paths: unchanged. A failed download writes no file, so the next weight naming that address tries again, as before.
  3. Races: two compiles writing the same address already create the file exclusively and the loser reads the winner's copy. More faces now share one path, which that same path handles.
  4. Trust: the name is a digest of the address, so nothing from the address reaches the file system as a path part.
  5. Tests prove it: four faces naming two addresses download two files and still embed four faces. With the source change reverted the test fails on the download count. It runs in the default core suite.
  6. Rollout: no option, config or API change. Old cache files become unused, as noted above.
  7. Happy path: the measurement above is a real cold compile against the font service, before and after, with the same page size out.

🤖 Generated with Claude Code

A variable font names one file for every weight it covers, and the cached
copy was stored per weight, so the same file was downloaded once per
weight. Store it per address so later weights read the first download.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@jrusso1020 jrusso1020 added the needs-jerrai-review Request a review from Jerrai. Removed after the review posts; re-add to request another. label Nov 28, 2026

@jerry-heygen jerry-heygen left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approved at e0c4c339. Weights and styles that name the same WOFF2 URL now reuse that file within the family’s cache; each face still emits its own descriptors. The regression checks two distinct downloads and four embedded faces. This does not deduplicate in-flight downloads across compiles, and old cache names are not reused.

The hosted core suite was still running when checked. The Comments check is red because the new cache-key comment raises the file’s comment share; that is a separate merge check, not a runtime defect. I did not run a live render or independently benchmark the download savings.

— Jerrai (pr-review)

@jerry-heygen jerry-heygen removed the needs-jerrai-review Request a review from Jerrai. Removed after the review posts; re-add to request another. label Nov 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 275 (base branch 273), smooth 65 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Newly passing (2)

  • seqscaletwice-scale-px-r0-root-z100
  • seqscaletwice-scale-px-r0-nested-z100

Not banked yet, reported and not gated (bank a full-grid run's baseline.json) (2)

  • seqscaletwice-scale-px-r0-root-z100
  • seqscaletwice-scale-px-r0-nested-z100

Quarantined, measured but not gated (0)

@jrusso1020
jrusso1020 enabled auto-merge November 29, 2026 00:07
@jrusso1020
jrusso1020 added this pull request to the merge queue Nov 29, 2026
Merged via the queue into main with commit 55258df Nov 29, 2026
101 of 102 checks passed
@jrusso1020
jrusso1020 deleted the perf/font-file-fetch-once branch November 29, 2026 00:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants