From 7d8cf33b01f8283c49fc7ff49e35e00efff1eb5d Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 22 Sep 2026 07:42:46 +0000 Subject: [PATCH] Parallelize file formatting with Rayon - Use `rayon::prelude::*` to iterate over `files_to_format` in parallel using `par_iter().for_each(...)`. - Use `std::sync::atomic::AtomicUsize` for thread-safe incrementing of `formatted_count`. - Add `rayon` as a dependency in `cli/Cargo.toml`. Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- Cargo.lock | 1 + cli/Cargo.toml | 1 + cli/src/commands/fmt.rs | 18 ++++++++++-------- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 693f55b9..4afda75d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3867,6 +3867,7 @@ dependencies = [ "dirs", "indexmap", "num_cpus", + "rayon", "rustyline", "serde", "serde_json", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 713f027b..7db8f2cf 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -42,6 +42,7 @@ bincode = "1.3.3" dirs = "6.0" num_cpus = "1.15" ureq = { version = "2.9" } +rayon = "1.10.0" [build-dependencies] winres = "0.1.12" diff --git a/cli/src/commands/fmt.rs b/cli/src/commands/fmt.rs index 8cb20444..8e9ab700 100644 --- a/cli/src/commands/fmt.rs +++ b/cli/src/commands/fmt.rs @@ -3,7 +3,9 @@ //! Formats TechScript source code files recursively in the specified path. use crate::exit_code::ExitCode; +use rayon::prelude::*; use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicUsize, Ordering}; pub fn execute(path_str: Option<&str>) -> ExitCode { let current_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")); @@ -41,11 +43,11 @@ pub fn execute(path_str: Option<&str>) -> ExitCode { files_to_format.push(target_path); } - let formatter = techscript_formatter::DocumentFormatter::new(4); - let mut formatted_count = 0; + let formatted_count = AtomicUsize::new(0); - for file in files_to_format { - match std::fs::read_to_string(&file) { + files_to_format.par_iter().for_each(|file| { + let formatter = techscript_formatter::DocumentFormatter::new(4); + match std::fs::read_to_string(file) { Ok(content) => { let formatted = formatter.format_source(&content); // In skeletal phase, if format_source returns empty, we just skip writing to avoid wiping out files. @@ -55,11 +57,11 @@ pub fn execute(path_str: Option<&str>) -> ExitCode { && !formatted.contains("") && !formatted.contains("") { - if let Err(e) = std::fs::write(&file, formatted) { + if let Err(e) = std::fs::write(file, formatted) { eprintln!("Error writing formatted file {:?}: {}", file, e); } else { println!("Formatted: {:?}", file); - formatted_count += 1; + formatted_count.fetch_add(1, Ordering::Relaxed); } } } @@ -67,8 +69,8 @@ pub fn execute(path_str: Option<&str>) -> ExitCode { eprintln!("Error reading file {:?}: {}", file, e); } } - } + }); - println!("Formatted {} files.", formatted_count); + println!("Formatted {} files.", formatted_count.into_inner()); ExitCode::Success }