Skip to content

Perform replacement in parallel #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions scooter/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Error;
use crossterm::event::KeyEvent;
use fancy_regex::Regex as FancyRegex;
use futures::future;
use ignore::{
overrides::{Override, OverrideBuilder},
WalkState,
Expand All @@ -24,7 +25,10 @@ use tempfile::NamedTempFile;
use tokio::{
fs::File,
io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter},
sync::mpsc::{self, UnboundedReceiver, UnboundedSender},
sync::{
mpsc::{self, UnboundedReceiver, UnboundedSender},
Semaphore,
},
task::JoinHandle,
};

Expand Down Expand Up @@ -666,24 +670,38 @@ impl App {
}

pub fn perform_replacement(
mut search_state: SearchState,
search_state: SearchState,
background_processing_sender: UnboundedSender<BackgroundProcessingEvent>,
) -> JoinHandle<()> {
tokio::spawn(async move {
let mut path_groups: HashMap<PathBuf, Vec<&mut SearchResult>> = HashMap::new();
for res in search_state.results.iter_mut().filter(|res| res.included) {
let mut path_groups: HashMap<PathBuf, Vec<SearchResult>> = HashMap::new();
for res in search_state.results.into_iter().filter(|res| res.included) {
path_groups.entry(res.path.clone()).or_default().push(res);
}

let semaphore = Arc::new(Semaphore::new(8));
let mut file_tasks = vec![];

for (path, mut results) in path_groups {
if let Err(file_err) = Self::replace_in_file(path, &mut results).await {
results.iter_mut().for_each(|res| {
res.replace_result = Some(ReplaceResult::Error(file_err.to_string()))
});
}
let semaphore = semaphore.clone();
let task = tokio::spawn(async move {
let permit = semaphore.clone().acquire_owned().await.unwrap();
if let Err(file_err) = Self::replace_in_file(path, &mut results).await {
results.iter_mut().for_each(|res| {
res.replace_result = Some(ReplaceResult::Error(file_err.to_string()))
});
}
drop(permit);
results
});
file_tasks.push(task);
}

let replace_state = Self::calculate_statistics(&search_state.results);
let replacement_results = future::join_all(file_tasks)
.await
.into_iter()
.flat_map(Result::unwrap);
let replace_state = Self::calculate_statistics(replacement_results);

// Ignore error: we may have gone back to the previous screen
let _ = background_processing_sender.send(
Expand Down Expand Up @@ -1003,13 +1021,16 @@ impl App {
false
}

fn calculate_statistics(results: &[SearchResult]) -> ReplaceState {
fn calculate_statistics<I>(results: I) -> ReplaceState
where
I: IntoIterator<Item = SearchResult>,
{
let mut num_successes = 0;
let mut num_ignored = 0;
let mut errors = vec![];

results
.iter()
.into_iter()
.for_each(|res| match (res.included, &res.replace_result) {
(false, _) => {
num_ignored += 1;
Expand Down Expand Up @@ -1039,7 +1060,7 @@ impl App {

async fn replace_in_file(
file_path: PathBuf,
results: &mut [&mut SearchResult],
results: &mut [SearchResult],
) -> anyhow::Result<()> {
let mut line_map: HashMap<_, _> =
HashMap::from_iter(results.iter_mut().map(|res| (res.line_number, res)));
Expand Down Expand Up @@ -1237,8 +1258,8 @@ mod tests {
#[tokio::test]
async fn test_calculate_statistics_all_success() {
let app = build_test_app(vec![success_result(), success_result(), success_result()]);
let stats = if let Screen::SearchComplete(search_state) = &app.current_screen {
App::calculate_statistics(&search_state.results)
let stats = if let Screen::SearchComplete(search_state) = app.current_screen {
App::calculate_statistics(search_state.results)
} else {
panic!("Expected SearchComplete");
};
Expand All @@ -1264,8 +1285,8 @@ mod tests {
error_result.clone(),
ignored_result(),
]);
let stats = if let Screen::SearchComplete(search_state) = &app.current_screen {
App::calculate_statistics(&search_state.results)
let stats = if let Screen::SearchComplete(search_state) = app.current_screen {
App::calculate_statistics(search_state.results)
} else {
panic!("Expected SearchComplete");
};
Expand Down