diff options
| author | jacksonfu14 <jackson@fashionunited.com> | 2025-11-17 17:21:40 +0100 |
|---|---|---|
| committer | Jackson Moore <jacksonmoore@tuta.io> | 2025-11-17 17:21:40 +0100 |
| commit | 73febb9177d86596522a4b7017ba6c3004826970 (patch) | |
| tree | c722037db060a0ee8d8a76101786e5b1d7b1f224 /morphology-img/src/lib.rs | |
| parent | e11602236c10467bbba4bee6faaabf49265b8f53 (diff) | |
refactor: call lilypond once per batch
Diffstat (limited to 'morphology-img/src/lib.rs')
| -rw-r--r-- | morphology-img/src/lib.rs | 107 |
1 files changed, 6 insertions, 101 deletions
diff --git a/morphology-img/src/lib.rs b/morphology-img/src/lib.rs index 386d8ac..ce33c34 100644 --- a/morphology-img/src/lib.rs +++ b/morphology-img/src/lib.rs @@ -34,25 +34,6 @@ pub use error::RenderError; pub use note::sequence_to_markup; use std::path::PathBuf; -use std::sync::Arc; - -/// Type alias for progress callback function. -/// -/// The callback receives: -/// - `current`: The number of sequences processed so far (1-indexed) -/// - `total`: The total number of sequences to process -/// -/// # Examples -/// -/// ```no_run -/// use morphology_img::ProgressCallback; -/// use std::sync::Arc; -/// -/// let callback: ProgressCallback = Arc::new(|current, total| { -/// println!("Progress: {}/{}", current, total); -/// }); -/// ``` -pub type ProgressCallback = Arc<dyn Fn(usize, usize) + Send + Sync>; /// Configuration for rendering sequences to SVGs. /// @@ -83,21 +64,11 @@ pub struct RenderConfig { /// Path to the LilyPond binary (default: "lilypond") pub lilypond_binary: PathBuf, - /// Optional callback for progress reporting during batch rendering - pub progress_callback: Option<ProgressCallback>, - /// Maximum number of sequences to process in a single batch /// /// 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 { @@ -106,9 +77,7 @@ impl std::fmt::Debug for RenderConfig { .field("output_dir", &self.output_dir) .field("keep_temp_files", &self.keep_temp_files) .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() } } @@ -119,9 +88,7 @@ impl Default for RenderConfig { output_dir: PathBuf::from("output"), keep_temp_files: false, lilypond_binary: PathBuf::from("lilypond"), - progress_callback: None, max_batch_size: None, - parallel: false, } } } @@ -188,27 +155,6 @@ impl RenderConfig { self } - /// Sets a progress callback for batch rendering. - /// - /// The callback will be called after each sequence is rendered, allowing - /// you to track progress for large batches. - /// - /// # Examples - /// - /// ``` - /// use morphology_img::RenderConfig; - /// use std::sync::Arc; - /// - /// let config = RenderConfig::new("output") - /// .progress_callback(Arc::new(|current, total| { - /// println!("Rendered {}/{} sequences", current, total); - /// })); - /// ``` - pub fn progress_callback(mut self, callback: ProgressCallback) -> Self { - self.progress_callback = Some(callback); - self - } - /// Sets the maximum batch size for processing sequences. /// /// When set, large batches of sequences will be split into smaller chunks @@ -229,37 +175,6 @@ 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. @@ -379,22 +294,12 @@ fn render_sequences_batch( total_count, )?; - // 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(), - )? - }; + // Render .ly files to SVGs in a single batch + let svg_paths = render::render_all_ly_files_batch( + &ly_files, + &config.output_dir, + &config.lilypond_binary, + )?; // Handle temp directory cleanup if config.keep_temp_files { |