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:
@@ -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),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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<Rect> 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(<String, Object?>{
|
||||
'type': 'fontsChange',
|
||||
}),
|
||||
(_) {},
|
||||
);
|
||||
await pumpFresh(tester);
|
||||
expect(ChatTableCellWidths.measured, greaterThan(measured));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user