From 914ecb285dd02688fd7f189439de90abae5e3117 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 22 Nov 2026 18:24:11 +0000 Subject: [PATCH] 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 --- .../ui/transcript/chat_markdown_view.dart | 6 +- .../transcript/chat_table_cell_measure.dart | 122 ++++++++++++++++++ app/notes/perf-4k-chat.md | 8 ++ .../chat_markdown_table_wrap_test.dart | 44 +++++++ 4 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 app/lib/features/chat/ui/transcript/chat_table_cell_measure.dart diff --git a/app/lib/features/chat/ui/transcript/chat_markdown_view.dart b/app/lib/features/chat/ui/transcript/chat_markdown_view.dart index 814f758c..144455c0 100644 --- a/app/lib/features/chat/ui/transcript/chat_markdown_view.dart +++ b/app/lib/features/chat/ui/transcript/chat_markdown_view.dart @@ -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 { 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), + ), ), ); } diff --git a/app/lib/features/chat/ui/transcript/chat_table_cell_measure.dart b/app/lib/features/chat/ui/transcript/chat_table_cell_measure.dart new file mode 100644 index 00000000..97b93034 --- /dev/null +++ b/app/lib/features/chat/ui/transcript/chat_table_cell_measure.dart @@ -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 = {}; + 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), + ); + } +} diff --git a/app/notes/perf-4k-chat.md b/app/notes/perf-4k-chat.md index 207cba2f..bdac45f3 100644 --- a/app/notes/perf-4k-chat.md +++ b/app/notes/perf-4k-chat.md @@ -50,6 +50,14 @@ timeline) in the self-time lists are measurement overhead. out for its max width, its min width and then its real width. Caching per cell text, or skipping the min pass when the max widths already fit, cuts it. + Change (2026-11-22): the min pass was already free (RenderParagraph + answers min and max from one infinite-width layout of its intrinsics + painter), so skipping it saves nothing. `chat_table_cell_measure.dart` + now caches each cell's min/max width across render objects, keyed by the + cell's markdown and its paragraph's style, scaler, direction and locale, + cleared on font changes. A table that scrolls back in or a chat opened + again lays each cell out once (its real width) instead of twice; a first + view still measures. Not yet re-benchmarked. 4. **Per-delta transcript rebuild**: `transcriptRows` 41 ms, `withChatShownItems` 28 ms (`_rowTime` 15 ms), `ChatRowKeys.forRows` 16 ms, `sliver_animated_extent` layout 250 ms inclusive over the stream. diff --git a/app/test/features/chat/ui/transcript/chat_markdown_table_wrap_test.dart b/app/test/features/chat/ui/transcript/chat_markdown_table_wrap_test.dart index 94750bb0..73548649 100644 --- a/app/test/features/chat/ui/transcript/chat_markdown_table_wrap_test.dart +++ b/app/test/features/chat/ui/transcript/chat_markdown_table_wrap_test.dart @@ -1,6 +1,8 @@ import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/chat_table_cell_measure.dart'; import 'chat_markdown_test_support.dart'; @@ -51,4 +53,46 @@ void main() { await pumpTable(tester, '| a | b |\n| --- | --- |\n| 1 | 2 |'); expect(tableRect(tester).width, lessThan(screenRight / 2)); }); + + group('cell measurements', () { + const table = + '| Item | Description |\n| --- | --- |\n' + '| one | some longer words that need to wrap inside the cell |\n' + '| two | `code` and **bold** text |'; + + setUp(ChatTableCellWidths.clear); + + Future pumpFresh(WidgetTester tester) async { + // An empty frame first, so the table gets new render objects. + await tester.pumpWidget(const SizedBox.shrink()); + await pumpTable(tester, table); + return tableRect(tester); + } + + testWidgets('a rebuilt table reuses them with the same layout', ( + tester, + ) async { + final first = await pumpFresh(tester); + final measured = ChatTableCellWidths.measured; + // Six cells, each measured for its widest line and longest word. + expect(measured, greaterThanOrEqualTo(12)); + final again = await pumpFresh(tester); + expect(ChatTableCellWidths.measured, measured); + expect(again, first); + }); + + testWidgets('a font change measures again', (tester) async { + await pumpFresh(tester); + final measured = ChatTableCellWidths.measured; + await tester.binding.defaultBinaryMessenger.handlePlatformMessage( + SystemChannels.system.name, + SystemChannels.system.codec.encodeMessage({ + 'type': 'fontsChange', + }), + (_) {}, + ); + await pumpFresh(tester); + expect(ChatTableCellWidths.measured, greaterThan(measured)); + }); + }); }