Reuse chat table cell measurements across rebuilds

A markdown table measures each cell's paragraph at infinite width before
laying it out. Cells rebuilt when a row scrolls back in or a chat is opened
again now reuse those widths from a bounded cache keyed by the cell markdown
and its paragraph style, cleared when fonts change. Layout is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 18:24:11 +00:00
parent 58c72538d5
commit 914ecb285d
4 changed files with 179 additions and 1 deletions
@@ -13,6 +13,7 @@ import 'chat_markdown_cache.dart';
import 'chat_markdown_speech.dart';
import 'chat_message_bubble.dart';
import '../../speech/speech.dart';
import 'chat_table_cell_measure.dart';
import 'chat_table_column_width.dart';
/// Renders one assistant text part with native text widgets and the host's own
@@ -305,7 +306,10 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
role: header ? SemanticsRole.columnHeader : null,
child: Padding(
padding: const EdgeInsets.all(8),
child: _text(context, text, style: style),
child: ChatTableCellMeasure(
source: text,
child: _text(context, text, style: style),
),
),
);
}
@@ -0,0 +1,122 @@
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
/// Remembers a markdown table cell's intrinsic widths across rebuilds.
///
/// A [Table] measures every cell's widest line and longest word before it
/// lays the cell out, which lays the paragraph out a second time. A transcript
/// row that scrolls back in, or a chat opened again, rebuilds the same cells
/// with fresh render objects, so those measurements are kept here, keyed by
/// the cell's markdown [source] and everything its paragraph lays out with.
/// The cached value is the one the paragraph itself reported, so the layout is
/// unchanged; only the repeated measuring is skipped.
class ChatTableCellMeasure extends SingleChildRenderObjectWidget {
const ChatTableCellMeasure({
super.key,
required this.source,
required super.child,
});
/// The cell's markdown text, which determines its spans and their styles.
final String source;
@override
RenderObject createRenderObject(BuildContext context) =>
RenderChatTableCellMeasure(source);
@override
void updateRenderObject(
BuildContext context,
RenderChatTableCellMeasure renderObject,
) => renderObject.source = source;
}
/// The shared measurements, bounded and dropped when fonts change.
abstract final class ChatTableCellWidths {
static const _capacity = 2048;
// Insertion-ordered, so the first key is the least recently used.
static final _widths = <Object, double>{};
static bool _listening = false;
/// Intrinsic widths computed by laying a paragraph out (cache misses).
@visibleForTesting
static int measured = 0;
@visibleForTesting
static void clear() => _widths.clear();
static double lookup(Object key, double Function() measure) {
if (!_listening) {
_listening = true;
// A web font that finishes loading changes every width.
PaintingBinding.instance.systemFonts.addListener(clear);
}
final cached = _widths.remove(key);
if (cached != null) return _widths[key] = cached;
measured++;
final width = measure();
_widths[key] = width;
if (_widths.length > _capacity) _widths.remove(_widths.keys.first);
return width;
}
}
/// Answers intrinsic width queries from [ChatTableCellWidths].
class RenderChatTableCellMeasure extends RenderProxyBox {
RenderChatTableCellMeasure(this._source);
String _source;
set source(String value) {
if (value == _source) return;
_source = value;
markNeedsLayout();
}
/// The paragraph below the cell's pass-through wrappers, or null when the
/// cell holds anything whose width the key would not describe.
RenderParagraph? get _paragraph {
RenderObject? node = child;
while (node is RenderMouseRegion || node is RenderSemanticsAnnotations) {
node = (node as RenderProxyBox).child;
}
return node is RenderParagraph ? node : null;
}
Object? _key(bool max, double height) {
final paragraph = _paragraph;
if (paragraph == null) return null;
return (
max,
height,
_source,
paragraph.text.style,
paragraph.textScaler,
paragraph.textDirection,
paragraph.locale,
paragraph.strutStyle,
paragraph.textWidthBasis,
paragraph.textHeightBehavior,
paragraph.maxLines,
);
}
@override
double computeMinIntrinsicWidth(double height) {
final key = _key(false, height);
if (key == null) return super.computeMinIntrinsicWidth(height);
return ChatTableCellWidths.lookup(
key,
() => super.computeMinIntrinsicWidth(height),
);
}
@override
double computeMaxIntrinsicWidth(double height) {
final key = _key(true, height);
if (key == null) return super.computeMaxIntrinsicWidth(height);
return ChatTableCellWidths.lookup(
key,
() => super.computeMaxIntrinsicWidth(height),
);
}
}