diff options
Diffstat (limited to 'morphology-img')
| -rw-r--r-- | morphology-img/Cargo.toml | 1 | ||||
| -rw-r--r-- | morphology-img/src/lib.rs | 63 | ||||
| -rw-r--r-- | morphology-img/src/render.rs | 74 | ||||
| -rw-r--r-- | morphology-img/tests/config.rs | 56 | ||||
| -rw-r--r-- | morphology-img/tests/integration.rs | 193 |
5 files changed, 380 insertions, 7 deletions
diff --git a/morphology-img/Cargo.toml b/morphology-img/Cargo.toml index 0c7a734..9454785 100644 --- a/morphology-img/Cargo.toml +++ b/morphology-img/Cargo.toml @@ -9,5 +9,6 @@ morphology = { path = "../morphology" } thiserror = "2.0" itertools = "0.13" tempfile = "3" +rayon = "1.8" [dev-dependencies] 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() +} diff --git a/morphology-img/tests/config.rs b/morphology-img/tests/config.rs index 6302cb0..7cc71cb 100644 --- a/morphology-img/tests/config.rs +++ b/morphology-img/tests/config.rs @@ -240,3 +240,59 @@ fn test_max_batch_size_field_is_public() { let _batch_size = config.max_batch_size; assert_eq!(_batch_size, Some(15)); } + +#[test] +fn test_default_parallel_is_false() { + let config = RenderConfig::default(); + assert!(!config.parallel); +} + +#[test] +fn test_builder_parallel_true() { + let config = RenderConfig::new("output") + .parallel(true); + + assert!(config.parallel); + assert_eq!(config.output_dir, PathBuf::from("output")); +} + +#[test] +fn test_builder_parallel_false() { + let config = RenderConfig::new("output") + .parallel(false); + + assert!(!config.parallel); +} + +#[test] +fn test_builder_parallel_overwrites() { + let config = RenderConfig::new("output") + .parallel(true) + .parallel(false); // Override previous value + + assert!(!config.parallel); +} + +#[test] +fn test_builder_with_all_options_including_parallel() { + let config = RenderConfig::new("my-output") + .keep_temp_files(true) + .lilypond_binary("/usr/bin/lilypond") + .max_batch_size(25) + .parallel(true); + + assert_eq!(config.output_dir, PathBuf::from("my-output")); + assert!(config.keep_temp_files); + assert_eq!(config.lilypond_binary, PathBuf::from("/usr/bin/lilypond")); + assert_eq!(config.max_batch_size, Some(25)); + assert!(config.parallel); +} + +#[test] +fn test_parallel_field_is_public() { + let config = RenderConfig::new("output").parallel(true); + + // Should be able to access field directly + let _parallel = config.parallel; + assert!(_parallel); +} diff --git a/morphology-img/tests/integration.rs b/morphology-img/tests/integration.rs index c8b60fc..423d668 100644 --- a/morphology-img/tests/integration.rs +++ b/morphology-img/tests/integration.rs @@ -720,3 +720,196 @@ fn test_batch_size_one() { assert!(path.exists()); } } + +// ============================================================================ +// Parallel Processing Tests +// ============================================================================ + +#[test] +#[ignore] +fn test_parallel_rendering_basic() { + // Test that parallel rendering works correctly + let sequences: Vec<Sequence> = (0..10) + .map(|i| { + Sequence::new(vec![Value::new(i, 0)]) + }) + .collect(); + + let output_dir = TempDir::new().unwrap(); + let config = RenderConfig::new(output_dir.path()) + .parallel(true); + + let svg_paths = render_sequences(&sequences, &config).unwrap(); + + assert_eq!(svg_paths.len(), 10); + for path in &svg_paths { + assert!(path.exists(), "SVG file should exist: {}", path.display()); + } +} + +#[test] +#[ignore] +fn test_parallel_vs_sequential_same_results() { + // Verify that parallel and sequential produce the same results + let sequences: Vec<Sequence> = (0..5) + .map(|i| { + Sequence::new(vec![Value::new(i, 0), Value::new(i, 1)]) + }) + .collect(); + + // Sequential rendering + let output_dir_seq = TempDir::new().unwrap(); + let config_seq = RenderConfig::new(output_dir_seq.path()) + .parallel(false); + + let svg_paths_seq = render_sequences(&sequences, &config_seq).unwrap(); + + // Parallel rendering + let output_dir_par = TempDir::new().unwrap(); + let config_par = RenderConfig::new(output_dir_par.path()) + .parallel(true); + + let svg_paths_par = render_sequences(&sequences, &config_par).unwrap(); + + // Both should produce same number of files + assert_eq!(svg_paths_seq.len(), svg_paths_par.len()); + + // Both should have files with same names + for (seq_path, par_path) in svg_paths_seq.iter().zip(svg_paths_par.iter()) { + assert_eq!( + seq_path.file_name(), + par_path.file_name(), + "Sequential and parallel should produce same filenames" + ); + assert!(seq_path.exists()); + assert!(par_path.exists()); + } +} + +#[test] +#[ignore] +fn test_parallel_with_progress_callback() { + // Test that progress callbacks work with parallel rendering + let sequences: Vec<Sequence> = (0..8) + .map(|i| { + Sequence::new(vec![Value::new(i, 0)]) + }) + .collect(); + + let progress_calls = Arc::new(Mutex::new(Vec::new())); + let progress_calls_clone = progress_calls.clone(); + + let callback: ProgressCallback = Arc::new(move |current, total| { + progress_calls_clone.lock().unwrap().push((current, total)); + }); + + let output_dir = TempDir::new().unwrap(); + let config = RenderConfig::new(output_dir.path()) + .parallel(true) + .progress_callback(callback); + + let svg_paths = render_sequences(&sequences, &config).unwrap(); + + assert_eq!(svg_paths.len(), 8); + + // Progress callback should have been called 8 times + let calls = progress_calls.lock().unwrap(); + assert_eq!(calls.len(), 8, "Progress callback should be called for each file"); + + // All calls should have total=8 + for (_current, total) in calls.iter() { + assert_eq!(*total, 8, "Total should always be 8"); + } +} + +#[test] +#[ignore] +fn test_parallel_default_is_false() { + let config = RenderConfig::default(); + assert!(!config.parallel, "Parallel should default to false"); + + let config2 = RenderConfig::new("output"); + assert!(!config2.parallel, "Parallel should default to false"); +} + +#[test] +#[ignore] +fn test_parallel_with_batch_chunking() { + // Test parallel rendering combined with batch chunking + let sequences: Vec<Sequence> = (0..15) + .map(|i| { + Sequence::new(vec![Value::new(i, 0)]) + }) + .collect(); + + let output_dir = TempDir::new().unwrap(); + let config = RenderConfig::new(output_dir.path()) + .parallel(true) + .max_batch_size(5); + + let svg_paths = render_sequences(&sequences, &config).unwrap(); + + // All 15 SVGs should be created + assert_eq!(svg_paths.len(), 15); + + // Verify files are in order (sequence-00.svg through sequence-14.svg) + for (i, path) in svg_paths.iter().enumerate() { + let expected_filename = format!("sequence-{:02}.svg", i); + assert_eq!( + path.file_name().unwrap().to_str().unwrap(), + expected_filename, + "Files should be in correct order" + ); + assert!(path.exists()); + } +} + +#[test] +#[ignore] +fn test_benchmark_parallel_vs_sequential() { + use std::time::Instant; + + // Create many sequences for meaningful benchmark + let sequences: Vec<Sequence> = (0..20) + .map(|i| { + Sequence::new(vec![Value::new(i, 0)]) + }) + .collect(); + + // Sequential rendering + let output_dir_seq = TempDir::new().unwrap(); + let config_seq = RenderConfig::new(output_dir_seq.path()) + .parallel(false); + + let start = Instant::now(); + let svg_paths_seq = render_sequences(&sequences, &config_seq).unwrap(); + let sequential_time = start.elapsed(); + + assert_eq!(svg_paths_seq.len(), 20); + + // Parallel rendering + let output_dir_par = TempDir::new().unwrap(); + let config_par = RenderConfig::new(output_dir_par.path()) + .parallel(true); + + let start = Instant::now(); + let svg_paths_par = render_sequences(&sequences, &config_par).unwrap(); + let parallel_time = start.elapsed(); + + assert_eq!(svg_paths_par.len(), 20); + + // Print timing results + println!("\n=== Parallel vs Sequential Benchmark (20 sequences) ==="); + println!("Sequential time: {:?}", sequential_time); + println!("Parallel time: {:?}", parallel_time); + + if parallel_time < sequential_time { + let speedup = sequential_time.as_secs_f64() / parallel_time.as_secs_f64(); + println!("Speedup: {:.2}x faster", speedup); + } else { + println!("Note: Parallel was slower (overhead or system constraints)"); + } + + // No strict assertion on speed - depends on system + // Just verify both completed successfully +} |