Repository navigation
Cache resolved custom fonts on iOS Fabric - #59153
Open
hryhoriiK97 wants to merge 1 commit into
Open
hryhoriiK97 wants to merge 1 commit into
hryhoriiK97 wants to merge 1 commit into
Conversation
hryhoriiK97
force-pushed
the
ios-fabric-font-cache
branch
from
October 17, 2026 11:57
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>
hryhoriiK97
force-pushed
the
ios-fabric-font-cache
branch
from
October 17, 2026 11:58
5cc89ee to
f9c4305
Compare
This branch has not been deployed
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.
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.
🤖 Generated with Claude Code