Rewrite of file-type filtering leading to 5% speed-up
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by David Peter · parent e5ee5eb · Browse files · 3 files·+44 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+44 added−38 removed
- other:+44 added−38 removed
Provenance
- Commit
40d811c- Tree
07a54f3- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
e5ee5eb
3 files changed+44 added−38 removed
modified:src/internal.rs+20 added-4 removed
4 unmodified lines5 // at your option. All files in the project carrying such6 // notice may not be copied, modified, or distributed except7 // according to those terms.8 9-use std::collections::HashSet;9 use std::ffi::OsString;10 use std::process;11 use std::time;12 use std::io::Write;13 14 use exec::CommandTemplate;15 use lscolors::LsColors;17-use walk::FileType;16 use regex_syntax::{Expr, ExprBuilder};17 use regex::RegexSet;18 19+/// Whether or not to show20+pub struct FileTypes {21+ pub files: bool,22+ pub directories: bool,23+ pub symlinks: bool,24+}25+26+impl Default for FileTypes {27+ fn default() -> FileTypes {28+ FileTypes {29+ files: false,30+ directories: false,31+ symlinks: false,32+ }33+ }34+}35+36 /// Configuration options for *fd*.37 pub struct FdOptions {38 /// Whether the search is case-sensitive or case-insensitive.39 pub case_sensitive: bool,34 unmodified lines74 /// `None` if the output should not be colorized. Otherwise, a `LsColors` instance that defines75 /// how to style different filetypes.76 pub ls_colors: Option<LsColors>,77 63- /// The type of file to search for. All files other than the specified type will be ignored.64- pub file_types: HashSet<FileType>,78+ /// The type of file to search for. If set to `None`, all file types are displayed. If79+ /// set to `Some(..)`, only the types that are specified are shown.80+ pub file_types: Option<FileTypes>,81 82 /// The extension to search for. Only entries matching the extension will be included.83 ///84 /// The value (if present) will be a lowercase string without leading dots.161 unmodified linesmodified:src/main.rs+13 added-18 removed
38 unmodified lines39 use atty::Stream;40 use regex::{RegexBuilder, RegexSetBuilder};41 42 use exec::CommandTemplate;43-use internal::{error, pattern_has_uppercase_char, transform_args_with_exec, FdOptions};43+use internal::{error, pattern_has_uppercase_char, transform_args_with_exec, FdOptions, FileTypes};44 use lscolors::LsColors;45-use walk::FileType;45 46 fn main() {47 let checked_args = transform_args_with_exec(env::args_os());48 let matches = app::build_app().get_matches_from(checked_args);91 unmodified lines140 .value_of("max-buffer-time")141 .and_then(|n| u64::from_str_radix(n, 10).ok())142 .map(time::Duration::from_millis),143 ls_colors,145- file_types: match matches.values_of("file-type") {146- None => vec![147- FileType::RegularFile,148- FileType::Directory,149- FileType::SymLink,150- ].into_iter()151- .collect(),152- Some(values) => values153- .map(|value| match value {154- "f" | "file" => FileType::RegularFile,155- "d" | "directory" => FileType::Directory,156- "l" | "symlink" => FileType::SymLink,157- _ => FileType::RegularFile,158- })159- .collect(),160- },144+ file_types: matches.values_of("file-type").map(|values| {145+ let mut file_types = FileTypes::default();146+ for value in values {147+ match value {148+ "f" | "file" => file_types.files = true,149+ "d" | "directory" => file_types.directories = true,150+ "l" | "symlink" => file_types.symlinks = true,151+ _ => unreachable!(),152+ }153+ }154+ file_types155+ }),156 extensions: matches.values_of("extension").map(|exts| {157 let patterns = exts.map(|e| e.trim_left_matches('.'))158 .map(|e| format!(r".\.{}$", regex::escape(e)));159 match RegexSetBuilder::new(patterns)29 unmodified linesmodified:src/walk.rs+11 added-16 removed
33 unmodified lines34 /// Receiver is directly printing results to the output.35 Streaming,36 }37 38-/// The types of file to search for.39-#[derive(Copy, Clone, PartialEq, Eq, Hash)]40-pub enum FileType {41- RegularFile,42- Directory,43- SymLink,44-}45-38 /// Recursively scan the given search path for files / pathnames matching the pattern.39 ///40 /// If the `--exec` argument was supplied, this will create a thread pool for executing41 /// jobs in parallel from a given command line and the discovered paths. Otherwise, each155 unmodified lines197 return ignore::WalkState::Continue;198 }199 200 // Filter out unwanted file types.209- if (entry.file_type().map_or(false, |ft| ft.is_file())210- && !config.file_types.contains(&FileType::RegularFile))211- || (entry.file_type().map_or(false, |ft| ft.is_dir())212- && !config.file_types.contains(&FileType::Directory))213- || (entry.file_type().map_or(false, |ft| ft.is_symlink())214- && !config.file_types.contains(&FileType::SymLink))215- {216- return ignore::WalkState::Continue;201+ if let Some(ref file_types) = config.file_types {202+ if let Some(ref entry_type) = entry.file_type() {203+ if (entry_type.is_file() && !file_types.files)204+ || (entry_type.is_dir() && !file_types.directories)205+ || (entry_type.is_symlink() && !file_types.symlinks)206+ {207+ return ignore::WalkState::Continue;208+ }209+ } else {210+ return ignore::WalkState::Continue;211+ }212 }213 214 // Filter out unwanted extensions.215 if let Some(ref exts_regex) = config.extensions {38 unmodified lines