From f4105cb42ffd1a510c9de0b150f6e7b9cfdf7e02 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 13 Mar 2021 08:10:48 -0800 Subject: [PATCH] avoid excess capacity when dealing with iterm2 image protocol Using a boxed slice means that we hold exactly the memory required for the file data, rather than the next-power-of-two, which can be wasteful when a large number of images are being sent to the terminal. This is a API breaking change for termwiz, so bump its version. refs: #534 --- Cargo.lock | 2 +- tabout/Cargo.toml | 2 +- term/Cargo.toml | 2 +- term/src/terminalstate.rs | 4 ++-- termwiz/Cargo.toml | 2 +- termwiz/src/escape/osc.rs | 20 ++++++++++---------- termwiz/src/image.rs | 4 ++-- termwiz/src/render/terminfo.rs | 2 +- termwiz/src/surface/mod.rs | 2 +- wezterm-gui/src/markdown.rs | 4 +++- wezterm-gui/src/termwindow/mod.rs | 4 ++-- wezterm-gui/src/update.rs | 2 +- wezterm/src/main.rs | 2 ++ 13 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dc98ce9fc..80b0f573a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3783,7 +3783,7 @@ dependencies = [ [[package]] name = "termwiz" -version = "0.11.0" +version = "0.12.0" dependencies = [ "anyhow", "base64", diff --git a/tabout/Cargo.toml b/tabout/Cargo.toml index 771c4feb6..eab468b29 100644 --- a/tabout/Cargo.toml +++ b/tabout/Cargo.toml @@ -9,4 +9,4 @@ license = "MIT" documentation = "https://docs.rs/tabout" [dependencies] -termwiz = { path = "../termwiz", version="0.11"} +termwiz = { path = "../termwiz", version="0.12"} diff --git a/term/Cargo.toml b/term/Cargo.toml index 1be424bbc..427078116 100644 --- a/term/Cargo.toml +++ b/term/Cargo.toml @@ -35,5 +35,5 @@ pretty_env_logger = "0.4" k9 = "0.10.0" [dependencies.termwiz] -version = "0.11" +version = "0.12" path = "../termwiz" diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index bbf194d7e..341e9b033 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -1470,12 +1470,12 @@ impl TerminalState { return; } - let image_data = self.raw_image_to_image_data(png_image_data); + let image_data = self.raw_image_to_image_data(png_image_data.into_boxed_slice()); self.assign_image_to_cells(width, height, image_data, false); } /// cache recent images and avoid assigning a new id for repeated data! - fn raw_image_to_image_data(&mut self, raw_data: Vec) -> Arc { + fn raw_image_to_image_data(&mut self, raw_data: Box<[u8]>) -> Arc { use sha2::Digest; let mut hasher = sha2::Sha256::new(); hasher.update(&raw_data); diff --git a/termwiz/Cargo.toml b/termwiz/Cargo.toml index 73a87c0be..5d3940848 100644 --- a/termwiz/Cargo.toml +++ b/termwiz/Cargo.toml @@ -1,7 +1,7 @@ [package] authors = ["Wez Furlong"] name = "termwiz" -version = "0.11.0" +version = "0.12.0" edition = "2018" repository = "https://github.com/wez/wezterm" description = "Terminal Wizardry for Unix and Windows" diff --git a/termwiz/src/escape/osc.rs b/termwiz/src/escape/osc.rs index bfcbd6281..58d814140 100644 --- a/termwiz/src/escape/osc.rs +++ b/termwiz/src/escape/osc.rs @@ -846,7 +846,7 @@ pub struct ITermFileData { /// the users download directory pub inline: bool, /// The data to transfer - pub data: Vec, + pub data: Box<[u8]>, } impl ITermFileData { @@ -874,7 +874,7 @@ impl ITermFileData { let param = if idx == last { // The final argument contains `:base64`, so look for that if let Some(colon) = param.iter().position(|c| *c == b':') { - data = Some(base64::decode(¶m[colon + 1..])?); + data = Some(base64::decode(¶m[colon + 1..])?.into_boxed_slice()); ¶m[..colon] } else { // If we don't find the colon in the last piece, we've @@ -1566,7 +1566,7 @@ mod test { height: ITermDimension::Automatic, preserve_aspect_ratio: true, inline: false, - data: b"hello".to_vec() + data: b"hello".to_vec().into_boxed_slice(), } ))) ); @@ -1584,7 +1584,7 @@ mod test { height: ITermDimension::Automatic, preserve_aspect_ratio: true, inline: false, - data: b"hello".to_vec() + data: b"hello".to_vec().into_boxed_slice(), } ))) ); @@ -1602,7 +1602,7 @@ mod test { height: ITermDimension::Automatic, preserve_aspect_ratio: true, inline: false, - data: b"hello".to_vec() + data: b"hello".to_vec().into_boxed_slice(), } ))) ); @@ -1620,7 +1620,7 @@ mod test { height: ITermDimension::Automatic, preserve_aspect_ratio: true, inline: false, - data: b"hello".to_vec() + data: b"hello".to_vec().into_boxed_slice(), } ))) ); @@ -1643,7 +1643,7 @@ mod test { height: ITermDimension::Automatic, preserve_aspect_ratio: true, inline: false, - data: b"hello".to_vec() + data: b"hello".to_vec().into_boxed_slice(), } ))) ); @@ -1661,7 +1661,7 @@ mod test { height: ITermDimension::Automatic, preserve_aspect_ratio: true, inline: false, - data: b"hello".to_vec() + data: b"hello".to_vec().into_boxed_slice(), } ))) ); @@ -1685,7 +1685,7 @@ mod test { height: ITermDimension::Percent(10), preserve_aspect_ratio: true, inline: false, - data: b"hello".to_vec() + data: b"hello".to_vec().into_boxed_slice(), } ))) ); @@ -1703,7 +1703,7 @@ mod test { height: ITermDimension::Pixels(10), preserve_aspect_ratio: false, inline: true, - data: b"hello".to_vec() + data: b"hello".to_vec().into_boxed_slice(), } ))) ); diff --git a/termwiz/src/image.rs b/termwiz/src/image.rs index 52a6298d3..db541dcaf 100644 --- a/termwiz/src/image.rs +++ b/termwiz/src/image.rs @@ -118,12 +118,12 @@ static IMAGE_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicU pub struct ImageData { id: usize, /// The image data bytes. Data is the native image file format - data: Vec, + data: Box<[u8]>, } impl ImageData { /// Create a new ImageData struct with the provided raw data. - pub fn with_raw_data(data: Vec) -> Self { + pub fn with_raw_data(data: Box<[u8]>) -> Self { let id = IMAGE_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed); Self { id, data } } diff --git a/termwiz/src/render/terminfo.rs b/termwiz/src/render/terminfo.rs index c885b57ba..1d69769af 100644 --- a/termwiz/src/render/terminfo.rs +++ b/termwiz/src/render/terminfo.rs @@ -558,7 +558,7 @@ impl TerminfoRenderer { { // The whole image is requested, so we can send the // original image bytes over - image.image.data().to_vec() + image.image.data().to_vec().into_boxed_slice() } else { // TODO: slice out the requested region of the image, // and encode as a PNG. diff --git a/termwiz/src/surface/mod.rs b/termwiz/src/surface/mod.rs index 46753b0ff..3aba93ef6 100644 --- a/termwiz/src/surface/mod.rs +++ b/termwiz/src/surface/mod.rs @@ -1569,7 +1569,7 @@ mod test { #[test] fn images() { // a dummy image blob with nonsense content - let data = Arc::new(ImageData::with_raw_data(vec![])); + let data = Arc::new(ImageData::with_raw_data(vec![].into_boxed_slice())); let mut s = Surface::new(2, 2); s.add_change(Change::Image(Image { top_left: TextureCoordinate::new_f32(0.0, 0.0), diff --git a/wezterm-gui/src/markdown.rs b/wezterm-gui/src/markdown.rs index ba77667dd..3f9f356bc 100644 --- a/wezterm-gui/src/markdown.rs +++ b/wezterm-gui/src/markdown.rs @@ -170,7 +170,9 @@ impl RenderState { let mut data = vec![]; if let Ok(_len) = f.read_to_end(&mut data) { if let Ok(decoded_image) = image::load_from_memory(&data) { - let image = Arc::new(termwiz::image::ImageData::with_raw_data(data)); + let image = Arc::new(termwiz::image::ImageData::with_raw_data( + data.into_boxed_slice(), + )); let scale = self.wrap_width as f32 / decoded_image.width() as f32; diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index 90d5949a6..d3defc837 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -381,7 +381,7 @@ fn load_background_image(config: &ConfigHandle) -> Option> { Some(p) => match std::fs::read(p) { Ok(data) => { log::error!("loaded {}", p.display()); - Some(Arc::new(ImageData::with_raw_data(data))) + Some(Arc::new(ImageData::with_raw_data(data.into_boxed_slice()))) } Err(err) => { log::error!( @@ -408,7 +408,7 @@ fn reload_background_image( return Some(Arc::clone(existing)); } } - Some(Arc::new(ImageData::with_raw_data(data))) + Some(Arc::new(ImageData::with_raw_data(data.into_boxed_slice()))) } Err(err) => { log::error!( diff --git a/wezterm-gui/src/update.rs b/wezterm-gui/src/update.rs index 1c695121c..05a359bac 100644 --- a/wezterm-gui/src/update.rs +++ b/wezterm-gui/src/update.rs @@ -292,7 +292,7 @@ fn set_banner_from_release_info(latest: &Release) { height: ITermDimension::Cells(2), preserve_aspect_ratio: true, inline: true, - data: ICON_DATA.to_vec(), + data: ICON_DATA.to_vec().into_boxed_slice(), }; let icon = OperatingSystemCommand::ITermProprietary(ITermProprietary::File(Box::new(icon))); let top_line_pos = CSI::Cursor(Cursor::CharacterAndLinePosition { diff --git a/wezterm/src/main.rs b/wezterm/src/main.rs index 733d617ec..95d45afb7 100644 --- a/wezterm/src/main.rs +++ b/wezterm/src/main.rs @@ -168,6 +168,8 @@ impl ImgCatCommand { stdin.read_to_end(&mut data)?; } + let data = data.into_boxed_slice(); + let osc = OperatingSystemCommand::ITermProprietary(ITermProprietary::File(Box::new( ITermFileData { name: None,