Repository navigation
Cache resolved custom fonts on iOS Fabric - #59153
hryhoriiK97 wants to merge 1 commit into
Conversation
8f3f830 to
5cc89ee
Compare
Resolving a custom `fontFamily` in `RCTFontWithFontProperties` enumerates the family's faces and instantiates every one of them to pick the closest weight and style, and it ran once per text fragment per attributed string build. Cache the resolved font per family, size, weight and style, cleared when fonts are registered at runtime, as the legacy `RCTFont.mm` already does for its own lookups. Variants and variation settings are applied after the cache as before. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
5cc89ee to
f9c4305
Compare
cipolleschi
left a comment
There was a problem hiding this comment.
Thanks for improving this.
|
@cipolleschi has imported this pull request. If you are a Meta employee, you can view this in D126316025. |
|
@hryhoriiK97: @javache asked what's the size of the cache and what's the impact on memory. Can you help running some tests and attach these information to the Test Plan or Summary, please? |
@cipolleschi @javache sure! I tested it on an iPhone 15 Pro (Release build), with the cache turned on and off:
The cache keeps one font for each family, size, weight and style the app uses. Each one takes about 0.7 KB, so the cache grows with the number of different fonts, not with the amount of text. In the first case, the app uses the same amount of memory with or without the cache. There's no fixed limit, but |
Summary:
RCTFontWithFontProperties(ReactCommon/react/renderer/textlayoutmanager/platform/ios/.../RCTFontUtils.mm) resolves theUIFontfor every text fragment when a paragraph'sNSAttributedStringis built. The system font is cached, but a customfontFamilyis not.Current behavior. For a custom family, every call enumerates the font database with
+[UIFont fontNamesForFamilyName:], then instantiates every face in the family and reads its traits and weight to pick the closest one. That runs once per fragment per attributed-string build, so a list of 1000 labels in a custom font resolves the same font 1000 times on the layout thread, and again on the main thread whenever the attributed-string cache (256 entries) has evicted the paragraph before it is drawn.With this change. The resolved font is cached per family, effective size, weight and style, in an
NSCache(thread-safe, so the layout and main threads share it) that is cleared onkCTFontManagerRegisteredFontsChangedNotification, so a font registered at runtime does not stay cached as unknown. The lookup is an extraelse ifarm ahead of the existing resolution code, which is unchanged; a miss runs it and stores the result, including the fallback to the system font for an unknown family. Font variants and variation settings are applied after the lookup, as before, so they are not part of the key. The legacyReact/Views/RCTFont.mmcaches its lookups the same way.Why. Custom fonts are the common case in production apps, and this cost scales with the number of text fragments on screen rather than with the number of distinct fonts, which is usually a handful.
Changelog:
[IOS] [CHANGED] - Cache custom font family resolution for Fabric text, so a font family is resolved once per family, size, weight and style instead of once per text fragment
Test Plan:
node scripts/clang-format.js --checkon the changed file passes.xcodebuildin Release for a physical iPhone 15 Pro; the Text examples render as before.TransactionTelemetry), a timer around the attributed-string conversion inside measurement (where fonts are resolved), a run-loop observer after the CoreAnimation commit (mount end to display) and a timer around-[RCTParagraphTextView drawRect:]. Two builds of the same tree differing only in this file. The button presses were driven by Argent on the physical iPhone, so both builds ran the identical sequence (5 mounts and unmounts per font). iPhone 15 Pro, Release, medians of 5 mounts:fontFamily: 'Avenir',fontWeight: '500'drawRecttotal (main thread)The same scenario with the system font is unchanged within noise (47.6 ms and 45.7 ms layout), as expected since the system font was already cached. The draw-side gain comes from paragraphs whose attributed string had been evicted from the 256-entry cache before drawing; a list with fewer than 256 distinct strings would see only the layout-side gain.
UIFontobject, measured withmalloc_size. iPhone 15 Pro, Release, one binary with the cache switched on and off by a launch argument (not part of this PR), 3 launches with the cache and 2 without:In the first scenario the app's total memory (
phys_footprint) is the same with and without the cache (106 and 108 MB with the labels mounted). In the other two, the difference is within a few MB, close to the run-to-run variation of the same build. Repeating the 1000 sizes reused the existing entries instead of adding new ones.NSCacheevicts entries under memory pressure; none were evicted in these runs.🤖 Generated with Claude Code