diff options
Diffstat (limited to 'morphology-img/src/lib.rs')
| -rw-r--r-- | morphology-img/src/lib.rs | 63 |
1 files changed, 56 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 { |