From da44001ab2ca6ed36bb02d8f6ac9e4a142cf6596 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sat, 24 Oct 2026 11:12:47 +0100 Subject: [PATCH] perf(chat): reuse unchanged markdown render trees --- .../ui/transcript/chat_markdown_view.dart | 25 +++- .../transcript/chat_markdown_cache_test.dart | 121 ++++++++++++++++++ 2 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 app/test/features/chat/ui/transcript/chat_markdown_cache_test.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 10b29cdc..0dce5a52 100644 --- a/app/lib/features/chat/ui/transcript/chat_markdown_view.dart +++ b/app/lib/features/chat/ui/transcript/chat_markdown_view.dart @@ -32,6 +32,24 @@ Widget chatMarkdownText( class _ChatMarkdownViewState extends State { final Map _links = {}; + Widget? _rendered; + + @override + void didUpdateWidget(ChatMarkdownView oldWidget) { + super.didUpdateWidget(oldWidget); + if (widget.text != oldWidget.text || + (widget.onOpenLink == null) != (oldWidget.onOpenLink == null)) { + _rendered = null; + } + } + + @override + void didChangeDependencies() { + super.didChangeDependencies(); + // Cached spans use host theme/localization roles. Never freeze those when + // brightness, typography, locale or surrounding text styles change. + _rendered = null; + } @override void dispose() { @@ -43,9 +61,12 @@ class _ChatMarkdownViewState extends State { @override Widget build(BuildContext context) { + // A new token or usage update elsewhere in the turn must not reparse and + // rebuild every earlier answer part, or disturb its text selection. + if (_rendered != null) return _rendered!; final cupertino = chatControlsStyleOf(context) == ChatControlsStyle.cupertino; - return _selectable( + return _rendered = _selectable( context, Column( mainAxisSize: MainAxisSize.min, @@ -279,7 +300,7 @@ class _ChatMarkdownViewState extends State { return TextSpan(children: children); } final recognizer = _links.putIfAbsent(url, TapGestureRecognizer.new); - recognizer.onTap = () => onOpenLink(url); + recognizer.onTap = () => widget.onOpenLink?.call(url); return _withRecognizer( TextSpan( style: const TextStyle(decoration: TextDecoration.underline), diff --git a/app/test/features/chat/ui/transcript/chat_markdown_cache_test.dart b/app/test/features/chat/ui/transcript/chat_markdown_cache_test.dart new file mode 100644 index 00000000..897ceb12 --- /dev/null +++ b/app/test/features/chat/ui/transcript/chat_markdown_cache_test.dart @@ -0,0 +1,121 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/gestures.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart'; + +void main() { + testWidgets( + 'unchanged markdown retains its rendered subtree across parent updates', + (tester) async { + final updates = ValueNotifier(0); + addTearDown(updates.dispose); + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: ValueListenableBuilder( + valueListenable: updates, + builder: (_, value, _) => ChatMarkdownView( + text: + '# Kept heading\n\nKept **answer**\n\n```dart\nfinal x = 1;\n```', + ), + ), + ), + ), + ); + final view = find.byType(ChatMarkdownView); + final column = find + .descendant(of: view, matching: find.byType(Column)) + .first; + final rendered = tester.widget(column); + final selection = tester.element(find.byType(SelectionArea)); + updates.value++; + await tester.pump(); + expect(tester.widget(column), same(rendered)); + expect(tester.element(find.byType(SelectionArea)), same(selection)); + }, + ); + + testWidgets('text and inherited theme changes invalidate cached rendering', ( + tester, + ) async { + final text = ValueNotifier('# First'); + final dark = ValueNotifier(false); + addTearDown(text.dispose); + addTearDown(dark.dispose); + await tester.pumpWidget( + ValueListenableBuilder( + valueListenable: dark, + builder: (_, value, _) => MaterialApp( + theme: ThemeData( + brightness: value ? Brightness.dark : Brightness.light, + ), + home: Scaffold( + body: ValueListenableBuilder( + valueListenable: text, + builder: (_, value, _) => ChatMarkdownView(text: value), + ), + ), + ), + ), + ); + final column = find + .descendant( + of: find.byType(ChatMarkdownView), + matching: find.byType(Column), + ) + .first; + final before = tester.widget(column); + text.value = '# Second'; + await tester.pump(); + expect(find.text('Second'), findsOneWidget); + expect(find.text('First'), findsNothing); + expect(tester.widget(column), isNot(same(before))); + final changed = tester.widget(column); + dark.value = true; + await tester.pumpAndSettle(); + expect(tester.widget(column), isNot(same(changed))); + }); + + testWidgets( + 'an unchanged link invokes the latest callback and becomes inert when disabled', + (tester) async { + final version = ValueNotifier(0); + addTearDown(version.dispose); + final opened = []; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: ValueListenableBuilder( + valueListenable: version, + builder: (_, value, _) => ChatMarkdownView( + text: '[Link](https://example.test)', + onOpenLink: value == 2 ? null : (_) => opened.add(value), + ), + ), + ), + ), + ); + GestureRecognizer? recognizer(InlineSpan span) { + if (span is! TextSpan) return null; + if (span.recognizer != null) return span.recognizer; + for (final child in span.children ?? const []) { + final found = recognizer(child); + if (found != null) return found; + } + return null; + } + + TapGestureRecognizer? link() => + recognizer(tester.widget(find.text('Link')).textSpan!) + as TapGestureRecognizer?; + link()!.onTap!(); + version.value = 1; + await tester.pump(); + link()!.onTap!(); + expect(opened, [0, 1]); + version.value = 2; + await tester.pump(); + expect(link(), isNull); + }, + ); +}