Skip to content

fontfile: name staged fonts by SHA-256 - #8

Closed
xiaoyumuxi wants to merge 1 commit into
mainfrom
fix/font-key-sha256
Closed

xiaoyumuxi wants to merge 1 commit into
mainfrom
fix/font-key-sha256

Conversation

@xiaoyumuxi

Copy link
Copy Markdown

What this changes

fontfile names the file it stages a registered font in by the first 128 bits of the font's SHA-256 and its length, instead of a 128-bit FNV-1a hash and the length. The key keeps its shape (17 bytes, the same file name length); a font staged under the old name is staged once more under the new one. BenchmarkKeyOf measures naming a 2.5 MB face.

Why

RegisterFont hashes the whole font before it registers it, and an application registers its fonts before its first paint. FNV-1a runs a byte at a time, about 390 MB/s here, so an application that ships an interface family, a code family and a patched terminal family (Inter, JetBrains Mono and JetBrains Mono Nerd Font Mono: 38 faces, 25.7 MB) waited about 75 ms of its 85–120 ms registration on the hash alone. SHA-256 uses the CPU's hash instructions (ARMv8 SHA2, x86 SHA-NI) and runs at about 3.3 GB/s, so the same set hashes in about 8 ms. Seen in Lithe, which registers those 38 faces at start.

How it was verified

  • test -z "$(gofmt -l $(git ls-files '*.go'))"
  • go vet ./...
  • go test ./... -count=1: everything passes except 15 cmd/viva packaging tests (TestPackage*, TestAnAdHocSignatureIsRealAndCarriesTheEntitlements, the notarization tests and TestMinGoMinorMatchesGoMod), which fail on this machine on base too (Go 1.27.3 and an Xcode SDK that builds for macOS 13.0 or later while the test manifests say min_system = 12.0). The same 15 fail on upstream/main.
  • platform vet: backends not touched
  • benchmarks before and after: BenchmarkKeyOf, 3 runs each, 6.76–6.91 ms/op (379–388 MB/s) before and 0.78–0.80 ms/op (3264–3341 MB/s) after
  • stripped binary size: crypto/sha256 replaces hash/fnv; both are small and crypto/sha256 is usually linked already
  • a real window: the change is under the text engines; TestARegisteredFontIsDrawnInItsFamily registers a font with CoreText on macOS and passes

Where the boundary sits

Framework: how RegisterFont names the file it stages. Nothing an application sees changes.

Cost

Nothing for a program that registers no fonts.

Registering a font hashes its bytes to name the staged file. FNV-1a 128
runs a byte at a time, about 390 MB/s on an M-series Mac, so an
application that registers a few dozen megabytes of faces before its first
paint (an interface family, a code family and a patched terminal family)
waited about 75 ms on the hash alone. SHA-256 uses the platform's hash
instructions and runs at about 3.3 GB/s. The key keeps its shape (128 bits
and the length byte); a face staged under the old name is staged once more
under the new one.
@xiaoyumuxi

Copy link
Copy Markdown
Author

上游已以 vivaui#233 合入同等改动,fork main 已通过上游同步 #21(merge commit 7df24f7)包含它,这里不再单独合入。Upstream merged the same change as vivaui#233; fork main carries it through the upstream sync #21 (merge commit 7df24f7).

@xiaoyumuxi xiaoyumuxi closed this Nov 15, 2026
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.

1 participant