Add rayon for parallel file scanning in for no indexingscan.rs

- Integrated `rayon` for concurrent iteration in `scan_filesystem`, enhancing performance.
- Updated `Cargo.toml` and `Cargo.lock` to include `rayon` and its dependencies.
- Adjusted `scan.rs` to utilize `rayon`'s `par_bridge` for parallel file processing with error handling.
This commit is contained in:
elipeter 2025-06-17 19:54:03 +02:00
parent d7b8833ec6
commit 1933082b41
3 changed files with 47 additions and 8 deletions

View file

@ -1,15 +1,18 @@
use crate::utils::project::get_project_info;
use console::style;
use std::path::Path;
use std::sync::Mutex;
use crate::database::index::{IssueRow, Indexer};
use crate::patterns::Severity;
use crate::utils::config::Config;
use crate::utils::query_cache;
use crate::walk::spawn_senders;
use rayon::prelude::*;
use tree_sitter::{Language, Parser, QueryCursor, StreamingIterator};
type DynError = Box<dyn std::error::Error + Send + Sync>;
#[derive(Debug)]
pub struct Diag {
pub(crate) path: String,
@ -34,7 +37,7 @@ pub fn handle(
let diags: Vec<Diag>;
if no_index {
diags = scan_filesystem(&scan_path, config)?;
diags = scan_filesystem(&scan_path, config).unwrap();
} else {
if rebuild_index || !db_path.exists() {
tracing::debug!("Scanning filesystem index filesystem");
@ -75,13 +78,21 @@ pub fn handle(
fn scan_filesystem(
root: &Path,
cfg: &Config,
) -> Result<Vec<Diag>, Box<dyn std::error::Error>> {
) ->Result<Vec<Diag>, Box<dyn std::error::Error + Send + Sync>> {
let rx = spawn_senders(root, cfg);
let mut issues: Vec<Diag> = Vec::new();
for batch in rx.iter().flatten() {
issues.append(&mut run_rules_on_file(&batch, cfg)?);
}
Ok(issues)
let acc = Mutex::new(Vec::new());
rx.into_iter()
.flatten()
.par_bridge() // rayon hand-off
.try_for_each(|path| { // stable API
let mut local = run_rules_on_file(&path, cfg).unwrap(); // <- same as before
let mut guard = acc.lock().unwrap();
guard.append(&mut local);
Ok::<(), DynError>(()) // explicit error type
})?; // propagate first error, if any
Ok(acc.into_inner().unwrap())
}
fn scan_with_index(