Skip to content
Myr

Check the pattern before anything else, since it doesn't require metadata

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This should partially address #432 by decreasing the number of stat() calls: $ strace -c -f ./fd-before '\.h$' /usr -j1 -S +1k >/dev/null % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 15.71 8.831948 7 1192279 46059 stat $ strace -c -f ./fd-after '\.h$' /usr -j1 -S +1k >/dev/null % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 7.92 1.972474 10 183907 46046 stat Though it's not as few as possible: $ strace -c -f find /usr -iname '*.h' -size +1k >/dev/null % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 19.01 0.946500 5 161649 newfstatat $ strace -c -f bfs /usr -iname '*.h' -size +1k >/dev/null % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 13.73 0.406565 5 69005 statx Performance is much better when metadata is required: $ hyperfine ./fd-{before,after}" '\.h$' /usr -j1 -S +1k" Benchmark #1: ./fd-before '\.h$' /usr -j1 -S +1k Time (mean ± σ): 4.623 s ± 0.154 s [User: 1.465 s, System: 3.354 s] Range (min … max): 4.327 s … 4.815 s 10 runs Benchmark #2: ./fd-after '\.h$' /usr -j1 -S +1k Time (mean ± σ): 2.650 s ± 0.058 s [User: 1.258 s, System: 1.592 s] Range (min … max): 2.568 s … 2.723 s 10 runs Summary './fd-after '\.h$' /usr -j1 -S +1k' ran 1.74 ± 0.07 times faster than './fd-before '\.h$' /usr -j1 -S +1k' While remaining the same when it's not: $ hyperfine ./fd-{before,after}" '\.h$' /usr -j1" Benchmark #1: ./fd-before '\.h$' /usr -j1 Time (mean ± σ): 2.382 s ± 0.038 s [User: 1.221 s, System: 1.286 s] Range (min … max): 2.325 s … 2.433 s 10 runs Benchmark #2: ./fd-after '\.h$' /usr -j1 Time (mean ± σ): 2.362 s ± 0.034 s [User: 1.193 s, System: 1.294 s] Range (min … max): 2.307 s … 2.422 s 10 runs Summary './fd-after '\.h$' /usr -j1' ran 1.01 ± 0.02 times faster than './fd-before '\.h$' /usr -j1'

authored by Tavian Barnes · committed by David Peter · parent 35945c4 · Browse files · 1 file+36 added−32 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+36 added−32 removed

  • other:+36 added−32 removed

1 file+36 added−32 removed

modified:src/walk.rs+36 added-32 removed
265 unmodified lines
266 if entry.depth() == 0 {267 return ignore::WalkState::Continue;268 }269270 // Check the name first, since it doesn't require metadata271272 let entry_path = entry.path();273274 let search_str_o = if config.search_full_path {275 match fshelper::path_absolute_form(entry_path) {276 Ok(path_abs_buf) => Some(path_abs_buf.to_string_lossy().into_owned().into()),277 Err(_) => {278 print_error_and_exit!("Unable to retrieve absolute path.");279 }280 }281 } else {282 entry_path.file_name().map(|f| f.to_string_lossy())283 };284285 if let Some(search_str) = search_str_o {286 if !pattern.is_match(&*search_str) {287 return ignore::WalkState::Continue;288 }289 }290291 // Filter out unwanted extensions.292 if let Some(ref exts_regex) = config.extensions {293 if let Some(path_str) = entry_path.file_name().and_then(|s| s.to_str()) {294 if !exts_regex.is_match(path_str) {295 return ignore::WalkState::Continue;296 }297 } else {298 return ignore::WalkState::Continue;299 }300 }301302 // Filter out unwanted file types.303304 if let Some(ref file_types) = config.file_types {305 if let Some(ref entry_type) = entry.file_type() {
19 unmodified lines
325 return ignore::WalkState::Continue;326 }327 }328297 let entry_path = entry.path();298299 // Filter out unwanted extensions.300 if let Some(ref exts_regex) = config.extensions {301 if let Some(path_str) = entry_path.file_name().and_then(|s| s.to_str()) {302 if !exts_regex.is_match(path_str) {303 return ignore::WalkState::Continue;304 }305 } else {306 return ignore::WalkState::Continue;307 }308 }309329 // Filter out unwanted sizes if it is a file and we have been given size constraints.330 if !config.size_constraints.is_empty() {331 if entry_path.is_file() {332 if let Ok(metadata) = entry_path.metadata() {
30 unmodified lines
363 return ignore::WalkState::Continue;364 }365 }366348 let search_str_o = if config.search_full_path {349 match fshelper::path_absolute_form(entry_path) {350 Ok(path_abs_buf) => Some(path_abs_buf.to_string_lossy().into_owned().into()),351 Err(_) => {352 print_error_and_exit!("Unable to retrieve absolute path.");353 }354 }355 } else {356 entry_path.file_name().map(|f| f.to_string_lossy())357 };367 // TODO: take care of the unwrap call368 tx_thread369 .send(WorkerResult::Entry(entry_path.to_owned()))370 .unwrap();371359 if let Some(search_str) = search_str_o {360 if pattern.is_match(&*search_str) {361 // TODO: take care of the unwrap call362 tx_thread363 .send(WorkerResult::Entry(entry_path.to_owned()))364 .unwrap()365 }366 }367372 ignore::WalkState::Continue373 })374 });375}