Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

Reduce memory usage: buffers, quads, and card rebuilds

RoversX204a4de7691b
diff --git a/mux/src/lib.rs b/mux/src/lib.rs
index bb39e961..7a5f6ed3 100644
--- a/mux/src/lib.rs
+++ b/mux/src/lib.rs
@@ -454,6 +454,12 @@ impl Drop for TabGeometryTransaction<'_> {
const BUFSIZE: usize = 1024 * 1024;
+/// Size of the per-pane pty read buffer. This lives for the lifetime of the
+/// pane's reader thread, so it is sized for what a single read() can actually
+/// return (the kernel tty buffer is far smaller than this) rather than
+/// BUFSIZE, which would pin 1MB per pane.
+const PTY_READ_BUFSIZE: usize = 64 * 1024;
+
/// This function applies parsed actions to the pane and notifies any
/// mux subscribers about the output event
fn send_actions_to_mux(pane: &Weak<dyn Pane>, dead: &Arc<AtomicBool>, actions: Vec<Action>) {
@@ -618,7 +624,7 @@ fn read_from_pane_pty(
banner: Option<String>,
mut reader: Box<dyn std::io::Read>,
) {
- let mut buf = vec![0; BUFSIZE];
+ let mut buf = vec![0; PTY_READ_BUFSIZE];
// This is used to signal that an error occurred either in this thread,
// or in the main mux thread. If `true`, this thread will terminate.
diff --git a/wezterm-gui/src/frontend.rs b/wezterm-gui/src/frontend.rs
index 5d789645..9d4adbcd 100644
--- a/wezterm-gui/src/frontend.rs
+++ b/wezterm-gui/src/frontend.rs
@@ -190,6 +190,7 @@ impl GuiFrontEnd {
let connection = Connection::init()?;
connection.set_event_handler(Self::app_event_handler);
crate::native_settings::apply_to_app(&crate::native_settings::load());
+ spawn_malloc_pressure_relief_thread();
let mux = Mux::get();
let client_id = mux.active_identity().expect("to have set my own id");
@@ -968,6 +969,32 @@ pub fn shutdown() {
FRONT_END.with(|f| drop(f.borrow_mut().take()));
}
+/// Hand the allocator's empty-but-dirty pages back to the kernel on a slow
+/// cadence. Rendering churns multi-megabyte buffers (vertex staging growth,
+/// card extraction), and macOS malloc holds the freed pages dirty instead of
+/// returning them -- measured at 300MB of phys footprint that no live object
+/// accounts for. Runs on its own thread so the walk over the zones (a few ms
+/// on a large heap) never lands inside a frame.
+#[cfg(target_os = "macos")]
+fn spawn_malloc_pressure_relief_thread() {
+ extern "C" {
+ fn malloc_zone_pressure_relief(zone: *mut std::ffi::c_void, goal: usize) -> usize;
+ }
+ std::thread::Builder::new()
+ .name("malloc-relief".to_string())
+ .spawn(|| loop {
+ std::thread::sleep(std::time::Duration::from_secs(30));
+ let freed = unsafe { malloc_zone_pressure_relief(std::ptr::null_mut(), 0) };
+ if freed > 0 {
+ log::debug!("malloc_zone_pressure_relief returned {freed} bytes to the kernel");
+ }
+ })
+ .expect("spawn malloc-relief thread");
+}
+
+#[cfg(not(target_os = "macos"))]
+fn spawn_malloc_pressure_relief_thread() {}
+
pub fn try_new() -> Result<Rc<GuiFrontEnd>, Error> {
let front_end = GuiFrontEnd::try_new()?;
FRONT_END.with(|f| *f.borrow_mut() = Some(Rc::clone(&front_end)));
diff --git a/wezterm-gui/src/quad.rs b/wezterm-gui/src/quad.rs
index 0ef6e4e9..19e5aacc 100644
--- a/wezterm-gui/src/quad.rs
+++ b/wezterm-gui/src/quad.rs
@@ -653,23 +653,17 @@ fn bilerp_color(corners: [[f32; 4]; 4], x: f32, y: f32) -> [f32; 4] {
#[derive(Default)]
pub struct HeapQuadAllocator {
- layer0: Vec<Box<BoxedQuad>>,
- layer1: Vec<Box<BoxedQuad>>,
- layer2: Vec<Box<BoxedQuad>>,
- /// Boxes handed back by [`HeapQuadAllocator::recycle`], waiting to be
- /// filled again. Quads are boxed so their addresses survive the vector
- /// growing, which means one allocation each -- fine for a surface recorded
- /// once, ruinous for the overview, which builds a fresh allocator per card
- /// per frame and drops every box at the end of it.
- spare: Vec<Box<BoxedQuad>>,
+ // Quads are stored inline: a cached surface with hundreds of thousands of
+ // quads would otherwise be that many separate 96-byte allocations, which
+ // both fragments the heap and makes every replay a pointer chase. Callers
+ // only ever hold the `QuadImpl` borrow from `allocate` until the next
+ // allocation, so element addresses do not need to survive vector growth.
+ layer0: Vec<BoxedQuad>,
+ layer1: Vec<BoxedQuad>,
+ layer2: Vec<BoxedQuad>,
position_transform: Option<QuadPositionTransform>,
}
-/// Ceiling on the recycling pool. A pool the size of the largest single
-/// surface is the whole point; one the size of the largest surface ever
-/// painted, held forever, is a leak wearing a cache's clothes.
-const MAX_SPARE_QUADS: usize = 65_536;
-
impl std::fmt::Debug for HeapQuadAllocator {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt.debug_struct("HeapQuadAllocator").finish()
@@ -779,20 +773,14 @@ pub struct HeapQuadMark {
}
impl HeapQuadAllocator {
- /// Empty the allocator but keep its boxes for whatever is recorded next.
- ///
- /// For a caller that records many surfaces in a row -- the overview draws
- /// one per visible card, every frame -- this turns a quad's allocation into
- /// a `Vec::pop`. Dropping the allocator instead frees every box, and the
- /// next card immediately allocates them all back.
+ /// Empty the allocator but keep its capacity for whatever is recorded
+ /// next. For a caller that records many surfaces in a row -- the overview
+ /// draws one per visible card, every frame -- this makes re-recording
+ /// allocation-free.
pub fn recycle(&mut self) {
- for layer in [&mut self.layer0, &mut self.layer1, &mut self.layer2] {
- if self.spare.len() + layer.len() <= MAX_SPARE_QUADS {
- self.spare.append(layer);
- } else {
- layer.clear();
- }
- }
+ self.layer0.clear();
+ self.layer1.clear();
+ self.layer2.clear();
self.position_transform = None;
}
@@ -806,7 +794,7 @@ impl HeapQuadAllocator {
}
}
- fn layers(&self) -> [(usize, &Vec<Box<BoxedQuad>>); 3] {
+ fn layers(&self) -> [(usize, &Vec<BoxedQuad>); 3] {
[(0, &self.layer0), (1, &self.layer1), (2, &self.layer2)]
}
@@ -984,13 +972,6 @@ impl HeapQuadAllocator {
impl TripleLayerQuadAllocatorTrait for HeapQuadAllocator {
fn allocate(&mut self, layer_num: usize) -> anyhow::Result<QuadImpl<'_>> {
let position_transform = self.position_transform;
- let fresh = match self.spare.pop() {
- Some(mut quad) => {
- *quad = BoxedQuad::default();
- quad
- }
- None => Box::new(BoxedQuad::default()),
- };
let quads = match layer_num {
0 => &mut self.layer0,
1 => &mut self.layer1,
@@ -998,7 +979,7 @@ impl TripleLayerQuadAllocatorTrait for HeapQuadAllocator {
_ => unreachable!(),
};
- quads.push(fresh);
+ quads.push(BoxedQuad::default());
let quad = quads.last_mut().unwrap();
Ok(match position_transform {
@@ -1033,7 +1014,7 @@ impl TripleLayerQuadAllocatorTrait for HeapQuadAllocator {
let (left, top, right, bottom) = quad.position;
quad.position = transform.map_position(left, top, right, bottom);
}
- dest_quads.push(Box::new(quad));
+ dest_quads.push(quad);
}
}
}
@@ -1331,12 +1312,12 @@ mod translated_clip_tests {
pixel_height: 800,
dpi: 96,
};
- fn quad_at(left: f32, top: f32, right: f32, bottom: f32) -> Box<BoxedQuad> {
+ fn quad_at(left: f32, top: f32, right: f32, bottom: f32) -> BoxedQuad {
let rect = QuadClipRect::from_top_left_pixels(left, top, right, bottom, &WINDOW);
- Box::new(BoxedQuad {
+ BoxedQuad {
position: (rect.left(), rect.top(), rect.right(), rect.bottom()),
..Default::default()
- })
+ }
}
// Left edge of a top-left `x`, in the centre-relative space quads use.
let at = |x: f32| x - WINDOW.pixel_width as f32 / 2.0;
diff --git a/wezterm-gui/src/renderstate.rs b/wezterm-gui/src/renderstate.rs
index e3166447..3053f3bd 100644
--- a/wezterm-gui/src/renderstate.rs
+++ b/wezterm-gui/src/renderstate.rs
@@ -12,7 +12,7 @@ use ::window::glium::{
};
use ::window::*;
use anyhow::Context;
-use std::cell::{Ref, RefCell, RefMut};
+use std::cell::{Cell, Ref, RefCell, RefMut};
use std::convert::TryInto;
use std::rc::Rc;
use wezterm_font::FontConfiguration;
@@ -414,10 +414,19 @@ impl<'a> QuadAllocator for MappedQuads<'a> {
pub struct TripleVertexBuffer {
pub index: RefCell<usize>,
- pub bufs: RefCell<[VertexBuffer; 3]>,
+ /// One buffer for WebGPU, three rotating buffers for Glium. Glium maps
+ /// buffer memory directly, so it rotates to avoid writing into a buffer
+ /// the GPU is still reading; WebGPU uploads go through
+ /// `queue.write_buffer`, which is ordered on the queue timeline, so a
+ /// single buffer is safe and costs a third of the memory.
+ pub bufs: RefCell<Vec<VertexBuffer>>,
pub indices: IndexBuffer,
pub capacity: usize,
pub next_quad: RefCell<usize>,
+ /// Peak quad usage seen in the current shrink observation window.
+ high_water: Cell<usize>,
+ /// Frames observed so far in the current shrink window.
+ frames_observed: Cell<u32>,
}
/// A trait to avoid broadly-scoped transmutes; we only want to
@@ -537,7 +546,13 @@ impl TripleVertexBuffer {
pub fn current_vb_mut(&self) -> RefMut<'static, VertexBuffer> {
let index = *self.index.borrow();
let bufs = self.bufs.borrow_mut();
- unsafe { RefMut::map(bufs, |bufs| &mut bufs[index]).extend_lifetime() }
+ unsafe {
+ RefMut::map(bufs, |bufs| {
+ let len = bufs.len();
+ &mut bufs[index % len]
+ })
+ .extend_lifetime()
+ }
}
pub fn next_index(&self) {
@@ -547,6 +562,28 @@ impl TripleVertexBuffer {
*index = 0;
}
}
+
+ /// Record this frame's quad usage; once usage has stayed well below
+ /// capacity for a whole observation window, return the capacity to
+ /// shrink to. Vertex buffers only ever grew before this, so a single
+ /// spike (opening the overview on a wall of busy panes) pinned tens of
+ /// megabytes of staging + GPU memory forever.
+ fn shrink_target(&self) -> Option<usize> {
+ const OBSERVE_FRAMES: u32 = 120;
+ const MIN_QUADS: usize = 32;
+ let used = *self.next_quad.borrow();
+ self.high_water.set(self.high_water.get().max(used));
+ let frames = self.frames_observed.get() + 1;
+ self.frames_observed.set(frames);
+ if frames < OBSERVE_FRAMES {
+ return None;
+ }
+ let high_water = self.high_water.get();
+ self.frames_observed.set(0);
+ self.high_water.set(0);
+ let target = (high_water.max(MIN_QUADS) * 5 / 4).next_power_of_two();
+ (self.capacity > target * 2).then_some(target)
+ }
}
pub struct RenderLayer {
@@ -658,16 +695,23 @@ impl RenderLayer {
indices.push(idx + V_BOT_RIGHT as u32);
}
+ let copies = match context {
+ RenderContext::Glium(_) => 3,
+ RenderContext::WebGpu(_) => 1,
+ };
+ let mut bufs = Vec::with_capacity(copies);
+ for _ in 0..copies {
+ bufs.push(context.allocate_vertex_buffer(num_quads, &verts)?);
+ }
+
let buffer = TripleVertexBuffer {
index: RefCell::new(0),
- bufs: RefCell::new([
- context.allocate_vertex_buffer(num_quads, &verts)?,
- context.allocate_vertex_buffer(num_quads, &verts)?,
- context.allocate_vertex_buffer(num_quads, &verts)?,
- ]),
+ bufs: RefCell::new(bufs),
capacity: num_quads,
indices: context.allocate_index_buffer(&indices)?,
next_quad: RefCell::new(0),
+ high_water: Cell::new(0),
+ frames_observed: Cell::new(0),
};
Ok(buffer)
@@ -808,6 +852,27 @@ impl RenderState {
Ok(allocated)
}
+ /// Give back vertex buffer memory once a usage spike has passed. Called
+ /// after the frame's draw has been submitted, while `next_quad` still
+ /// holds this frame's usage. WebGPU only: dropping the old wgpu buffer is
+ /// deferred until the GPU is done with it, whereas glium's rotating
+ /// buffers are persistently mapped and are left alone.
+ pub fn maybe_shrink_quads(&self) {
+ if matches!(self.context, RenderContext::Glium(_)) {
+ return;
+ }
+ for layer in self.layers.borrow().iter() {
+ for vb_idx in 0..3 {
+ let target = layer.vb.borrow()[vb_idx].shrink_target();
+ if let Some(target) = target {
+ if let Err(err) = layer.reallocate_quads(vb_idx, target) {
+ log::error!("failed to shrink vertex buffer: {err:#}");
+ }
+ }
+ }
+ }
+ }
+
fn compile_prog(
context: &Rc<GliumContext>,
fragment_shader: fn(&str) -> (String, String),
diff --git a/wezterm-gui/src/settings_window.rs b/wezterm-gui/src/settings_window.rs
index 29e494ce..49c5a1cc 100644
--- a/wezterm-gui/src/settings_window.rs
+++ b/wezterm-gui/src/settings_window.rs
@@ -3599,6 +3599,7 @@ impl SettingsWindow {
composites: Vec::new(),
},
&mut None,
+ &mut Vec::new(),
)?;
Ok(true)
}
diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs
index b1262d7f..8b112596 100644
--- a/wezterm-gui/src/termwindow/mod.rs
+++ b/wezterm-gui/src/termwindow/mod.rs
@@ -1503,9 +1503,16 @@ pub struct TermWindow {
/// wholesale when the glyph atlas is repacked -- the quads hold atlas
/// coordinates.
preview_quad_cache: RefCell<HashMap<TabId, crate::termwindow::render::paint::CachedPreviewQuads>>,
+ /// The one card rebuild currently sliced across frames, if any.
+ preview_rebuild_partial:
+ RefCell<Option<crate::termwindow::render::paint::PreviewRebuildPartial>>,
/// Cards whose heap must be rendered into their texture this frame;
/// queued by the paint pass, encoded by draw before the main pass.
pending_card_renders: RefCell<Vec<crate::termwindow::render::paint::PendingCardRender>>,
+ /// Flattened vertices for every pending card render and composite quad of
+ /// the current frame, in one persistent grow-only buffer. Cleared (not
+ /// dropped) each pass so rebuilds stop allocating megabytes per card.
+ card_frame_verts: RefCell<Vec<crate::quad::Vertex>>,
/// One textured quad per card standing in for its glyph quads.
card_composites: RefCell<Vec<crate::termwindow::render::paint::CardComposite>>,
/// The settled frame's composites, kept so a closing overview's ghost
@@ -2110,6 +2117,8 @@ impl TermWindow {
// braces -- but the two live in different files and the invariant
// belongs with the rest of the captures.
self.preview_quad_cache.borrow_mut().clear();
+ // The half-built card holds the same kind of atlas-addressed quads.
+ *self.preview_rebuild_partial.borrow_mut() = None;
let Some(fade) = self.content_view_fade.as_mut() else {
return;
};
@@ -2455,7 +2464,9 @@ impl TermWindow {
pane_font_cache: RefCell::new(HashMap::new()),
pane_font_cache_tick: Cell::new(0),
preview_quad_cache: RefCell::new(HashMap::new()),
+ preview_rebuild_partial: RefCell::new(None),
pending_card_renders: RefCell::new(Vec::new()),
+ card_frame_verts: RefCell::new(Vec::new()),
card_composites: RefCell::new(Vec::new()),
content_view_last_composites: RefCell::new(Vec::new()),
card_scratch: RefCell::new(None),
diff --git a/wezterm-gui/src/termwindow/render/draw.rs b/wezterm-gui/src/termwindow/render/draw.rs
index 6c401ffa..7d13df04 100644
--- a/wezterm-gui/src/termwindow/render/draw.rs
+++ b/wezterm-gui/src/termwindow/render/draw.rs
@@ -212,6 +212,7 @@ pub(crate) fn draw_webgpu_layers(
window_label: usize,
cards: CardDrawData,
card_scratch: &mut Option<CardScratch>,
+ frame_verts: &mut Vec<crate::quad::Vertex>,
) -> anyhow::Result<()> {
let acquire_start = crate::perf::now();
let output = webgpu.surface.get_current_texture()?;
@@ -272,42 +273,39 @@ pub(crate) fn draw_webgpu_layers(
// One combined vertex upload covers every card render pass and every
// composite quad; each draw slices it with `base_vertex`. The uploads
// must all happen before any pass is encoded because queued buffer
- // writes run at the head of the submit.
+ // writes run at the head of the submit. The pending cards' vertices were
+ // already extracted into `frame_verts` by the paint pass; only the
+ // composite quads are appended here.
let card_pass_start = crate::perf::now();
- let mut card_verts: Vec<crate::quad::Vertex> = Vec::new();
- let mut pending_ranges = Vec::with_capacity(cards.pending.len());
- for pending in &cards.pending {
- let base = card_verts.len();
- card_verts.extend_from_slice(&pending.verts);
- pending_ranges.push((base, pending.verts.len() / 4));
- }
let mut composite_draws: Vec<(i8, i32, &crate::termwindow::render::paint::CardComposite)> =
Vec::with_capacity(cards.composites.len());
for composite in &cards.composites {
if let Some(verts) = composite_quad_verts(composite, &dimensions) {
- let base = card_verts.len();
- card_verts.extend_from_slice(&verts);
+ let base = frame_verts.len();
+ frame_verts.extend_from_slice(&verts);
composite_draws.push((composite.zindex, base as i32, composite));
}
}
- if !card_verts.is_empty() {
- let max_quads = pending_ranges
+ if !frame_verts.is_empty() {
+ let max_quads = cards
+ .pending
.iter()
- .map(|(_, quads)| *quads)
+ .map(|pending| pending.quad_count)
.max()
.unwrap_or(0)
.max(1);
- CardScratch::ensure(card_scratch, webgpu, card_verts.len(), max_quads);
+ CardScratch::ensure(card_scratch, webgpu, frame_verts.len(), max_quads);
let scratch = card_scratch.as_ref().expect("just ensured");
webgpu
.queue
- .write_buffer(&scratch.vb, 0, bytemuck::cast_slice(&card_verts));
+ .write_buffer(&scratch.vb, 0, bytemuck::cast_slice(frame_verts));
// Render each dirty card's quads into its texture. These passes are
// encoded before the main pass, so the composites below sample the
// fresh picture.
- for (pending, (base, quads)) in cards.pending.iter().zip(&pending_ranges) {
- if *quads == 0 {
+ for pending in cards.pending.iter() {
+ let (base, quads) = (pending.first_vertex, pending.quad_count);
+ if quads == 0 {
continue;
}
let half_w = dimensions.pixel_width as f32 / 2.0;
@@ -363,7 +361,7 @@ pub(crate) fn draw_webgpu_layers(
pass.set_bind_group(2, &texture_nearest_bind_group, &[]);
pass.set_vertex_buffer(0, scratch.vb.slice(..));
pass.set_index_buffer(scratch.index.slice(..), wgpu::IndexFormat::Uint32);
- pass.draw_indexed(0..(*quads * 6) as u32, *base as i32, 0..1);
+ pass.draw_indexed(0..(quads * 6) as u32, base as i32, 0..1);
}
}
crate::perf::log_duration("card_texture_passes", card_pass_start);
@@ -575,6 +573,7 @@ impl crate::TermWindow {
composites: std::mem::take(&mut *self.card_composites.borrow_mut()),
};
let mut card_scratch = self.card_scratch.borrow_mut();
+ let mut frame_verts = self.card_frame_verts.borrow_mut();
draw_webgpu_layers(
webgpu,
render_state,
@@ -592,7 +591,10 @@ impl crate::TermWindow {
self.mux_window_id as usize,
cards,
&mut card_scratch,
- )
+ &mut frame_verts,
+ )?;
+ render_state.maybe_shrink_quads();
+ Ok(())
}
fn call_draw_glium(&mut self, frame: &mut glium::Frame) -> anyhow::Result<()> {
diff --git a/wezterm-gui/src/termwindow/render/paint.rs b/wezterm-gui/src/termwindow/render/paint.rs
index cc71634f..1072a380 100644
--- a/wezterm-gui/src/termwindow/render/paint.rs
+++ b/wezterm-gui/src/termwindow/render/paint.rs
@@ -58,6 +58,16 @@ const TAB_BAR_SEAM_HEIGHT: f32 = 1.0;
/// drains at one per frame.
const MAX_PREVIEW_QUAD_REBUILDS_PER_FRAME: usize = 1;
+/// How long one frame may spend rebuilding a card before the rest of the work
+/// is carried over to the next frame.
+///
+/// The one-card budget above bounds *how many* cards rebuild per frame, but
+/// not how big that card is: a full-window card of braille TUI output measured
+/// 29ms at p99 and 71ms at worst -- two to four missed frames for one card.
+/// Slicing inside the card caps the per-frame cost; the partially built card
+/// keeps showing its previous texture until the new picture is complete.
+const PREVIEW_REBUILD_SLICE: Duration = Duration::from_millis(3);
+
/// Everything a card's recorded quads depend on.
///
/// Equal keys mean the recorded heap still draws the right picture and can be
@@ -134,13 +144,35 @@ pub(crate) struct CachedPreviewQuads {
texture: Option<Rc<crate::termwindow::webgpu::CardRenderTexture>>,
}
+/// A card rebuild in flight, sliced across frames.
+///
+/// A single slot rather than a map: the per-frame rebuild budget is one card,
+/// so at most one card is ever mid-rebuild. A different card claiming the
+/// budget simply drops the slot and the interrupted card starts over when its
+/// turn comes back.
+pub(crate) struct PreviewRebuildPartial {
+ tab_id: TabId,
+ key: PreviewQuadKey,
+ /// Keeps `key.snapshot`'s address alive, same as the cache entry.
+ #[allow(dead_code)]
+ snapshot: Arc<crate::termwindow::content_view::TerminalPreviewSnapshot>,
+ heap: HeapQuadAllocator,
+ /// Resume point: the next pane and the next line within it.
+ pane_idx: usize,
+ line_idx: usize,
+}
+
/// A card whose heap must be rendered into its texture this frame, queued by
/// the paint pass and encoded by `draw_webgpu_layers` before the main pass.
pub(crate) struct PendingCardRender {
pub texture: Rc<crate::termwindow::webgpu::CardRenderTexture>,
- /// The card's quads, flattened; positions are window-centre-relative and
- /// the pass's projection maps `area` onto the texture.
- pub verts: Vec<crate::quad::Vertex>,
+ /// Where this card's flattened quads live inside the window's shared
+ /// `card_frame_verts` buffer. A range instead of an owned Vec: extracting
+ /// a large card is a multi-megabyte allocation, and doing that per rebuild
+ /// (plus a per-frame combined copy in draw) was the biggest source of
+ /// malloc large-block churn.
+ pub first_vertex: usize,
+ pub quad_count: usize,
/// The rect (top-left window pixels) the verts were recorded against.
pub area: RectF,
}
@@ -1348,6 +1380,14 @@ impl crate::TermWindow {
self.preview_quad_cache
.borrow_mut()
.retain(|tab_id, _| live.contains(tab_id));
+ // Same policy for a rebuild sliced across frames: its card is gone.
+ let mut partial = self.preview_rebuild_partial.borrow_mut();
+ if partial
+ .as_ref()
+ .is_some_and(|partial| !live.contains(&partial.tab_id))
+ {
+ *partial = None;
+ }
}
let started = crate::perf::now();
crate::perf::reset_accums();
@@ -1476,24 +1516,83 @@ impl crate::TermWindow {
}
*budget = budget.saturating_sub(1);
- // Reuse the stale entry's boxes rather than freeing thousands of them
- // and asking for the same number back on the next line of this
- // function. `recycle` empties the layers into the spare pool.
- let (mut heap, prior_texture) = match self
- .preview_quad_cache
- .borrow_mut()
- .remove(&preview.tab_id)
- {
- Some(cached) => (cached.heap, cached.texture),
- None => (HeapQuadAllocator::default(), None),
+
+ // Continue the sliced rebuild when the slot holds this card and the
+ // key still describes the same picture; anything else in the slot is
+ // stale and a fresh build starts. The old cache entry stays in place
+ // meanwhile -- it is what keeps the card showing its previous picture
+ // until the new one is complete.
+ let mut partial = match self.preview_rebuild_partial.borrow_mut().take() {
+ Some(partial) if partial.tab_id == preview.tab_id && partial.key == key => partial,
+ _ => {
+ // When the old entry's texture can carry the display duty by
+ // itself, take its quad storage: recycling keeps the rebuild
+ // allocation-free instead of freeing thousands of quads and
+ // asking for the same memory back a moment later.
+ let mut heap = HeapQuadAllocator::default();
+ if let Some(cached) = self
+ .preview_quad_cache
+ .borrow_mut()
+ .get_mut(&preview.tab_id)
+ .filter(|cached| cached.texture.is_some())
+ {
+ heap = std::mem::take(&mut cached.heap);
+ heap.recycle();
+ }
+ PreviewRebuildPartial {
+ tab_id: preview.tab_id,
+ key,
+ snapshot: Arc::clone(&preview.snapshot),
+ heap,
+ pane_idx: 0,
+ line_idx: 0,
+ }
+ }
};
- heap.recycle();
- {
+ let finished = {
let card_started = crate::perf::now();
- let mut heap_layers = TripleLayerQuadAllocator::Heap(&mut heap);
- self.paint_terminal_preview_unclipped(&mut heap_layers, preview)?;
+ let deadline = Instant::now() + PREVIEW_REBUILD_SLICE;
+ let mut heap_layers = TripleLayerQuadAllocator::Heap(&mut partial.heap);
+ let finished = self.paint_terminal_preview_unclipped(
+ &mut heap_layers,
+ preview,
+ &mut partial.pane_idx,
+ &mut partial.line_idx,
+ deadline,
+ )?;
crate::perf::log_duration("preview_rebuild_card", card_started);
+ finished
+ };
+ if !finished {
+ // Keep the card's previous picture on screen: its texture
+ // stretches to the current rect even across a geometry change,
+ // and a card that never had one simply stays background until
+ // the first build lands.
+ if !(self.card_texture_path_active() && self.composite_cached_card(preview)?) {
+ let cache = self.preview_quad_cache.borrow();
+ if let Some(cached) = cache.get(&preview.tab_id) {
+ if cached.area == preview.area {
+ cached.heap.apply_to_clipped(layers, clip, 1.0)?;
+ } else {
+ let source = quad_clip_rect(cached.area, &self.dimensions);
+ let target = quad_clip_rect(preview.area, &self.dimensions);
+ cached
+ .heap
+ .apply_to_scaled(layers, source, target, clip, 1.0)?;
+ }
+ }
+ }
+ *self.preview_rebuild_partial.borrow_mut() = Some(partial);
+ // The rest of this card's build should get the very next frame.
+ self.update_next_frame_time(Some(Instant::now()));
+ return Ok(true);
}
+ let PreviewRebuildPartial { heap, .. } = partial;
+ let prior_texture = self
+ .preview_quad_cache
+ .borrow_mut()
+ .remove(&preview.tab_id)
+ .and_then(|cached| cached.texture);
let mut texture = None;
let result = if self.card_texture_path_active() {
// Reuse the previous texture when the card size is unchanged --
@@ -1509,11 +1608,16 @@ impl crate::TermWindow {
};
match tex {
Ok(tex) => {
- let mut verts = Vec::new();
- heap.extract_vertices(&mut verts);
+ let (first_vertex, quad_count) = {
+ let mut frame_verts = self.card_frame_verts.borrow_mut();
+ let base = frame_verts.len();
+ heap.extract_vertices(&mut frame_verts);
+ (base, (frame_verts.len() - base) / 4)
+ };
self.pending_card_renders.borrow_mut().push(PendingCardRender {
texture: Rc::clone(&tex),
- verts,
+ first_vertex,
+ quad_count,
area: preview.area,
});
self.card_composites.borrow_mut().push(CardComposite {
@@ -1593,18 +1697,21 @@ impl crate::TermWindow {
return Ok(false);
}
};
- let mut verts = Vec::new();
- {
+ let (first_vertex, quad_count) = {
let mut cache = self.preview_quad_cache.borrow_mut();
let Some(cached) = cache.get_mut(&preview.tab_id) else {
return Ok(false);
};
- cached.heap.extract_vertices(&mut verts);
+ let mut frame_verts = self.card_frame_verts.borrow_mut();
+ let base = frame_verts.len();
+ cached.heap.extract_vertices(&mut frame_verts);
cached.texture = Some(Rc::clone(&texture));
- }
+ (base, (frame_verts.len() - base) / 4)
+ };
self.pending_card_renders.borrow_mut().push(PendingCardRender {
texture,
- verts,
+ first_vertex,
+ quad_count,
area: recorded_area,
});
}
@@ -1679,18 +1786,27 @@ impl crate::TermWindow {
quantized_scale
}
+ /// Returns whether the card is complete. `false` means the slice deadline
+ /// arrived first: the resume indices point at the next line to render and
+ /// the caller re-enters with the same heap on a later frame. Everything
+ /// outside the line loop is either recomputed idempotently on re-entry
+ /// (scale search, font lookups) or guarded by the resume indices (the
+ /// background fills, which would otherwise be recorded twice).
fn paint_terminal_preview_unclipped(
&mut self,
layers: &mut TripleLayerQuadAllocator<'_>,
preview: &TerminalPreviewRequest,
- ) -> anyhow::Result<()> {
+ resume_pane: &mut usize,
+ resume_line: &mut usize,
+ deadline: Instant,
+ ) -> anyhow::Result<bool> {
let snapshot = &preview.snapshot;
let tab_size = snapshot.tab_size;
if tab_size.cols == 0 || tab_size.rows == 0 {
- return Ok(());
+ return Ok(true);
}
if snapshot.panes.is_empty() {
- return Ok(());
+ return Ok(true);
}
// A thumbnail represents the whole terminal surface, not just the
@@ -1703,7 +1819,9 @@ impl crate::TermWindow {
.or_else(|| snapshot.panes.first())
.map(|pane| pane.palette.resolve_bg(ColorAttribute::Default).to_linear())
.expect("checked that the tab has panes");
- self.filled_rectangle(layers, 0, preview.area, preview_background)?;
+ if *resume_pane == 0 && *resume_line == 0 {
+ self.filled_rectangle(layers, 0, preview.area, preview_background)?;
+ }
// Quantization bounds the number of cached FontConfigurations even
// when cards continuously resize. Flooring guarantees the terminal
@@ -1824,7 +1942,14 @@ impl crate::TermWindow {
let mut hidden_cursor = StableCursorPosition::default();
hidden_cursor.y = isize::MIN;
- for pane in &snapshot.panes {
+ let mut rendered_this_slice = 0usize;
+ for (pane_idx, pane) in snapshot.panes.iter().enumerate().skip(*resume_pane) {
+ // Lines already recorded in an earlier slice of this same build.
+ let start_line = if pane_idx == *resume_pane {
+ *resume_line
+ } else {
+ 0
+ };
let palette = &pane.palette;
let pane_x = origin_x + pane.left as f32 * cell_width;
let pane_y = origin_y + pane.top as f32 * cell_height;
@@ -1834,12 +1959,14 @@ impl crate::TermWindow {
let Some(pane_bounds) = pane_rect.intersection(&preview.area) else {
continue;
};
- self.filled_rectangle(
- layers,
- 0,
- pane_bounds,
- palette.resolve_bg(ColorAttribute::Default).to_linear(),
- )?;
+ if start_line == 0 {
+ self.filled_rectangle(
+ layers,
+ 0,
+ pane_bounds,
+ palette.resolve_bg(ColorAttribute::Default).to_linear(),
+ )?;
+ }
let source_dims = pane.dimensions;
let rows = pane.rows;
@@ -1965,11 +2092,21 @@ impl crate::TermWindow {
let source_visible_bottom =
(pane_bounds.max_y() - grid_top) * rendered_height / grid_height.max(1.0);
- for (line_idx, line) in pane.lines.iter().take(rows).enumerate() {
+ for (line_idx, line) in pane.lines.iter().take(rows).enumerate().skip(start_line) {
let y = line_idx as f32 * pane_cell_height;
if y + pane_cell_height <= source_visible_top || y >= source_visible_bottom {
continue;
}
+ // Out of time: park the resume point at this line. Requiring
+ // one rendered line first guarantees forward progress even
+ // when a single line overruns the whole slice.
+ if rendered_this_slice > 0 && Instant::now() >= deadline {
+ *resume_pane = pane_idx;
+ *resume_line = line_idx;
+ let cleared = layers.set_heap_position_transform(None);
+ debug_assert!(cleared);
+ return Ok(false);
+ }
let hash_started = crate::perf::now();
let shape_hash = self.shape_hash_for_line(line);
crate::perf::accum("line_hash", hash_started);
@@ -2059,6 +2196,7 @@ impl crate::TermWindow {
// may have claimed a blink/expiry deadline of its own, and a
// merge would let the thumbnail keep it.
*self.has_animation.borrow_mut() = next_due;
+ rendered_this_slice += 1;
}
// Draw a non-blinking cursor for the active split. The regular
@@ -2140,7 +2278,7 @@ impl crate::TermWindow {
self.filled_rectangle(layers, 2, visible, split_color)?;
}
}
- Ok(())
+ Ok(true)
}
pub fn paint_modal(&mut self) -> anyhow::Result<()> {
@@ -2639,6 +2777,8 @@ impl crate::TermWindow {
// Card texture work is queued per pass; a retried pass re-queues it.
self.pending_card_renders.borrow_mut().clear();
self.card_composites.borrow_mut().clear();
+ // Truncate, not drop: the buffer's capacity is the whole point.
+ self.card_frame_verts.borrow_mut().clear();
{
let gl_state = self.render_state.as_ref().unwrap();
for layer in gl_state.layers.borrow().iter() {
diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs
index b0b92d78..9eadfdc3 100644
--- a/window/src/os/macos/window.rs
+++ b/window/src/os/macos/window.rs
@@ -284,6 +284,15 @@ unsafe fn set_standard_window_buttons_visible(window: &StrongPtr, visible: bool)
}
pub fn set_application_icon_from_file(path: &Path) -> anyhow::Result<()> {
+ // Loading the icon decodes several MB of NSImage, and every copy handed to
+ // setApplicationIconImage_ stays alive. Every new window applies the icon,
+ // so reloading per call leaks one decoded image per window.
+ static LAST_ICON_PATH: std::sync::Mutex<Option<std::path::PathBuf>> =
+ std::sync::Mutex::new(None);
+ if LAST_ICON_PATH.lock().unwrap().as_deref() == Some(path) {
+ return Ok(());
+ }
+
let path_string = path.to_string_lossy();
unsafe {
@@ -298,6 +307,7 @@ pub fn set_application_icon_from_file(path: &Path) -> anyhow::Result<()> {
app.setApplicationIconImage_(*ns_image);
}
+ *LAST_ICON_PATH.lock().unwrap() = Some(path.to_path_buf());
Ok(())
}