-
-
Notifications
You must be signed in to change notification settings - Fork 803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return first file found and terminate #472
Comments
I looked into some closed issues and found |
Thank you for your feedback. I managed to find a similar example on my filesystem where I could reproduce your results. I think the problem is that piping into As a demonstration, let's look at hyperfine --warmup 3 \
'find -iname "*flow.yaml"' \
'find -iname "*flow.yaml" | head -n1' \
'find -iname "*flow.yaml" -print -quit'
Notice how the variant with With
The variant with We can demonstrate a similar behavior by running: (echo first; sleep 1; echo second; sleep 100; echo third) | head -n 1 This command runs one second instead of quitting immediately. To make sure that this is the actual problem with --- a/src/output.rs
+++ b/src/output.rs
@@ -90,5 +90,6 @@ fn print_entry_uncolorized(
let separator = if config.null_separator { "\0" } else { "\n" };
let path_str = path.to_string_lossy();
- write!(stdout, "{}{}", path_str, separator)
+ write!(stdout, "{}{}", path_str, separator)?;
+ writeln!(stdout)
} With this small modification,
Now, this is obviously not something we want to implement in this way. If anybody has any good suggestions on how to "fix" this, please let us know. One potential way could be to test (however that works) if STDOUT has been closed after printing each result. However, it should be checked if this has any performance impact when not piping to If there is no great solution, we should actually think about implementing a |
I believe you can attempt to write 0 bytes to stdout, and you'll get So maybe have a timer such that if the main thread hasn't received any files to print in a while, it writes 0 bytes to stdout and exits if that fails. Alternatively don't bother, since no other tool seems to. |
Correction: despite what StackOverflow said, writing 0 bytes to a closed pipe does not trigger |
There is a way, at least on Linux: https://stackoverflow.com/a/57959507/502399 On Windows, apparently the write-zero-bytes thing works. |
This new option can be used instead of piping to `head -n <count>` for improved performance: | Command | Mean [ms] | Min [ms] | Max [ms] | Relative | |:---|---:|---:|---:|---:| | `fd --max-buffer-time=0 flow.yaml` | 153.9 ± 2.5 | 151.3 | 170.3 | 4.21 ± 5.86 | | `fd --max-buffer-time=0 flow.yaml \| head -n 1` | 145.3 ± 17.4 | 111.0 | 180.2 | 3.98 ± 5.55 | | `fd --max-results=1 flow.yaml` | 36.5 ± 50.8 | 7.2 | 145.7 | 1.00 | Note: there is a large standard deviation on the last result due to the non-deterministic file system traversal. With `--max-results`, we don't have to traverse the whole filesystem tree, so it's all about luck. closes #472 closes #476
@tavianator Thank you very much for your analysis. I opted to implement Please see #555 for benchmark results. |
This new option can be used instead of piping to `head -n <count>` for improved performance: | Command | Mean [ms] | Min [ms] | Max [ms] | Relative | |:---|---:|---:|---:|---:| | `fd --max-buffer-time=0 flow.yaml` | 153.9 ± 2.5 | 151.3 | 170.3 | 4.21 ± 5.86 | | `fd --max-buffer-time=0 flow.yaml \| head -n 1` | 145.3 ± 17.4 | 111.0 | 180.2 | 3.98 ± 5.55 | | `fd --max-results=1 flow.yaml` | 36.5 ± 50.8 | 7.2 | 145.7 | 1.00 | Note: there is a large standard deviation on the last result due to the non-deterministic file system traversal. With `--max-results`, we don't have to traverse the whole filesystem tree, so it's all about luck. closes #472 closes #476
This has now been released in fd v8.0. We also have |
Does
fd
support the equivalent offind PATH -name NAME -print -quit
, which finds the first match, prints the result, and terminates?The text was updated successfully, but these errors were encountered: