From 46c070011fbb929719a0bc4317250b8a47d64b53 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sun, 15 Sep 2024 00:37:12 +0800 Subject: [PATCH] perf: avoid unnecessary allocations in base64 encoding of IIP --- yazi-adapter/src/iip.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/yazi-adapter/src/iip.rs b/yazi-adapter/src/iip.rs index a2cfb7164..3d2db5bb2 100644 --- a/yazi-adapter/src/iip.rs +++ b/yazi-adapter/src/iip.rs @@ -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}; @@ -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? }