Skip to content

Commit 4b2789b

Browse files
committed
feat: parallelize imports patching
1 parent 7183a84 commit 4b2789b

1 file changed

Lines changed: 32 additions & 21 deletions

File tree

go-runner/src/builder/patcher.rs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//! Patches the imports to use codspeed rather than the official "testing" package.
22
33
use crate::prelude::*;
4+
use rayon::prelude::*;
45
use std::fs;
56
use std::path::Path;
67
use std::process::Command;
8+
use std::sync::atomic::AtomicU64;
79

810
pub fn replace_pkg<P: AsRef<Path>>(folder: P) -> anyhow::Result<()> {
911
let codspeed_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
@@ -34,28 +36,37 @@ pub fn patch_imports<P: AsRef<Path>>(folder: P) -> anyhow::Result<()> {
3436
debug!("Patching imports in folder: {folder:?}");
3537

3638
// 1. Find all imports that match "testing" and replace them with codspeed equivalent
37-
let mut patched_files = 0;
38-
39+
let patched_files = AtomicU64::new(0);
3940
let pattern = folder.join("**/*.go");
40-
for go_file in glob::glob(pattern.to_str().unwrap())?.filter_map(Result::ok) {
41-
// Skip directories - glob can match directories ending in .go (e.g., vendor/github.com/nats-io/nats.go)
42-
if !go_file.is_file() {
43-
continue;
44-
}
45-
46-
let content =
47-
fs::read_to_string(&go_file).context(format!("Failed to read Go file: {go_file:?}"))?;
48-
49-
let patched_content = patch_imports_for_source(&content);
50-
if patched_content != content {
51-
fs::write(&go_file, patched_content)
52-
.context(format!("Failed to write patched Go file: {go_file:?}"))?;
53-
54-
debug!("Patched imports in: {go_file:?}");
55-
patched_files += 1;
56-
}
57-
}
58-
debug!("Patched {patched_files} files");
41+
glob::glob(pattern.to_str().unwrap())?
42+
.filter_map(Result::ok)
43+
.par_bridge()
44+
.for_each(|go_file| {
45+
// Skip directories - glob can match directories ending in .go (e.g., vendor/github.com/nats-io/nats.go)
46+
if !go_file.is_file() {
47+
return;
48+
}
49+
50+
let Ok(content) = fs::read_to_string(&go_file) else {
51+
error!("Failed to read Go file: {go_file:?}");
52+
return;
53+
};
54+
55+
let patched_content = patch_imports_for_source(&content);
56+
if patched_content != content {
57+
let Ok(_) = fs::write(&go_file, patched_content) else {
58+
error!("Failed to write patched Go file: {go_file:?}");
59+
return;
60+
};
61+
62+
debug!("Patched imports in: {go_file:?}");
63+
patched_files.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
64+
}
65+
});
66+
debug!(
67+
"Patched {} files",
68+
patched_files.load(std::sync::atomic::Ordering::SeqCst)
69+
);
5970

6071
Ok(())
6172
}

0 commit comments

Comments
 (0)