slightly improve performance by replacing iterators with owning for loops

This commit is contained in:
2025-06-04 00:08:33 +02:00
parent 6d4ffa209a
commit 2f381d540b
5 changed files with 313 additions and 288 deletions

View File

@@ -1,14 +1,17 @@
#![feature(test)]
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use response_deserializer::{ChapterImages, SearchResult};
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::pin::Pin;
use zip::write::{SimpleFileOptions, ZipWriter};
use zip::CompressionMethod;
mod response_deserializer;
mod select;
mod test;
mod util;
use response_deserializer::{Chapter, Id};
@@ -256,7 +259,7 @@ async fn main() {
let zip_file_path = format!("{} - Volume {:0>3}.cbz", title.en, volume);
let zip_file_path = Path::new(&zip_file_path);
let zip_file = File::create(&zip_file_path).unwrap();
let zip_file = File::create(zip_file_path).unwrap();
let mut zip = ZipWriter::new(zip_file);
let options =
@@ -281,7 +284,7 @@ async fn main() {
let zip_file_path = format!("{} - Chapter {:0>3}.cbz", title.en, chapter);
let zip_file_path = Path::new(&zip_file_path);
let zip_file = File::create(&zip_file_path).unwrap();
let zip_file = File::create(zip_file_path).unwrap();
let mut zip = ZipWriter::new(zip_file);
let options =
@@ -403,7 +406,8 @@ async fn select_manga_from_search(
util::CoverSize::W512 => ".512.jpg",
};
let mut entry_futures = Vec::new();
use std::future::Future;
let mut entry_futures: Vec<Pin<Box<dyn Future<Output = Entry>>>> = Vec::new();
for result in results.data.iter() {
let mut entry = Entry::new(result.attributes.title.en.clone());
if let Some(year) = result.attributes.year {
@@ -425,25 +429,29 @@ async fn select_manga_from_search(
if let Some(cover_data) = &result.relationships[2].attributes {
// The lib used for converting to sixel is abysmally slow for larger images, this
// should be in a future to allow for multithreaded work
let future = async move {
let image_url = format!(
"https://uploads.mangadex.org/covers/{id}/{}{cover_ex}",
&cover_data.file_name
);
let data = client
.get(&image_url)
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
let result = util::convert_to_sixel(&data);
if config.cover != Some(false) {
let future = async move {
let image_url = format!(
"https://uploads.mangadex.org/covers/{id}/{}{cover_ex}",
&cover_data.file_name
);
let data = client
.get(&image_url)
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
let result = util::convert_to_sixel(&data);
entry.set_image(result);
entry
};
entry_futures.push(future);
entry.set_image(result);
entry
};
entry_futures.push(Box::pin(future));
} else {
entry_futures.push(Box::pin(async move { entry }));
}
}
}
let entries = futures::future::join_all(entry_futures).await;