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.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
perf(term): cache hot config values until the config changes
RoversXd6c4abe9eb83
diff --git a/config/src/lib.rs b/config/src/lib.rsindex a4cc6ef1..421ec482 100644--- a/config/src/lib.rs+++ b/config/src/lib.rs@@ -659,7 +659,7 @@ impl ConfigInner {self.config = Arc::new(config);self.config_file = loaded_file_name;self.error.take();- self.generation += 1;+ self.bump_generation();// If we loaded a user config, publish this latest version of// the lua state to the LUA_PIPE. This allows a subsequent@@ -689,6 +689,14 @@ impl ConfigInner {}}+ /// Count a change of the effective configuration and publish the new+ /// generation for [`configuration_generation`] before the lock that+ /// installed it is released.+ fn bump_generation(&mut self) {+ self.generation += 1;+ CONFIG_GENERATION.store(self.generation, Ordering::Release);+ }+/// Discard the current configuration and any recorded/// error message; replace them with the default/// configuration@@ -696,14 +704,14 @@ impl ConfigInner {self.config = Arc::new(Config::default_config());self.config_file = None;self.error.take();- self.generation += 1;+ self.bump_generation();}fn use_this_config(&mut self, cfg: Config) {self.config = Arc::new(cfg);self.config_file = None;self.error.take();- self.generation += 1;+ self.bump_generation();}fn overridden(&mut self, overrides: &wezterm_dynamic::Value) -> Result<ConfigHandle, Error> {@@ -732,10 +740,20 @@ impl ConfigInner {config.dpi.replace(96.0);self.config = Arc::new(config);self.error.take();- self.generation += 1;+ self.bump_generation();}}+/// The global configuration's generation, readable without its lock.+static CONFIG_GENERATION: AtomicUsize = AtomicUsize::new(0);++/// The generation of the global configuration, read without taking its+/// lock: every change to what [`configuration`] returns publishes a new+/// value here before that change's lock is released.+pub fn configuration_generation() -> usize {+ CONFIG_GENERATION.load(Ordering::Acquire)+}+pub struct Configuration {inner: Mutex<ConfigInner>,}diff --git a/config/src/terminal.rs b/config/src/terminal.rsindex 242c3f13..8d11d2e3 100644--- a/config/src/terminal.rs+++ b/config/src/terminal.rs@@ -1,6 +1,7 @@//! Bridge our gui config into the terminal crate configurationuse crate::{configuration, ConfigHandle, NewlineCanon};+use std::sync::atomic::{AtomicUsize, Ordering};use std::sync::Mutex;use termwiz::cell::UnicodeVersion;use wezterm_term::color::ColorPalette;@@ -9,6 +10,10 @@ use wezterm_term::config::BidiMode;#[derive(Debug)]pub struct TermConfig {config: Mutex<Option<ConfigHandle>>,+ /// Bumped by every `set_config`. With the global configuration's+ /// generation it forms `change_key`, so the terminal can tell cheaply+ /// whether settings it already read are still current.+ version: AtomicUsize,client_palette: Mutex<Option<ColorPalette>>,/// True while `client_palette` is a scheme being *shown* rather than/// chosen. See [`TermConfig::set_preview_palette`].@@ -19,6 +24,7 @@ impl TermConfig {pub fn new() -> Self {Self {config: Mutex::new(None),+ version: AtomicUsize::new(0),client_palette: Mutex::new(None),client_palette_is_preview: Mutex::new(false),}@@ -27,13 +33,17 @@ impl TermConfig {pub fn with_config(config: ConfigHandle) -> Self {Self {config: Mutex::new(Some(config)),+ version: AtomicUsize::new(0),client_palette: Mutex::new(None),client_palette_is_preview: Mutex::new(false),}}pub fn set_config(&self, config: ConfigHandle) {- self.config.lock().unwrap().replace(config);+ let mut current = self.config.lock().unwrap();+ current.replace(config);+ // Published before the lock is released, like the global generation.+ self.version.fetch_add(1, Ordering::Release);}pub fn set_client_palette(&self, palette: ColorPalette) {@@ -75,6 +85,13 @@ impl wezterm_term::TerminalConfiguration for TermConfig {self.configuration().generation()}+ fn change_key(&self) -> Option<(usize, usize)> {+ Some((+ self.version.load(Ordering::Acquire),+ crate::configuration_generation(),+ ))+ }+fn scrollback_size(&self) -> usize {self.configuration().scrollback_lines}@@ -165,3 +182,21 @@ impl wezterm_term::TerminalConfiguration for TermConfig {}}}++#[cfg(test)]+mod test {+ use super::*;+ use wezterm_term::TerminalConfiguration;++ #[test]+ fn change_key_moves_with_every_set_config() {+ let config = TermConfig::new();+ let before = config.change_key();+ assert!(before.is_some());+ config.set_config(crate::configuration());+ let after = config.change_key();+ assert_ne!(after, before);+ config.set_config(crate::configuration());+ assert_ne!(config.change_key(), after);+ }+}diff --git a/term/src/config.rs b/term/src/config.rsindex d48a4d34..49e2abea 100644--- a/term/src/config.rs+++ b/term/src/config.rs@@ -145,6 +145,14 @@ pub trait TerminalConfiguration: Downcast + std::fmt::Debug + Send + Sync {0}+ /// A cheap value that changes whenever anything these settings are read+ /// from changes. While it stays the same, the terminal may reuse settings+ /// it has read on hot paths instead of asking again. `None`, the+ /// default, means every setting is read afresh whenever it is needed.+ fn change_key(&self) -> Option<(usize, usize)> {+ None+ }+/// Returns the size of the scrollback in terms of the number of rows.fn scrollback_size(&self) -> usize {3500diff --git a/term/src/screen.rs b/term/src/screen.rsindex 5365c1b4..2431daea 100644--- a/term/src/screen.rs+++ b/term/src/screen.rs@@ -31,6 +31,9 @@ pub struct Screen {/// config so we can access Maximum number of lines of scrollbackconfig: Arc<dyn TerminalConfiguration>,+ /// The scrollback size last read from `config`, with the `change_key`+ /// it was read under: every line feed into the history consults it.+ scrollback_size_cache: std::cell::Cell<Option<((usize, usize), usize)>>,/// Whether scrollback is allowed; this is another way of saying/// that we're the primary rather than the alternate screen.@@ -86,6 +89,7 @@ impl Screen {Screen {lines,config: Arc::clone(config),+ scrollback_size_cache: std::cell::Cell::new(None),allow_scrollback,physical_rows,physical_cols,@@ -101,7 +105,20 @@ impl Screen {}fn scrollback_size(&self) -> usize {- scrollback_size(&self.config, self.allow_scrollback)+ if !self.allow_scrollback {+ return 0;+ }+ let Some(key) = self.config.change_key() else {+ return self.config.scrollback_size();+ };+ match self.scrollback_size_cache.get() {+ Some((cached, size)) if cached == key => size,+ _ => {+ let size = self.config.scrollback_size();+ self.scrollback_size_cache.set(Some((key, size)));+ size+ }+ }}/// Every line held, scrollback first, visible rows last.diff --git a/term/src/terminalstate/mod.rs b/term/src/terminalstate/mod.rsindex 5e349b59..bb992c4b 100644--- a/term/src/terminalstate/mod.rs+++ b/term/src/terminalstate/mod.rs@@ -292,9 +292,21 @@ pub struct AgentOscEvidence {pub progress: Option<String>,}+/// Settings read on hot paths (every flushed print run, line feed and+/// erase), kept while the configuration's `change_key` stays the same.+#[derive(Clone, Copy)]+struct HotSettings {+ key: (usize, usize),+ normalize_output_to_unicode_nfc: bool,+ bidi_mode: BidiMode,+ scrollback_size: usize,+}+/// Manages the state for the terminalpub struct TerminalState {config: Arc<dyn TerminalConfiguration>,+ /// See [`HotSettings`]; `None` until first read or after `set_config`.+ hot_settings: std::cell::Cell<Option<HotSettings>>,screen: ScreenOrAlt,/// The current set of attributes in effect for the next@@ -611,6 +623,7 @@ impl TerminalState {TerminalState {config,+ hot_settings: std::cell::Cell::new(None),screen,pen: CellAttributes::default(),cursor: CursorPosition::default(),@@ -698,6 +711,47 @@ impl TerminalState {pub fn set_config(&mut self, config: Arc<dyn TerminalConfiguration>) {self.config = config;+ self.hot_settings.set(None);+ }++ /// The hot-path settings, read again only when the configuration's+ /// `change_key` has moved; `None` for a configuration without one.+ fn hot_settings(&self) -> Option<HotSettings> {+ let key = self.config.change_key()?;+ if let Some(hot) = self.hot_settings.get() {+ if hot.key == key {+ return Some(hot);+ }+ }+ let hot = HotSettings {+ key,+ normalize_output_to_unicode_nfc: self.config.normalize_output_to_unicode_nfc(),+ bidi_mode: self.config.bidi_mode(),+ scrollback_size: self.config.scrollback_size(),+ };+ self.hot_settings.set(Some(hot));+ Some(hot)+ }++ pub(crate) fn config_normalize_output_to_unicode_nfc(&self) -> bool {+ match self.hot_settings() {+ Some(hot) => hot.normalize_output_to_unicode_nfc,+ None => self.config.normalize_output_to_unicode_nfc(),+ }+ }++ fn config_bidi_mode(&self) -> BidiMode {+ match self.hot_settings() {+ Some(hot) => hot.bidi_mode,+ None => self.config.bidi_mode(),+ }+ }++ fn config_scrollback_size(&self) -> usize {+ match self.hot_settings() {+ Some(hot) => hot.scrollback_size,+ None => self.config.scrollback_size(),+ }}pub fn get_config(&self) -> Arc<dyn TerminalConfiguration> {@@ -2299,7 +2353,7 @@ impl TerminalState {}fn get_bidi_mode(&self) -> BidiMode {- let mut mode = self.config.bidi_mode();+ let mut mode = self.config_bidi_mode();if let Some(enabled) = &self.bidi_enabled {mode.enabled = *enabled;}@@ -2473,7 +2527,7 @@ impl TerminalState {// reaches u32::MAX and this loop runs for a minute.let width = (left_and_right_margins.end - left_and_right_margins.start).max(1);let saturated = width- .saturating_mul(self.screen().physical_rows + self.config.scrollback_size());+ .saturating_mul(self.screen().physical_rows + self.config_scrollback_size());let n = n as usize;let n = if n > saturated {saturated + (n % width)diff --git a/term/src/terminalstate/performer.rs b/term/src/terminalstate/performer.rsindex 1a136c0c..444c668c 100644--- a/term/src/terminalstate/performer.rs+++ b/term/src/terminalstate/performer.rs@@ -126,7 +126,7 @@ impl<'a> Performer<'a> {let seqno = self.seqno;let mut p = std::mem::take(&mut self.print);let normalized: String;- let text = if self.config.normalize_output_to_unicode_nfc()+ let text = if self.config_normalize_output_to_unicode_nfc()&& is_nfc_quick(p.chars()) != IsNormalized::Yes{normalized = p.as_str().nfc().collect();diff --git a/term/src/test/mod.rs b/term/src/test/mod.rsindex 2d2f2b9b..4cb6e7e6 100644--- a/term/src/test/mod.rs+++ b/term/src/test/mod.rs@@ -1474,6 +1474,100 @@ fn styled_alt_screen_scrolling_matches_retained_primary_rows() {}}+/// A configuration whose hot-path settings change between batches and which+/// counts every read of them. With a `key` the terminal may cache them.+#[derive(Debug, Default)]+struct SwitchingConfig {+ key: Mutex<Option<(usize, usize)>>,+ nfc: std::sync::atomic::AtomicBool,+ bidi: std::sync::atomic::AtomicBool,+ scrollback: std::sync::atomic::AtomicUsize,+ reads: std::sync::atomic::AtomicUsize,+}++impl TerminalConfiguration for SwitchingConfig {+ fn change_key(&self) -> Option<(usize, usize)> {+ *self.key.lock().unwrap()+ }+ fn scrollback_size(&self) -> usize {+ self.reads.fetch_add(1, std::sync::atomic::Ordering::SeqCst);+ self.scrollback.load(std::sync::atomic::Ordering::SeqCst)+ }+ fn normalize_output_to_unicode_nfc(&self) -> bool {+ self.reads.fetch_add(1, std::sync::atomic::Ordering::SeqCst);+ self.nfc.load(std::sync::atomic::Ordering::SeqCst)+ }+ fn bidi_mode(&self) -> crate::config::BidiMode {+ self.reads.fetch_add(1, std::sync::atomic::Ordering::SeqCst);+ crate::config::BidiMode {+ enabled: self.bidi.load(std::sync::atomic::Ordering::SeqCst),+ hint: wezterm_bidi::ParagraphDirectionHint::LeftToRight,+ }+ }+ fn color_palette(&self) -> ColorPalette {+ ColorPalette::default()+ }+}++#[test]+fn cached_hot_settings_follow_every_config_change() {+ use std::sync::atomic::Ordering::SeqCst;+ let make = |cache: bool| {+ let config = Arc::new(SwitchingConfig::default());+ if cache {+ *config.key.lock().unwrap() = Some((0, 0));+ }+ let term = Terminal::new(+ TerminalSize {+ rows: 4,+ cols: 12,+ pixel_width: 96,+ pixel_height: 64,+ dpi: 0,+ },+ Arc::clone(&config) as Arc<dyn TerminalConfiguration>,+ "ThinkTerm",+ "O_o",+ Box::new(Vec::new()),+ );+ (config, term)+ };+ let (cached_config, mut cached) = make(true);+ let (plain_config, mut plain) = make(false);+ // Each step changes what the hot paths read: NFC for printed runs, bidi+ // for line feeds and erases, the scrollback size for history and REP.+ let steps = [+ (false, false, 3),+ (true, false, 3),+ (true, true, 1),+ (false, true, 5),+ (false, false, 0),+ ];+ let chunk = "e\u{301}x\r\nab\u{5d0}\u{5d1}\r\n\x1b[2Kz\x1b[20b\r\n".repeat(6);+ for (step, &(nfc, bidi, scrollback)) in steps.iter().enumerate() {+ for config in [&cached_config, &plain_config] {+ config.nfc.store(nfc, SeqCst);+ config.bidi.store(bidi, SeqCst);+ config.scrollback.store(scrollback, SeqCst);+ }+ *cached_config.key.lock().unwrap() = Some((step + 1, 0));+ cached.advance_bytes(&chunk);+ plain.advance_bytes(&chunk);+ assert_eq!(+ cached.screen().all_lines(),+ plain.screen().all_lines(),+ "step {step}"+ );+ assert_eq!(cached.cursor_pos(), plain.cursor_pos(), "step {step}");+ }+ let cached_reads = cached_config.reads.load(SeqCst);+ let plain_reads = plain_config.reads.load(SeqCst);+ assert!(+ cached_reads * 4 < plain_reads,+ "settings are reused while the key stays: {cached_reads} vs {plain_reads} reads"+ );+}+#[test]fn perform_actions_in_place_matches_owned_batches() {use wezterm_escape_parser::parser::Parser;