Repository navigation
Conversation
SetQuery always discarded the candidate fonts, so ResolveFace built them again on its next rune cache miss, even when the query had not changed. Text that alternates between families, such as prose with inline code, built the candidates at every switch. SetQuery now keeps the candidates when the query is unchanged, and the font map keeps the candidates of the eight most recently used queries and scripts, so switching back to one of them reuses its candidates. They are compared with a copy of the query families, since callers may change a families slice in place. The cache is cleared whenever the font database changes (AddFont, AddFace, UseSystemFonts). BenchmarkResolveFaceAlternating (three queries in turn, rune cache off, system fonts of macOS): 295µs to 0.36µs per round, 36.6KB to 288B allocated. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Thank you for this nice optimization ! After a quick read, I have two overall question/suggestions about the implementation :
|
|
using a fixed-size array seems fine to me. my experience is that slices of structs as opposed to pointers is typically more hassle than its worth except for small, large-volume elements, just because there are so many gotcha's associated with writing to such things. so I always use pointers as a default. I don't see in this case that it would matter, and we could switch to non-pointer if you'd prefer? |
Yes, that would be best, if doable. Thank you ! |
The cache of candidates is now an array of eight entries stored by value. Each entry records when it was last used, and a missing query and script replaces the least recently used entry, reusing its buffers. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
FontMap.SetQueryalways setbuilt = false, so the nextResolveFacethat missed the rune cache ranbuildCandidatesagain, even when the query was identical to the current one.buildCandidatesscans the whole font database. With the system fonts of macOS, that costs about 100 µs. Shapers set the query for each span of text, so text that alternates between families rebuilt the candidates at every switch: prose with inline code (sans, mono, sans), or bold and regular words. The rune cache does not help when the next rune after a switch has not been seen with that query yet.SetScriptalready skipped an unchanged script.This PR makes two changes:
SetQuerykeeps the built candidates when the new query has the same families and aspect as the candidates. The comparison uses a copy of the families taken when the candidates were built, not the stored query, because a caller may change itsFamiliesslice in place between calls.buildCandidatesreuses a cached entry when there is one; otherwise it builds a new entry, recycling the buffers of the least recently used one.The cache is cleared in
appendFootprints, which is the only place the database changes.AddFont,AddFaceandUseSystemFontsall go through it, so no future mutation can forget to invalidate. Nothing else feedsbuildCandidates: the inputs are the database, the query and the script.FontMapstays not safe for concurrent use, and the cache adds no shared state.Tests:
TestCandidatesCacheruns 400 steps on the sample Linux font set. Each step sets a query and script chosen from 11 queries (more than the cache holds) and 4 scripts, mostly alternating between two queries. It compares the candidates and the faces resolved for 8 runes (with the rune cache cleared) against a freshFontMap.TestCandidatesCacheFamiliesChangedchanges a families slice in place between twoSetQuerycalls.TestCandidatesCacheAddFontcoversAddFace,AddFontand adding system fonts after both queries were cached. The query that matches the new font must resolve to it, and the other query's candidates must equal a fresh map's.Benchmarks (main vs this branch, n=8):
go test ./...,go vet ./...and staticcheck v0.8.0 pass. The fontscan tests also pass with Go 1.22.12 and under-race.🤖 Generated with Claude Code