Skip to content

refactor(theme): drop the fake limits on the font caches - #2228

Merged
WhiredPlanck merged 1 commit into
osfans:developfrom
WhiredPlanck:theme-font-dedup
Nov 12, 2026
Merged

WhiredPlanck merged 1 commit into
osfans:developfrom
WhiredPlanck:theme-font-dedup

Conversation

@WhiredPlanck

Copy link
Copy Markdown
Collaborator

Pull request

Issue tracker

Fixes will automatically close the related issues

Fixes #

Feature

ThemeFonts kept its two font lookups in LruCaches whose limits could never be reached.

  • Every Typeface it builds is held by one of the ten role fields, so nothing is ever evicted while the theme lives, and the 16 * 1024 figure on the typeface cache was never applied. Its sizeOf counted 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.
  • 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. 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 — FileState still carries path, mtime and size, so a font file replaced on disk still misses on the next theme load. getTypeface() and getFontFamily() collapse to getOrPut. 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:

situation Native Heap extension font
no custom fonts 24.2 MB —
fonts configured, keyboard drawn 22.6 MB mapped 19032 KB, resident 4608 KB
after typing and picking CJK candidates 23.3 MB mapped 19032 KB, resident 4928 KB

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:

  • A role whose own font file is missing returns Typeface.DEFAULT and never builds a chain at all, so dropping only hanb.ttf into fonts/ has no effect (verified: the file is never read).
  • preview_font is documented in trime.yaml but has no field in GeneralStyle, so it is silently ignored.

Code of conduct

Code style

Build pass

  • make debug

Manually test

  • Done

Code Review

  1. No wildcards import
  2. Manual build and test pass
  3. GitHub Action CI pass
  4. At least one contributor review and approve
  5. Merged clean without conflicts
  6. PR will be merged by rebase upstream base

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:

  1. Put any .ttf in rime/fonts/ and point key_font at it — the key labels change, which is the normal path through getTypeface().
  2. Add latin_font and hanb_font files as well, then run adb 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.
  3. Replace one font file on disk and reload the theme — the new file takes effect, because mtime and size are still part of the key.
  4. adb shell dumpsys meminfo com.osfans.trime with and without the fonts installed: the native heap is the same within noise, i.e. this commit is not claiming a win.

spotlessCheck, :app:testDebugUnitTest and make debug all pass. One commit, one file, +9/−23.

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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

@WhiredPlanck
WhiredPlanck merged commit 29a0a78 into osfans:develop Nov 12, 2026
5 checks passed
@WhiredPlanck
WhiredPlanck deleted the theme-font-dedup branch November 14, 2026 11:44
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.

3 participants