From 3295a48f171980dda669b96b2cd5b49c91fdf469 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Tue, 2 Apr 2024 22:22:56 -0700 Subject: [PATCH] Avoid calling `TextPainter.plainText` for simple static text (#146084) `InlineSpan.toPlainText` is surprisingly expensive even for a simple `TextSpan` like `TextSpan(text: 'AAA', children: [TextSpan(text: char * 10), TextSpan(text: char * 20)])` ![image](https://github.com/flutter/flutter/assets/31859944/60014acb-78bd-4dbb-a48d-74295aeb612c) --- packages/flutter/lib/src/painting/text_painter.dart | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/painting/text_painter.dart b/packages/flutter/lib/src/painting/text_painter.dart index 1993c04c68..ae5907dd55 100644 --- a/packages/flutter/lib/src/painting/text_painter.dart +++ b/packages/flutter/lib/src/painting/text_painter.dart @@ -276,10 +276,14 @@ class _UntilTextBoundary extends TextBoundary { } class _TextLayout { - _TextLayout._(this._paragraph, this.writingDirection, this.rawString); + _TextLayout._(this._paragraph, this.writingDirection, this._painter); final TextDirection writingDirection; - final String rawString; + + // Computing plainText is a bit expensive and is currently not needed for + // simple static text. Pass in the entire text painter so `TextPainter.plainText` + // is only called when needed. + final TextPainter _painter; // This field is not final because the owner TextPainter could create a new // ui.Paragraph with the exact same text layout (for example, when only the @@ -340,6 +344,7 @@ class _TextLayout { /// line ended with a line feed. late final _LineCaretMetrics _endOfTextCaretMetrics = _computeEndOfTextCaretAnchorOffset(); _LineCaretMetrics _computeEndOfTextCaretAnchorOffset() { + final String rawString = _painter.plainText; final int lastLineIndex = _paragraph.numberOfLines - 1; assert(lastLineIndex >= 0); final ui.LineMetrics lineMetrics = _paragraph.getLineMetricsAt(lastLineIndex)!; @@ -1189,7 +1194,7 @@ class TextPainter { // called. final ui.Paragraph paragraph = (cachedLayout?.paragraph ?? _createParagraph(text)) ..layout(ui.ParagraphConstraints(width: layoutMaxWidth)); - final _TextLayout layout = _TextLayout._(paragraph, textDirection, plainText); + final _TextLayout layout = _TextLayout._(paragraph, textDirection, this); final double contentWidth = layout._contentWidthFor(minWidth, maxWidth, textWidthBasis); final _TextPainterLayoutCacheWithOffset newLayoutCache;