Rebuild only the live chat turn while replies stream
Reuse unchanged transcript rows, the composer bar and the header across streamed deltas, and share parsed markdown by text so rows scrolled back into view are not parsed again. Adds a rebuild/parse harness and a release-web frame-timing probe with a long-transcript fixture.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import 'dart:collection';
|
||||
|
||||
import 'chat_markdown.dart';
|
||||
|
||||
/// Parsed markdown blocks shared by every markdown view, keyed by the text.
|
||||
///
|
||||
/// A lazily built transcript disposes rows scrolled out of view, so scrolling
|
||||
/// back would parse the same answer again. Blocks are immutable and depend on
|
||||
/// the text alone, so one parse serves every later view of that text. Bounded
|
||||
/// by entries and by held characters, evicting the least recently used first,
|
||||
/// so a long streamed reply's intermediate texts cannot grow it without limit.
|
||||
class ChatMarkdownCache {
|
||||
ChatMarkdownCache({this.maxEntries = 400, this.maxChars = 1000000});
|
||||
|
||||
final int maxEntries;
|
||||
final int maxChars;
|
||||
|
||||
/// Least recently used first.
|
||||
final LinkedHashMap<String, List<MarkdownBlock>> _blocks = LinkedHashMap();
|
||||
int _chars = 0;
|
||||
|
||||
/// How many texts were actually parsed (cache misses).
|
||||
int parses = 0;
|
||||
|
||||
List<MarkdownBlock> blocksFor(String text) {
|
||||
final held = _blocks.remove(text);
|
||||
if (held != null) return _blocks[text] = held;
|
||||
final blocks = List<MarkdownBlock>.unmodifiable(parseMarkdown(text));
|
||||
parses++;
|
||||
if (text.length > maxChars) return blocks;
|
||||
_blocks[text] = blocks;
|
||||
_chars += text.length;
|
||||
while (_blocks.length > maxEntries || _chars > maxChars) {
|
||||
final oldest = _blocks.keys.first;
|
||||
_blocks.remove(oldest);
|
||||
_chars -= oldest.length;
|
||||
}
|
||||
return blocks;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_blocks.clear();
|
||||
_chars = 0;
|
||||
parses = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// The one cache the transcript's markdown views share.
|
||||
final chatMarkdownCache = ChatMarkdownCache();
|
||||
@@ -6,6 +6,7 @@ import 'package:flutter/semantics.dart';
|
||||
import '../../../../ui/adaptive/host.dart';
|
||||
import '../sessions/chat_adaptive.dart';
|
||||
import 'chat_markdown.dart';
|
||||
import 'chat_markdown_cache.dart';
|
||||
import 'chat_message_bubble.dart';
|
||||
|
||||
/// Renders one assistant text part with native text widgets and the host's own
|
||||
@@ -68,7 +69,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final gap = chatParagraphGap(context);
|
||||
final blocks = parseMarkdown(widget.text);
|
||||
final blocks = chatMarkdownCache.blocksFor(widget.text);
|
||||
return _rendered = _selectable(
|
||||
context,
|
||||
Column(
|
||||
|
||||
Reference in New Issue
Block a user