Skip to content

Commit 83604bd

Browse files
tomekzawclaude
andcommitted
Skip the unused getDesiredWidth pass when creating Android text layouts
TextLayoutManager.createLayout measured the whole string with Layout.getDesiredWidth before building a StaticLayout even when the result could not affect the layout width: for an EXACTLY width, and for boring text that did not fit (min(desired, available) is then always available). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8aa97bb commit 83604bd

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.kt‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -877,13 +877,22 @@ internal object TextLayoutManager {
877877
)
878878
}
879879

880-
val desiredWidth = ceil(Layout.getDesiredWidth(text, paint)).toInt()
881-
880+
// Layout.getDesiredWidth is a full measuring pass over the text, so only run it when its
881+
// result can affect the layout width. It cannot when the width is EXACTLY, nor when the text
882+
// was boring but did not fit: boring.width is the unwrapped width of the same text and we only
883+
// get here if it exceeds floor(width), so min(desiredWidth, floor(width)) is floor(width).
882884
val layoutWidth =
883-
when (widthYogaMeasureMode) {
884-
YogaMeasureMode.EXACTLY -> ceil(width).toInt()
885-
YogaMeasureMode.AT_MOST -> min(desiredWidth, floor(width).toInt())
886-
else -> desiredWidth
885+
when {
886+
widthYogaMeasureMode == YogaMeasureMode.EXACTLY -> ceil(width).toInt()
887+
widthYogaMeasureMode == YogaMeasureMode.AT_MOST && boring != null -> floor(width).toInt()
888+
else -> {
889+
val desiredWidth = ceil(Layout.getDesiredWidth(text, paint)).toInt()
890+
if (widthYogaMeasureMode == YogaMeasureMode.AT_MOST) {
891+
min(desiredWidth, floor(width).toInt())
892+
} else {
893+
desiredWidth
894+
}
895+
}
887896
}
888897
val enableStartOverhang = widthYogaMeasureMode == YogaMeasureMode.EXACTLY
889898
val layout =

0 commit comments

Comments
 (0)