paralellize image generator

This commit is contained in:
Michal Vanko 2024-09-05 21:23:28 +02:00
parent 7faafcdda9
commit 0a72d5a203
5 changed files with 33 additions and 10 deletions

View File

@ -22,6 +22,7 @@ tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
image = "0.25.2"
anyhow = "1.0.86"
rayon = "1.10.0"
[build]
rustflags = ["-Z", "threads=8"]

View File

@ -31,6 +31,7 @@ async fn main() {
let app = router::get_router()
.nest_service("/styles", ServeDir::new("styles"))
.nest_service("/images", ServeDir::new("../static/images"))
.nest_service("/generated_images", ServeDir::new("generated_images"))
.nest_service("/svg", ServeDir::new("../static/svg"))
.nest_service(
"/config.yml",

View File

@ -1,6 +1,6 @@
use image::ImageFormat;
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ExportFormat {
Jpeg,
Avif,

View File

@ -1,6 +1,7 @@
use std::{fs::create_dir_all, path::Path};
use image::{imageops::FilterType, DynamicImage};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use tracing::{debug, error};
use super::export_format::ExportFormat;
@ -11,8 +12,8 @@ pub fn generate_images(
resolutions: &[(u32, u32, f32)],
formats: &[ExportFormat],
) -> Result<(), anyhow::Error> {
formats.iter().for_each(|format| {
resolutions.iter().for_each(|resolution| {
formats.par_iter().for_each(|format| {
resolutions.par_iter().for_each(|resolution| {
let (width, height, _) = *resolution;
// let image = image.clone();
let resized = image.resize_to_fill(width, height, FilterType::Triangle);

View File

@ -2,6 +2,7 @@ use std::{
cmp::Ordering,
path::{Path, PathBuf},
str::FromStr as _,
sync::Arc,
};
use anyhow::Context;
@ -30,13 +31,32 @@ pub fn generate_picture_markup(
let resolutions = get_resolutions(orig_img_dimensions, width, height);
// TODO lets generate images
generate_images(
&orig_img,
&path_to_generated,
&resolutions,
&exported_formats,
)
.with_context(|| "Failed to generate images".to_string())?;
let orig_img_arc = Arc::new(orig_img);
let orig_img_clone = Arc::clone(&orig_img_arc);
let path_to_generated_arc = Arc::new(path_to_generated);
let path_to_generated_clone = Arc::clone(&path_to_generated_arc);
let resolutions_arc = Arc::new(resolutions);
let resolutions_clone = Arc::clone(&resolutions_arc);
let exported_formats_arc = Arc::new(exported_formats);
let exported_formats_clone = Arc::clone(&exported_formats_arc);
tokio::spawn(async move {
let orig_img = orig_img_clone.as_ref();
let path_to_generated = path_to_generated_clone.as_ref();
let resolutions = resolutions_clone.as_ref();
let exported_formats = exported_formats_clone.as_ref();
let result = generate_images(orig_img, path_to_generated, resolutions, exported_formats)
.with_context(|| "Failed to generate images".to_string());
if let Err(e) = result {
tracing::error!("Error: {}", e);
}
});
let exported_formats = Arc::clone(&exported_formats_arc);
let path_to_generated = Arc::clone(&path_to_generated_arc);
let resolutions = Arc::clone(&resolutions_arc);
let source_tags = exported_formats
.iter()