move image decoding to blocking task

This commit is contained in:
Erica Z 2024-11-19 19:15:42 +01:00
parent 79f0e3d978
commit 5032b9d3a2

View file

@ -294,13 +294,19 @@ impl Client {
}; };
return error; return error;
} }
let image = ImageReader::new(std::io::BufReader::new(std::io::Cursor::new( // decoding the image can take up a good chunk of cpu time, so spawn a blocking task
byteresponse.bytes, let image = runtime()
))) .spawn_blocking(move || {
.with_guessed_format() ImageReader::new(std::io::BufReader::new(std::io::Cursor::new(
.map_err(|e| Error::ImageDecodeError(image::ImageError::IoError(e)))? byteresponse.bytes,
.decode() )))
.map_err(Error::ImageDecodeError)?; .with_guessed_format()
.map_err(|e| Error::ImageDecodeError(image::ImageError::IoError(e)))?
.decode()
.map_err(Error::ImageDecodeError)
})
.await
.expect("could not receive image from blocking tokio task")?;
let width = image.width(); let width = image.width();
let height = image.height(); let height = image.height();