2889d59119
A socket that outlived a sleep or network change looked open, so every queued request waited its own 30 s timeout behind the last until the page was reopened. The client now replaces a socket whose request times out unheard (or after two timeouts in a row), and probes it with a ping after a quiet spell or on returning to the foreground. The terminal's blinking cursor ran a Ticker (a frame every vsync); a 530 ms timer now flips it. Streamed deltas no longer flood the markdown cache and evict finished answers. Adds tool/perf/idle_session_bench.mjs (idle frames, long-session heap, snapshot census) and notes/perf-hangs-idle.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
62 lines
2.0 KiB
Dart
62 lines
2.0 KiB
Dart
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.
|
|
///
|
|
/// A streamed reply asks for a longer text on every delta. Each new text
|
|
/// replaces the newest entry when that entry is its prefix, so a reply holds
|
|
/// one entry while it streams instead of one per delta: its intermediate
|
|
/// texts would otherwise fill the character budget and evict every finished
|
|
/// answer, which then parse again when scrolled back into view.
|
|
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;
|
|
if (_blocks.isNotEmpty) {
|
|
final newest = _blocks.keys.last;
|
|
if (newest.length < text.length && text.startsWith(newest)) {
|
|
_blocks.remove(newest);
|
|
_chars -= newest.length;
|
|
}
|
|
}
|
|
_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();
|