Skip to content

Commit

Permalink
perf: avoid unnecessary allocations in base64 encoding of IIP (#1639)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi authored Sep 14, 2024
1 parent 0acf5c2 commit 49639aa
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions yazi-adapter/src/iip.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io::Write, path::Path};
use std::{fmt::Write, io::Write as ioWrite, path::Path};

use anyhow::Result;
use base64::{engine::{general_purpose::STANDARD, Config}, Engine};
Expand Down Expand Up @@ -48,20 +48,22 @@ impl Iip {
JpegEncoder::new_with_quality(&mut b, PREVIEW.image_quality).encode_image(&img)?;
};

let len = base64::encoded_len(b.len(), STANDARD.config().encode_padding());
let mut buf = Vec::with_capacity(200 + len.unwrap_or(1 << 16));
let mut buf = String::with_capacity(
200 + base64::encoded_len(b.len(), STANDARD.config().encode_padding()).unwrap_or(0),
);

write!(
buf,
"{}]1337;File=inline=1;size={};width={}px;height={}px;doNotMoveCursor=1:{}\x07{}",
"{}]1337;File=inline=1;size={};width={}px;height={}px;doNotMoveCursor=1:",
START,
b.len(),
w,
h,
STANDARD.encode(b),
CLOSE
)?;
Ok(buf)
STANDARD.encode_string(b, &mut buf);
write!(buf, "\x07{}", CLOSE)?;

Ok(buf.into_bytes())
})
.await?
}
Expand Down

0 comments on commit 49639aa

Please sign in to comment.