Skip to content

[fontscan] keep the candidate fonts of recent queries - #305

Open
rcoreilly wants to merge 2 commits into
go-text:mainfrom
cogentcore:fontscan-candidates-cache
Open

rcoreilly wants to merge 2 commits into
go-text:mainfrom
cogentcore:fontscan-candidates-cache

Conversation

@rcoreilly

Copy link
Copy Markdown
Contributor

FontMap.SetQuery always set built = false, so the next ResolveFace that missed the rune cache ran buildCandidates again, even when the query was identical to the current one. buildCandidates scans 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. SetScript already skipped an unchanged script.

This PR makes two changes:

  • SetQuery keeps 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 its Families slice in place between calls.
  • The font map keeps the candidates of the 8 most recently used (families, aspect, script) combinations. buildCandidates reuses 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, AddFace and UseSystemFonts all go through it, so no future mutation can forget to invalidate. Nothing else feeds buildCandidates: the inputs are the database, the query and the script. FontMap stays not safe for concurrent use, and the cache adds no shared state.

Tests:

  • TestCandidatesCache runs 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 fresh FontMap.
  • TestCandidatesCacheFamiliesChanged changes a families slice in place between two SetQuery calls.
  • TestCandidatesCacheAddFont covers AddFace, AddFont and 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.
  • A mutation check confirms that removing the invalidation, or ignoring the script in the cache key, fails these tests.

Benchmarks (main vs this branch, n=8):

benchmark main branch
ResolveFaceAlternating (new: 3 queries in turn, rune cache off, system fonts) 295 µs, 36.6 KB, 56 allocs 0.36 µs, 288 B, 6 allocs -99.9%
ResolveFont 4.94 µs 4.97 µs ~
SetQuery (same query each time) 11.5 ns 14.3 ns +2.8 ns (families comparison)

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

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>
@benoitkugler

Copy link
Copy Markdown
Contributor

Thank you for this nice optimization !

After a quick read, I have two overall question/suggestions about the implementation :

  • can we use a (fixed size) array (of length 8) for candidatesCache. That would probably clarify the code.
  • do we really need candidatesCache elements to be pointers ?

@rcoreilly

Copy link
Copy Markdown
Contributor Author

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?

@benoitkugler

Copy link
Copy Markdown
Contributor

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>
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