summaryrefslogtreecommitdiff
path: root/morphology-img/src/render.rs
diff options
context:
space:
mode:
Diffstat (limited to 'morphology-img/src/render.rs')
-rw-r--r--morphology-img/src/render.rs74
1 files changed, 74 insertions, 0 deletions
diff --git a/morphology-img/src/render.rs b/morphology-img/src/render.rs
index ca61df4..a437a21 100644
--- a/morphology-img/src/render.rs
+++ b/morphology-img/src/render.rs
@@ -188,3 +188,77 @@ pub fn render_ly_files_to_svg(
Ok(svg_paths)
}
+
+/// Renders multiple .ly files to SVGs using LilyPond in parallel.
+///
+/// This function processes multiple LilyPond source files concurrently using rayon,
+/// converting each to an SVG image in the specified output directory. This can
+/// significantly improve performance for large batches on multi-core systems.
+///
+/// # Arguments
+/// - `ly_files`: Paths to the .ly files to render
+/// - `output_dir`: Directory where SVGs should be created
+/// - `lilypond_binary`: Path to the LilyPond binary
+/// - `progress_callback`: Optional callback for progress reporting
+///
+/// # Returns
+/// Vector of paths to generated SVG files, in the same order as input
+///
+/// # Errors
+/// Returns an error if any file fails to render. Processing stops at the first error.
+///
+/// # Performance Notes
+/// - Files are processed in parallel using rayon
+/// - Progress callbacks may be invoked out of order (but still track correct indices)
+/// - Best for batches of 10+ files on multi-core systems
+/// - May be slower than sequential for very small batches due to overhead
+///
+/// # Examples
+/// ```no_run
+/// use morphology_img::render::render_ly_files_to_svg_parallel;
+/// use std::path::{Path, PathBuf};
+///
+/// let ly_files = vec![
+/// PathBuf::from("/tmp/sequence-0.ly"),
+/// PathBuf::from("/tmp/sequence-1.ly"),
+/// ];
+/// let output_dir = Path::new("output");
+/// let lilypond = Path::new("lilypond");
+///
+/// let svg_paths = render_ly_files_to_svg_parallel(&ly_files, output_dir, lilypond, None).unwrap();
+/// println!("Generated {} SVG files", svg_paths.len());
+/// ```
+pub fn render_ly_files_to_svg_parallel(
+ ly_files: &[PathBuf],
+ output_dir: &Path,
+ lilypond_binary: &Path,
+ progress_callback: Option<&crate::ProgressCallback>,
+) -> Result<Vec<PathBuf>, RenderError> {
+ use rayon::prelude::*;
+ use std::sync::atomic::{AtomicUsize, Ordering};
+ use std::sync::Arc;
+
+ let total_count = ly_files.len();
+
+ // Use atomic counter for progress tracking
+ let completed = Arc::new(AtomicUsize::new(0));
+
+ // Process files in parallel
+ let results: Vec<Result<PathBuf, RenderError>> = ly_files
+ .par_iter()
+ .map(|ly_file| {
+ let svg_path = render_ly_to_svg(ly_file, output_dir, lilypond_binary)?;
+
+ // Update progress if callback is provided
+ if let Some(callback) = progress_callback {
+ let current = completed.fetch_add(1, Ordering::SeqCst) + 1;
+ callback(current, total_count);
+ }
+
+ Ok(svg_path)
+ })
+ .collect();
+
+ // Collect results, returning early on first error
+ results.into_iter().collect()
+}