mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
Size the transcript layout cache to the viewport (#64882)
## Why On tall terminals, painting the transcript and measuring the preceding screenful for pagination can exceed the fixed 64-entry cache. Hidden reasoning entries in compact mode also take up cache slots, which can force visible text to be laid out again on the next frame. ## What changed Set the entry limit to twice the viewport height plus two, clamped to 64–512 entries, and trim the cache when the viewport shrinks. Keep the existing byte budget and skip caching hidden reasoning in compact mode. ## Testing Cover eviction after shrinking the viewport and verify that repeated frames reuse visible layouts in an 88-row viewport with partial history and 100 hidden reasoning entries. GitOrigin-RevId: 194f39e3ad1eb72a223481c600e95463a5674fc4
This commit is contained in:
@@ -237,7 +237,7 @@ impl TranscriptView {
|
||||
self.area.height + u16::from(self.prompt_header_target.is_some());
|
||||
self.composer_tip = None;
|
||||
self.prompt_header_target = None;
|
||||
self.cache.begin_frame();
|
||||
self.cache.begin_frame(area.height);
|
||||
self.sync_history_tail(cells);
|
||||
let current_cells = cells;
|
||||
let snapshot = self.snapshot_cells();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Width-specific layouts retained only for recently displayed conversation entries.
|
||||
//! Width-specific layouts for the visible transcript and nearby history.
|
||||
//!
|
||||
//! Mutable cells refresh each frame, then share that frame's layout across measurement and paint.
|
||||
//! Stable cells also invalidate when animation ticks, syntax themes, or terminal colors change.
|
||||
@@ -10,7 +10,9 @@ use std::sync::Weak;
|
||||
|
||||
use super::*;
|
||||
|
||||
const MAX_CACHED_ENTRIES: usize = 64;
|
||||
const MIN_ENTRY_LIMIT: usize = 64;
|
||||
// Bound the work of the linear lookup even for exceptionally tall viewports.
|
||||
const MAX_ENTRY_LIMIT: usize = 512;
|
||||
const MAX_CACHED_LAYOUT_BYTES: usize = 8 * 1024 * 1024;
|
||||
|
||||
/// The selected activity presentation and any live content that follows it.
|
||||
@@ -119,6 +121,7 @@ fn is_rich_user_prompt(cell: &dyn HistoryCell, mode: HistoryRenderMode) -> bool
|
||||
pub(super) struct LayoutCache {
|
||||
pub(super) images: markdown_images::MarkdownImages,
|
||||
entries: Vec<CachedLayout>,
|
||||
viewport_height: u16,
|
||||
document_resize_preview: bool,
|
||||
frame: u64,
|
||||
render_state: Option<RenderState>,
|
||||
@@ -356,7 +359,9 @@ impl LayoutCache {
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn begin_frame(&mut self) {
|
||||
pub(super) fn begin_frame(&mut self, viewport_height: u16) {
|
||||
self.viewport_height = viewport_height;
|
||||
self.trim_to_viewport();
|
||||
self.frame = self.frame.wrapping_add(/*rhs*/ 1);
|
||||
let state = RenderState {
|
||||
theme: crate::render::highlight::syntax_theme_revision(),
|
||||
@@ -382,6 +387,11 @@ impl LayoutCache {
|
||||
presentation: CellPresentation,
|
||||
render: impl FnOnce() -> TextLayout,
|
||||
) -> Arc<TextLayout> {
|
||||
// Transcript-only reasoning takes no rows in compact mode. A run of
|
||||
// these entries should not displace layouts for the surrounding text.
|
||||
if !presentation.detailed && crate::thread_transcript::is_hidden_activity_detail(cell) {
|
||||
return Arc::new(TextLayout::new(Vec::new(), width));
|
||||
}
|
||||
let source = Arc::downgrade(cell);
|
||||
let animation_tick = cell.transcript_animation_tick();
|
||||
if let Some(index) = self.entries.iter().position(|entry| {
|
||||
@@ -473,7 +483,18 @@ impl LayoutCache {
|
||||
self.entries.clear();
|
||||
}
|
||||
|
||||
fn trim_to_viewport(&mut self) {
|
||||
// Painting and pagination each visit a screenful. The paging check reads
|
||||
// one entry past its threshold, and painting also checks the latest entry.
|
||||
let limit =
|
||||
(2 * usize::from(self.viewport_height) + 2).clamp(MIN_ENTRY_LIMIT, MAX_ENTRY_LIMIT);
|
||||
if self.entries.len() > limit {
|
||||
self.entries.drain(..self.entries.len() - limit);
|
||||
}
|
||||
}
|
||||
|
||||
fn evict(&mut self) {
|
||||
self.trim_to_viewport();
|
||||
let retained_bytes = |entry: &CachedLayout| {
|
||||
entry.layout.text().len()
|
||||
+ entry.layout.mapping_bytes
|
||||
@@ -486,9 +507,7 @@ impl LayoutCache {
|
||||
};
|
||||
let mut bytes = self.entries.iter().map(retained_bytes).sum::<usize>();
|
||||
// Retain a single oversized entry rather than repeatedly laying it out while visible.
|
||||
while self.entries.len() > 1
|
||||
&& (self.entries.len() > MAX_CACHED_ENTRIES || bytes > MAX_CACHED_LAYOUT_BYTES)
|
||||
{
|
||||
while self.entries.len() > 1 && bytes > MAX_CACHED_LAYOUT_BYTES {
|
||||
bytes -= retained_bytes(&self.entries.remove(/*index*/ 0));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,18 +59,18 @@ fn layouts_refresh_for_width_animation_and_mutable_frames() {
|
||||
|| TextLayout::new(history.transcript_hyperlink_lines(width), width),
|
||||
)
|
||||
};
|
||||
cache.begin_frame();
|
||||
cache.begin_frame(/*viewport_height*/ 24);
|
||||
get(&mut cache, /*width*/ 20);
|
||||
get(&mut cache, /*width*/ 20);
|
||||
assert_eq!(cell.renders.load(Ordering::Relaxed), 1);
|
||||
cache.begin_frame();
|
||||
cache.begin_frame(/*viewport_height*/ 24);
|
||||
get(&mut cache, /*width*/ 20);
|
||||
assert_eq!(
|
||||
cell.renders.load(Ordering::Relaxed),
|
||||
1 + usize::from(mutable)
|
||||
);
|
||||
cell.tick.store(/*val*/ 2, Ordering::Relaxed);
|
||||
cache.begin_frame();
|
||||
cache.begin_frame(/*viewport_height*/ 24);
|
||||
let next = get(&mut cache, /*width*/ 20);
|
||||
assert_eq!(next.text(), "tick 2");
|
||||
get(&mut cache, /*width*/ 10);
|
||||
@@ -96,7 +96,7 @@ fn layouts_refresh_for_width_animation_and_mutable_frames() {
|
||||
bg: (65, 43, 21),
|
||||
},
|
||||
|| {
|
||||
cache.begin_frame();
|
||||
cache.begin_frame(/*viewport_height*/ 24);
|
||||
get(&mut cache, /*width*/ 10);
|
||||
assert_eq!(
|
||||
cell.renders.load(Ordering::Relaxed),
|
||||
@@ -111,7 +111,7 @@ fn layouts_refresh_for_width_animation_and_mutable_frames() {
|
||||
fn recent_entries_are_reused_and_old_entries_are_evicted() {
|
||||
let cells: Vec<_> = (0..100).map(|_| Arc::new(Cell::default())).collect();
|
||||
let mut cache = LayoutCache::default();
|
||||
cache.begin_frame();
|
||||
cache.begin_frame(/*viewport_height*/ 88);
|
||||
for cell in &cells {
|
||||
let history = cell.clone() as Arc<dyn HistoryCell>;
|
||||
cache.get(
|
||||
@@ -136,6 +136,7 @@ fn recent_entries_are_reused_and_old_entries_are_evicted() {
|
||||
},
|
||||
);
|
||||
}
|
||||
cache.begin_frame(/*viewport_height*/ 24);
|
||||
for index in [99, 98, 0] {
|
||||
let history = cells[index].clone() as Arc<dyn HistoryCell>;
|
||||
cache.get(
|
||||
|
||||
@@ -926,7 +926,7 @@ fn wheel_scrolling_pauses_selection_autoscroll_until_the_next_drag() {
|
||||
#[test]
|
||||
fn only_visible_history_is_laid_out_and_repeated_frames_reuse_it() {
|
||||
let renders = Arc::new(AtomicUsize::new(/*v*/ 0));
|
||||
let cells = (0..10_000)
|
||||
let mut cells = (0..10_000)
|
||||
.map(|index| {
|
||||
Arc::new(TestCell {
|
||||
text: format!("message {index}"),
|
||||
@@ -948,6 +948,23 @@ fn only_visible_history_is_laid_out_and_repeated_frames_reuse_it() {
|
||||
render(&mut view, &cells, /*width*/ 40, /*height*/ 20);
|
||||
assert_eq!(renders.load(Ordering::Relaxed), initial);
|
||||
}
|
||||
|
||||
// With older pages available, the app measures the preceding screenful after painting.
|
||||
view.history = TranscriptHistoryState::Partial;
|
||||
cells.extend((0..100).map(|_| {
|
||||
Arc::new(crate::history_cell::ReasoningSummaryCell::new(
|
||||
String::new(),
|
||||
"hidden detail".into(),
|
||||
std::path::Path::new("."),
|
||||
/*transcript_only*/ true,
|
||||
)) as Arc<dyn HistoryCell>
|
||||
}));
|
||||
render(&mut view, &cells, /*width*/ 40, /*height*/ 88);
|
||||
assert!(!view.needs_history(&cells));
|
||||
let warmed = renders.load(Ordering::Relaxed);
|
||||
render(&mut view, &cells, /*width*/ 40, /*height*/ 88);
|
||||
assert!(!view.needs_history(&cells));
|
||||
assert_eq!(renders.load(Ordering::Relaxed), warmed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user