Skip to content
Myr

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

3 files+44 added−38 removed

modified:src/internal.rs+20 added-4 removed
4 unmodified lines
5// at your option. All files in the project carrying such6// notice may not be copied, modified, or distributed except7// according to those terms.89use std::collections::HashSet;9use std::ffi::OsString;10use std::process;11use std::time;12use std::io::Write;1314use exec::CommandTemplate;15use lscolors::LsColors;17use walk::FileType;16use regex_syntax::{Expr, ExprBuilder};17use regex::RegexSet;1819/// Whether or not to show20pub struct FileTypes {21 pub files: bool,22 pub directories: bool,23 pub symlinks: bool,24}2526impl Default for FileTypes {27 fn default() -> FileTypes {28 FileTypes {29 files: false,30 directories: false,31 symlinks: false,32 }33 }34}3536/// Configuration options for *fd*.37pub struct FdOptions {38 /// Whether the search is case-sensitive or case-insensitive.39 pub case_sensitive: bool,
34 unmodified lines
74 /// `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>,7763 /// 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>,8182 /// 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 lines
modified:src/main.rs+13 added-18 removed
38 unmodified lines
39use atty::Stream;40use regex::{RegexBuilder, RegexSetBuilder};4142use exec::CommandTemplate;43use internal::{error, pattern_has_uppercase_char, transform_args_with_exec, FdOptions};43use internal::{error, pattern_has_uppercase_char, transform_args_with_exec, FdOptions, FileTypes};44use lscolors::LsColors;45use walk::FileType;4546fn 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 lines
140 .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 lines
modified:src/walk.rs+11 added-16 removed
33 unmodified lines
34 /// Receiver is directly printing results to the output.35 Streaming,36}3738/// The types of file to search for.39#[derive(Copy, Clone, PartialEq, Eq, Hash)]40pub enum FileType {41 RegularFile,42 Directory,43 SymLink,44}4538/// 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, each
155 unmodified lines
197 return ignore::WalkState::Continue;198 }199200 // 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 }213214 // Filter out unwanted extensions.215 if let Some(ref exts_regex) = config.extensions {
38 unmodified lines