summaryrefslogtreecommitdiff
path: root/morphology-img/src
diff options
context:
space:
mode:
authorjacksonfu14 <jackson@fashionunited.com>2025-11-17 08:55:46 +0100
committerJackson Moore <jacksonmoore@tuta.io>2025-11-17 08:55:46 +0100
commit7087c4f7da3749f12a2daa1e1de1c7f947e91638 (patch)
tree99011c9b8a936076396733e5145ec241f53def7d /morphology-img/src
parente37f7ba671f3e80396e7a4ac3a461365bd0ca78a (diff)
feature: ly parallel processing
Diffstat (limited to 'morphology-img/src')
-rw-r--r--morphology-img/src/lib.rs63
-rw-r--r--morphology-img/src/render.rs74
2 files changed, 130 insertions, 7 deletions
diff --git a/morphology-img/src/lib.rs b/morphology-img/src/lib.rs
index 7d0825f..386d8ac 100644
--- a/morphology-img/src/lib.rs
+++ b/morphology-img/src/lib.rs
@@ -91,6 +91,13 @@ pub struct RenderConfig {
/// If set, large batches will be split into smaller chunks to avoid
/// filesystem issues. If None, all sequences are processed in one batch.
pub max_batch_size: Option<usize>,
+
+ /// Whether to use parallel processing for rendering SVGs
+ ///
+ /// When enabled, multiple LilyPond processes will run concurrently using rayon.
+ /// This can significantly improve performance for large batches on multi-core systems.
+ /// Default: false (conservative default to avoid overwhelming the system).
+ pub parallel: bool,
}
impl std::fmt::Debug for RenderConfig {
@@ -101,6 +108,7 @@ impl std::fmt::Debug for RenderConfig {
.field("lilypond_binary", &self.lilypond_binary)
.field("progress_callback", &self.progress_callback.as_ref().map(|_| "<callback>"))
.field("max_batch_size", &self.max_batch_size)
+ .field("parallel", &self.parallel)
.finish()
}
}
@@ -113,6 +121,7 @@ impl Default for RenderConfig {
lilypond_binary: PathBuf::from("lilypond"),
progress_callback: None,
max_batch_size: None,
+ parallel: false,
}
}
}
@@ -220,6 +229,37 @@ impl RenderConfig {
self.max_batch_size = Some(size);
self
}
+
+ /// Enables or disables parallel processing for SVG rendering.
+ ///
+ /// When enabled, LilyPond will be invoked in parallel using rayon,
+ /// which can significantly improve performance for large batches on
+ /// multi-core systems. The default is false (sequential processing).
+ ///
+ /// # Performance Considerations
+ ///
+ /// Parallel processing is most beneficial when:
+ /// - Rendering many sequences (typically 10+)
+ /// - Running on a multi-core system
+ /// - LilyPond execution is the bottleneck (not file I/O)
+ ///
+ /// For small batches (< 10 sequences), sequential processing may be faster
+ /// due to the overhead of spawning parallel tasks.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use morphology_img::RenderConfig;
+ ///
+ /// let config = RenderConfig::new("output")
+ /// .parallel(true);
+ ///
+ /// assert!(config.parallel);
+ /// ```
+ pub fn parallel(mut self, parallel: bool) -> Self {
+ self.parallel = parallel;
+ self
+ }
}
/// Renders multiple sequences to SVG files.
@@ -339,13 +379,22 @@ fn render_sequences_batch(
total_count,
)?;
- // Render .ly files to SVGs
- let svg_paths = render::render_ly_files_to_svg(
- &ly_files,
- &config.output_dir,
- &config.lilypond_binary,
- config.progress_callback.as_ref(),
- )?;
+ // Render .ly files to SVGs (parallel or sequential based on config)
+ let svg_paths = if config.parallel {
+ render::render_ly_files_to_svg_parallel(
+ &ly_files,
+ &config.output_dir,
+ &config.lilypond_binary,
+ config.progress_callback.as_ref(),
+ )?
+ } else {
+ render::render_ly_files_to_svg(
+ &ly_files,
+ &config.output_dir,
+ &config.lilypond_binary,
+ config.progress_callback.as_ref(),
+ )?
+ };
// Handle temp directory cleanup
if config.keep_temp_files {
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()
+}