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
This commit is contained in:
Wez Furlong committed 2021-03-13 08:10:48 -08:00
1 parent dd5db065dc
commit f4105cb42f
13 files changed
+28 -24

No files matched your search

Generated
+1 -1
View File
@@ -3783,7 +3783,7 @@ dependencies = [
[[package]]
name = "termwiz"
version = "0.11.0"
version = "0.12.0"
dependencies = [
"anyhow",
"base64",
+1 -1
View File
@@ -9,4 +9,4 @@ license = "MIT"
documentation = "https://docs.rs/tabout"
[dependencies]
termwiz = { path = "../termwiz", version="0.11"}
termwiz = { path = "../termwiz", version="0.12"}
+1 -1
View File
@@ -35,5 +35,5 @@ pretty_env_logger = "0.4"
k9 = "0.10.0"
[dependencies.termwiz]
version = "0.11"
version = "0.12"
path = "../termwiz"
+2 -2
View File
@@ -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<u8>) -> Arc<ImageData> {
fn raw_image_to_image_data(&mut self, raw_data: Box<[u8]>) -> Arc<ImageData> {
use sha2::Digest;
let mut hasher = sha2::Sha256::new();
hasher.update(&raw_data);
+1 -1
View File
@@ -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"
+10 -10
View File
@@ -846,7 +846,7 @@ pub struct ITermFileData {
/// the users download directory
pub inline: bool,
/// The data to transfer
pub data: Vec<u8>,
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(&param[colon + 1..])?);
data = Some(base64::decode(&param[colon + 1..])?.into_boxed_slice());
&param[..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(),
}
)))
);
+2 -2
View File
@@ -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<u8>,
data: Box<[u8]>,
}
impl ImageData {
/// Create a new ImageData struct with the provided raw data.
pub fn with_raw_data(data: Vec<u8>) -> 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 }
}
+1 -1
View File
@@ -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.
+1 -1
View File
@@ -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),
+3 -1
View File
@@ -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;
+2 -2
View File
@@ -381,7 +381,7 @@ fn load_background_image(config: &ConfigHandle) -> Option<Arc<ImageData>> {
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!(
+1 -1
View File
@@ -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 {
+2
View File
@@ -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,