дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 01:01:17 +02:00
perf(terminal): stop copying and shifting the scrollback on every output batch (#7840)
The reattach scrollback copied every output batch in full to find a clear marker split across the previous batch, and once 256 KiB were kept it shifted the whole buffer on every batch, under the lock output delivery also takes. Only the marker's length on each side of the boundary is checked now, and the oldest output is dropped 128 KiB at a time (a reattached terminal replays up to 384 KiB). An alternate-screen switch split across two batches is now counted, so a reattached terminal comes back on the right screen. Co-authored-by: yohannlog <[email protected]>
Цей коміт міститься в:
1 джерело
35e4b4cc92
коміт
b21be36bf8
1 file changed
+75
-12
@@ -2,8 +2,10 @@
|
||||
//! restores recent output, alternate-screen state, working directory, title
|
||||
//! and command-running state without gaps.
|
||||
|
||||
/// Maximum scrollback retained per live PTY session (256 KiB).
|
||||
/// Scrollback retained per live PTY session (256 KiB; up to `TRIM_SLACK` more between trims).
|
||||
const MAX_SCROLLBACK_BYTES: usize = 256 * 1024;
|
||||
/// Output let past `MAX_SCROLLBACK_BYTES` before the oldest is dropped.
|
||||
const TRIM_SLACK: usize = 128 * 1024;
|
||||
/// Maximum incomplete OSC sequence buffered across chunks.
|
||||
const MAX_OSC_TAIL: usize = 2048;
|
||||
/// PowerShell / OpenIDEA clear-scrollback OSC marker.
|
||||
@@ -12,6 +14,8 @@ const CLEAR_CSI: &str = "\x1b[3J";
|
||||
|
||||
const ALT_ON: [&str; 3] = ["\x1b[?1049h", "\x1b[?1047h", "\x1b[?47h"];
|
||||
const ALT_OFF: [&str; 3] = ["\x1b[?1049l", "\x1b[?1047l", "\x1b[?47l"];
|
||||
/// The longest alternate-screen switch, `ESC[?1049h`.
|
||||
const ALT_SEQ_MAX: usize = 8;
|
||||
|
||||
pub(crate) struct Scrollback {
|
||||
buf: String,
|
||||
@@ -20,6 +24,8 @@ pub(crate) struct Scrollback {
|
||||
/// Whether the current terminal state (evicted + `buf`) is on the alternate screen.
|
||||
alt_screen: bool,
|
||||
osc_tail: String,
|
||||
/// An escape sequence the last chunk ended in, maybe an alternate-screen switch.
|
||||
alt_tail: String,
|
||||
pub cwd: String,
|
||||
pub app_title: Option<String>,
|
||||
pub command_running: bool,
|
||||
@@ -32,6 +38,7 @@ impl Scrollback {
|
||||
evicted_alt_screen: false,
|
||||
alt_screen: false,
|
||||
osc_tail: String::new(),
|
||||
alt_tail: String::new(),
|
||||
cwd: initial_cwd,
|
||||
app_title: None,
|
||||
command_running: false,
|
||||
@@ -47,7 +54,15 @@ impl Scrollback {
|
||||
return;
|
||||
}
|
||||
self.scan_osc(chunk);
|
||||
// An alternate-screen switch split across chunks: only the bytes around the boundary
|
||||
// are rescanned (a sequence wholly in `chunk` is found below either way).
|
||||
if !self.alt_tail.is_empty() {
|
||||
let mut boundary = std::mem::take(&mut self.alt_tail);
|
||||
boundary.push_str(&chunk[..floor_char_boundary(chunk, ALT_SEQ_MAX - 1)]);
|
||||
self.alt_screen = scan_alt_screen(self.alt_screen, &boundary);
|
||||
}
|
||||
self.alt_screen = scan_alt_screen(self.alt_screen, chunk);
|
||||
self.alt_tail = unfinished_escape(chunk, ALT_SEQ_MAX - 1).to_owned();
|
||||
|
||||
// Honour explicit scrollback clears so `clear` / Cmd+K doesn't replay cleared history.
|
||||
let mut slice = chunk;
|
||||
@@ -56,23 +71,24 @@ impl Scrollback {
|
||||
self.evicted_alt_screen = false;
|
||||
slice = &slice[idx..];
|
||||
} else if !self.buf.is_empty() {
|
||||
// Handle a clear marker split across the boundary between `self.buf` and `chunk`.
|
||||
let tail_start = floor_char_boundary(&self.buf, self.buf.len().saturating_sub(CLEAR_MARKER.len()));
|
||||
let combined = format!("{}{}", &self.buf[tail_start..], slice);
|
||||
if let Some(idx) = last_clear_end(&combined) {
|
||||
// A clear marker split across `self.buf` and `chunk` (one wholly in `chunk` was
|
||||
// handled above): only the marker's length on each side can hold it.
|
||||
let span = CLEAR_MARKER.len() - 1;
|
||||
let tail_start = floor_char_boundary(&self.buf, self.buf.len().saturating_sub(span));
|
||||
let mut boundary = String::with_capacity(2 * span + 4);
|
||||
boundary.push_str(&self.buf[tail_start..]);
|
||||
boundary.push_str(&slice[..floor_char_boundary(slice, span)]);
|
||||
if let Some(idx) = last_clear_end(&boundary) {
|
||||
let tail_len = self.buf.len() - tail_start;
|
||||
self.buf.clear();
|
||||
self.evicted_alt_screen = false;
|
||||
if idx > tail_len {
|
||||
slice = &slice[idx - tail_len..];
|
||||
} else {
|
||||
slice = "";
|
||||
}
|
||||
slice = &slice[idx.saturating_sub(tail_len)..];
|
||||
}
|
||||
}
|
||||
|
||||
self.buf.push_str(slice);
|
||||
if self.buf.len() > MAX_SCROLLBACK_BYTES {
|
||||
// Trimmed by `TRIM_SLACK` at a time: one move per that much output, not per chunk.
|
||||
if self.buf.len() > MAX_SCROLLBACK_BYTES + TRIM_SLACK {
|
||||
let mut cut = self.buf.len() - MAX_SCROLLBACK_BYTES;
|
||||
while cut < self.buf.len() && !self.buf.is_char_boundary(cut) {
|
||||
cut += 1;
|
||||
@@ -188,6 +204,13 @@ fn scan_alt_screen(mut current: bool, text: &str) -> bool {
|
||||
current
|
||||
}
|
||||
|
||||
/// The end of `s` from its last ESC, when that is within its last `max` bytes: an escape
|
||||
/// sequence the next chunk may finish.
|
||||
fn unfinished_escape(s: &str, max: usize) -> &str {
|
||||
let from = floor_char_boundary(s, s.len().saturating_sub(max));
|
||||
s[from..].rfind('\x1b').map_or("", |i| &s[from + i..])
|
||||
}
|
||||
|
||||
fn avoid_mid_escape(s: &str, cut: usize) -> usize {
|
||||
let lookback = floor_char_boundary(s, cut.saturating_sub(16));
|
||||
let Some(esc_rel) = s[lookback..cut].rfind('\x1b') else {
|
||||
@@ -277,7 +300,7 @@ mod tests {
|
||||
let mut sb = Scrollback::new("/".into());
|
||||
sb.push("\x1b[?1049hframe-0\r\n");
|
||||
assert!(sb.is_alt_screen());
|
||||
let filler = "x".repeat(MAX_SCROLLBACK_BYTES + 1024);
|
||||
let filler = "x".repeat(MAX_SCROLLBACK_BYTES + TRIM_SLACK + 1024);
|
||||
sb.push(&filler);
|
||||
let snap = sb.snapshot();
|
||||
assert!(snap.starts_with("\x1b[?1049h"));
|
||||
@@ -294,4 +317,44 @@ mod tests {
|
||||
sb.push("\x1b[2J\x1b[3Jfresh prompt$ ");
|
||||
assert_eq!(sb.snapshot(), "fresh prompt$ ");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alt_screen_switches_split_across_chunks_count() {
|
||||
for split in 1..ALT_SEQ_MAX {
|
||||
let mut sb = Scrollback::new("/".into());
|
||||
let (on, rest) = "\x1b[?1049hvim".split_at(split);
|
||||
sb.push(&format!("$ vim{on}"));
|
||||
sb.push(rest);
|
||||
assert!(sb.is_alt_screen(), "split at {split}");
|
||||
let (off, rest) = "\x1b[?1049l$ ".split_at(split);
|
||||
sb.push(off);
|
||||
sb.push(rest);
|
||||
assert!(!sb.is_alt_screen(), "split at {split}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_marker_split_across_chunks_drops_prior_history() {
|
||||
for marker in [CLEAR_MARKER, CLEAR_CSI] {
|
||||
for split in 1..marker.len() {
|
||||
let mut sb = Scrollback::new("/".into());
|
||||
sb.push("old line\r\n");
|
||||
sb.push(&marker[..split]);
|
||||
sb.push(&format!("{}fresh$ ", &marker[split..]));
|
||||
assert_eq!(sb.snapshot(), "fresh$ ", "{marker:?} split at {split}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retained_output_stays_bounded() {
|
||||
let mut sb = Scrollback::new("/".into());
|
||||
let line = format!("{}\r\n", "é".repeat(100));
|
||||
for _ in 0..10_000 {
|
||||
sb.push(&line);
|
||||
assert!(sb.buf.len() <= MAX_SCROLLBACK_BYTES + TRIM_SLACK);
|
||||
}
|
||||
assert!(sb.buf.len() >= MAX_SCROLLBACK_BYTES);
|
||||
assert!(sb.snapshot().ends_with(&line));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Заблокувати користувача