Repository navigation
refactor(theme): drop the fake limits on the font caches - #2228
Merged
Merged
Conversation
ThemeFonts held its two caches as LruCaches, but neither limit could ever be reached: every Typeface it builds is kept alive by one of the ten role fields, so nothing is evicted while the theme lives, and the 16 MB figure only counted the role's own files, never the latin and extension families that each chain attaches as well. The FontFamily cache was the worse of the two, because deduplication is all it is good for: evicting a family that a live Typeface still refers to makes the next role build a second one for the same file, that is a second native copy of the same bytes. Both are plain maps now, keyed exactly as before, so a replaced font file still misses on the next theme load. This is not a memory saving: measured on an API 35 emulator with a 19 MB extension font, the file is mapped rather than copied, only the pages shaping touches stay resident (~4.8 MB), and the native heap is unchanged from the same build without any custom fonts. It removes a limit that never protected anything and an eviction path that could only cost memory.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The review identifies no unresolved issues, and the change is narrowly scoped to simplifying font caches while retaining deduplication and file-state invalidation.
No open issues 🎉
What changed in this PR
Refactors ThemeFonts to remove ineffective LRU limits while preserving per-theme font-family and typeface deduplication and file-state-based cache invalidation.
Changes:
- Replaces both LRU caches with maps retained for the theme lifetime.
- Simplifies cache population with
getOrPut. - Preserves path, modification-time, and size tracking for font replacement detection.
| File | Description |
|---|---|
| app/src/main/java/com/osfans/trime/data/theme/ThemeFonts.kt | Removes artificial cache eviction while retaining deduplicated font-family and typeface construction per theme. |
Review effort: Lite
Bambooin
approved these changes
Nov 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull request
Issue tracker
Fixes will automatically close the related issues
Fixes #
Feature
ThemeFontskept its two font lookups inLruCaches whose limits could never be reached.Typefaceit builds is held by one of the ten role fields, so nothing is ever evicted while the theme lives, and the16 * 1024figure on the typeface cache was never applied. ItssizeOfcounted only the role's own files, not the latin and extension families that the same chain attaches, so even the number it pretended to enforce was too small.FontFamilycache was the worse of the two, because deduplication is all it is good for: evicting a family that a liveTypefacestill refers to makes the next role build a second one for the same file, that is a second native copy of the same bytes. A theme whose roles list several font files can reach the 16-entry limit, and reaching it costs memory instead of saving it.Both are plain maps now, keyed exactly as before —
FileStatestill carries path, mtime and size, so a font file replaced on disk still misses on the next theme load.getTypeface()andgetFontFamily()collapse togetOrPut. No behaviour change, no new API, nothing to configure.This is not a memory saving. Measured on an API 35 emulator with a 19 MB extension font (
hanb_font) and the other roles pointing at a 742 KB font:Android maps font files instead of copying them onto the heap, so only the pages text shaping actually touches stay resident, and those are reclaimable page cache: the native heap does not move whether the fonts are there or not. What this commit removes is a limit that never protected anything, and an eviction path that could only ever cost memory.
Two findings right next to this code are deliberately not touched here, and are worth a separate decision:
Typeface.DEFAULTand never builds a chain at all, so dropping onlyhanb.ttfintofonts/has no effect (verified: the file is never read).preview_fontis documented intrime.yamlbut has no field inGeneralStyle, so it is silently ignored.Code of conduct
Code style
make style-lintBuild pass
make debugManually test
Code Review
Daily build
Login and download artifact at https://github.com/osfans/trime/actions
Additional Info
Related to #1791: the per-draw
getTypeface()file lookups it reports were already removed by the previous font change, this one only fixes the bookkeeping around them. Preloading fonts off the main thread is out of scope here.How to check it by hand:
.ttfinrime/fonts/and pointkey_fontat it — the key labels change, which is the normal path throughgetTypeface().latin_fontandhanb_fontfiles as well, then runadb shell su 0 showmap <pid> | grep fonts— each font file appears exactly once no matter how many roles reference it, which is the deduplication this keeps.adb shell dumpsys meminfo com.osfans.trimewith and without the fonts installed: the native heap is the same within noise, i.e. this commit is not claiming a win.spotlessCheck,:app:testDebugUnitTestandmake debugall pass. One commit, one file, +9/−23.