Merge remote-tracking branch 'origin/master' into clap-derive
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parents 36ee44a, c6f9595 · Browse files · 5 files·+33 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+33 added−28 removed
- other:+33 added−28 removed
Provenance
- Commit
169d22f- Tree
bfac866- Author
David Peter <mail@david-peter.de>- Committer
David Peter <mail@david-peter.de>- Parent 1
36ee44a- Parent 2
c6f9595
5 files changed+33 added−28 removed
modified:src/exec/job.rs+4 added-8 removed
1-use std::sync::mpsc::Receiver;1 use std::sync::{Arc, Mutex};2 3+use crossbeam_channel::Receiver;4+5 use crate::config::Config;6 use crate::dir_entry::DirEntry;7 use crate::error::print_error;8 use crate::exit_codes::{merge_exitcodes, ExitCode};4 unmodified lines13 /// An event loop that listens for inputs from the `rx` receiver. Each received input will14 /// generate a command with the supplied command template. The generated command will then15 /// be executed, and this process will continue until the receiver's sender has closed.16 pub fn job(16- rx: Arc<Mutex<Receiver<WorkerResult>>>,17+ rx: Receiver<WorkerResult>,18 cmd: Arc<CommandSet>,19 out_perm: Arc<Mutex<()>>,20 config: &Config,21 ) -> ExitCode {1 unmodified line23 let buffer_output: bool = config.threads > 1;24 25 let mut results: Vec<ExitCode> = Vec::new();26 loop {26- // Create a lock on the shared receiver for this thread.27- let lock = rx.lock().unwrap();28-27 // Obtain the next result from the receiver, else if the channel28 // has closed, exit from the loop31- let dir_entry: DirEntry = match lock.recv() {29+ let dir_entry: DirEntry = match rx.recv() {30 Ok(WorkerResult::Entry(dir_entry)) => dir_entry,31 Ok(WorkerResult::Error(err)) => {32 if config.show_filesystem_errors {33 print_error(err.to_string());2 unmodified lines36 }37 Err(_) => break,38 };39 42- // Drop the lock so that other threads can read from the receiver.43- drop(lock);40 // Generate a command, execute it and store its exit code.41 results.push(cmd.execute(42 dir_entry.stripped_path(config),43 config.path_separator.as_deref(),23 unmodified linesmodified:src/walk.rs+12 added-19 removed
1 unmodified line2 use std::io;3 use std::mem;4 use std::path::PathBuf;5 use std::sync::atomic::{AtomicBool, Ordering};6-use std::sync::mpsc::{channel, Receiver, RecvTimeoutError, Sender};6 use std::sync::{Arc, Mutex};7 use std::thread;8 use std::time::{Duration, Instant};9 use std::{borrow::Cow, io::Write};10 11 use anyhow::{anyhow, Result};12+use crossbeam_channel::{bounded, Receiver, RecvTimeoutError, Sender};13 use ignore::overrides::OverrideBuilder;14 use ignore::{self, WalkBuilder};15 use regex::bytes::Regex;16 31 unmodified lines48 ///49 /// If the `--exec` argument was supplied, this will create a thread pool for executing50 /// jobs in parallel from a given command line and the discovered paths. Otherwise, each51 /// path will simply be written to standard output.52-pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<Config>) -> Result<ExitCode> {53- let mut path_iter = path_vec.iter();54- let first_path_buf = path_iter55- .next()56- .expect("Error: Path vector can not be empty");57- let (tx, rx) = channel();52+pub fn scan(paths: &[PathBuf], pattern: Arc<Regex>, config: Arc<Config>) -> Result<ExitCode> {53+ let first_path = &paths[0];54 59- let mut override_builder = OverrideBuilder::new(first_path_buf.as_path());55+ // Channel capacity was chosen empircally to perform similarly to an unbounded channel56+ let (tx, rx) = bounded(0x4000 * config.threads);57 58+ let mut override_builder = OverrideBuilder::new(first_path);59+60 for pattern in &config.exclude_patterns {61 override_builder62 .add(pattern)63 .map_err(|e| anyhow!("Malformed exclude pattern: {}", e))?;1 unmodified line65 let overrides = override_builder66 .build()67 .map_err(|_| anyhow!("Mismatch in exclude patterns"))?;68 70- let mut walker = WalkBuilder::new(first_path_buf.as_path());69+ let mut walker = WalkBuilder::new(first_path);70 walker71 .hidden(config.ignore_hidden)72 .ignore(config.read_fdignore)73 .parents(config.read_parent_ignore && (config.read_fdignore || config.read_vcsignore))45 unmodified lines119 None => (),120 }121 }122 124- for path_entry in path_iter {125- walker.add(path_entry.as_path());123+ for path in &paths[1..] {124+ walker.add(path);125 }126 127 let parallel_walker = walker.threads(config.threads).build_parallel();128 94 unmodified lines223 fn recv(&self) -> Result<WorkerResult, RecvTimeoutError> {224 match self.mode {225 ReceiverMode::Buffering => {226 // Wait at most until we should switch to streaming228- let now = Instant::now();229- self.deadline230- .checked_duration_since(now)231- .ok_or(RecvTimeoutError::Timeout)232- .and_then(|t| self.rx.recv_timeout(t))227+ self.rx.recv_deadline(self.deadline)228 }229 ReceiverMode::Streaming => {230 // Wait however long it takes for a result231 Ok(self.rx.recv()?)110 unmodified lines342 if let Some(ref cmd) = config.command {343 if cmd.in_batch_mode() {344 exec::batch(rx, cmd, &config)345 } else {351- let shared_rx = Arc::new(Mutex::new(rx));352-346 let out_perm = Arc::new(Mutex::new(()));347 348 // Each spawned job will store it's thread handle in here.349 let mut handles = Vec::with_capacity(threads);350 for _ in 0..threads {351 let config = Arc::clone(&config);359- let rx = Arc::clone(&shared_rx);352+ let rx = rx.clone();353 let cmd = Arc::clone(cmd);354 let out_perm = Arc::clone(&out_perm);355 356 // Spawn a job thread that will listen for and execute inputs.195 unmodified linesmodified:Cargo.lock+11 added
173 unmodified lines174 source = "registry+https://github.com/rust-lang/crates.io-index"175 checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"176 177 [[package]]178+name = "crossbeam-channel"179+version = "0.5.6"180+source = "registry+https://github.com/rust-lang/crates.io-index"181+checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521"182+dependencies = [183+ "cfg-if",184+ "crossbeam-utils",185+]186+187+[[package]]188 name = "crossbeam-utils"189 version = "0.8.12"190 source = "registry+https://github.com/rust-lang/crates.io-index"191 checksum = "edbafec5fa1f196ca66527c1b12c2ec4745ca14b50f1ad8f9f6f720b55d11fac"124 unmodified lines316 "atty",317 "chrono",318 "clap",319 "clap_complete",320+ "crossbeam-channel",321 "ctrlc",322 "diff",323 "dirs-next",324 "faccess",793 unmodified linesmodified:Cargo.toml+1 added
47 unmodified lines48495051525354555633 unmodified lines47 unmodified linesdirs-next = "2.0"normpath = "0.3.2"chrono = "0.4"once_cell = "1.15.0"crossbeam-channel = "0.5.6"clap_complete = {version = "4.0", optional = true}
[dependencies.clap]version = "4.0.12"33 unmodified linesmodified:CHANGELOG.md+5 added-1 removed
8 unmodified lines9 - Fixed differences between piped / non-piped output. This changes `fd`s behavior back to what we10 had before 8.3.0, i.e. there will be no leading `./` prefixes, unless `--exec`/`-x`,11 `--exec-batch`/`-X`, or `--print0`/`-0` are used. `--strip-cwd-prefix` can be used to strip that12 prefix in those cases. See #1046, #1115, and #1121 (@tavianator)13+- `fd` could previously crash with a panic due to a race condition in Rusts standard library14+ (see https://github.com/rust-lang/rust/issues/39364). This has been fixed by switching to a different15+ message passing implementation, see #1060 and #1146 (@tavianator)16+- `fd`s memory usage will not grow unboundedly on huge directory trees, see #1146 (@tavianator)17 - fd returns an error when current working directory does not exist while a search path is18 specified, see #1072 (@vijfhoek)19 - Improved "command not found" error message, see #1083 and #1109 (@themkat)20 21 ## Changes22 23 - No leading `./` prefix for non-interactive results, see above.24+- fd now colorizes paths in parallel, significantly improving performance, see #1148 (@tavianator)25 - fd can now avoid `stat` syscalls even when colorizing paths, as long as the color scheme doesn't26 require metadata, see #1148 (@tavianator)22-- fd now colorizes paths in parallel, significantly improving performance, see #1148 (@tavianator)27 28 ## Other29 30 - Added link back to GitHub in man page and `--help` text, see #1086 (@scottchiefbaker)593 unmodified lines