Skip to content
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

ignore: add existence check for ignore files #1381

Closed
wants to merge 1 commit into from

Conversation

sharkdp
Copy link
Sponsor Contributor

@sharkdp sharkdp commented Sep 17, 2019

This PR suggests to add a simple .exists() check for .gitignore, .ignore, and other similar files before actually calling File::open(…) in GitIgnoreBuilder::add.

The reason is that a simple existence check via stat can be faster than actually trying to open the file (see https://stackoverflow.com/a/12774387/704831). As we typically expect the number of directories without ignore files to be much larger than the number of directories with ignore files, this leads to an overall speedup.

The performance gain is not huge for rg, but can be quite significant if more .gitignore-like files are added via add_custom_ignore_filename. The speedup is larger for folders with low files-per-directory ratios.

Benchmark results

rg --files in my home folder (200k results, 6.5 files per directory):

Command Mean [ms] Min [ms] Max [ms] Relative
./rg-master --files 396.4 ± 3.2 390.9 400.0 1.05
./rg-feature --files 376.0 ± 3.6 369.3 383.5 1.00

rg --files --hidden in my home folder (800k results, 5.4 files per directory)

Command Mean [s] Min [s] Max [s] Relative
./rg-master --files --hidden 1.575 ± 0.012 1.560 1.597 1.06
./rg-feature --files --hidden 1.479 ± 0.011 1.464 1.496 1.00

rg --files in the chromium-79.0.3915.2 source tree (300k results, 12.7 files per directory)

Command Mean [ms] Min [ms] Max [ms] Relative
~/rg-master --files 445.2 ± 5.3 435.6 453.0 1.04
~/rg-feature --files 428.9 ± 7.0 418.2 440.0 1.00

rg --files in the linux-5.3 source tree (65k results, 15.1 files per directory)

Command Mean [ms] Min [ms] Max [ms] Relative
./rg-master --files 94.5 ± 1.9 89.8 98.5 1.02
./rg-feature --files 92.6 ± 2.7 88.4 98.7 1.00

Running a fd search in my home directory (fd uses more .gitignore-like files):

Command Mean [s] Min [s] Max [s] Relative
./fd-master -H foobar 1.136 ± 0.012 1.122 1.158 1.10
./fd-feature -H foobar 1.030 ± 0.006 1.022 1.041 1.00

This commit adds a simple `.exists()` check for `.gitignore`,
`.ignore`, and other similar files before actually calling
`File::open(…)` in `GitIgnoreBuilder::add`.

The reason is that a simple existence check via `stat` can be faster
than actually trying to `open` the file, see
https://stackoverflow.com/a/12774387/704831. As we typically expect(?)
the number of directories *without* ignore files to be much larger
than the number of directories *with* ignore files, this leads to an
overall speedup.

The performance gain is not huge for `rg`, but can be quite significant
if more `.gitignore`-like files are added via
`add_custom_ignore_filename`. The speedup is *larger* for folders with
*low* files-per-directory ratios.

Benchmark results
-----------------

`rg --files` in my home folder (200k results, 6.5 files per directory):

| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `./rg-master --files` | 396.4 ± 3.2 | 390.9 | 400.0 | 1.05 |
| `./rg-feature --files` | 376.0 ± 3.6 | 369.3 | 383.5 | 1.00 |

`rg --files --hidden` in my home folder (800k results, 5.4
files per directory)

| Command | Mean [s] | Min [s] | Max [s] | Relative |
|:---|---:|---:|---:|---:|
| `./rg-master --files --hidden` | 1.575 ± 0.012 | 1.560 | 1.597 | 1.06 |
| `./rg-feature --files --hidden` | 1.479 ± 0.011 | 1.464 | 1.496 | 1.00 |

`rg --files` in the chromium-79.0.3915.2 source tree (300k results, 12.7 files per
directory)

| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `~/rg-master --files` | 445.2 ± 5.3 | 435.6 | 453.0 | 1.04 |
| `~/rg-feature --files` | 428.9 ± 7.0 | 418.2 | 440.0 | 1.00 |

`rg --files` in the linux-5.3 source tree (65k results, 15.1
files per directory)

| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `./rg-master --files` | 94.5 ± 1.9 | 89.8 | 98.5 | 1.02 |
| `./rg-feature --files` | 92.6 ± 2.7 | 88.4 | 98.7 | 1.00 |
@sharkdp
Copy link
Sponsor Contributor Author

sharkdp commented Sep 23, 2019

(I am assuming that the CI failure is not related to my PR. If this is not the case, please let me know)

@BurntSushi BurntSushi closed this Feb 15, 2020
@BurntSushi BurntSushi reopened this Feb 15, 2020
Copy link
Owner

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I buy it, thanks!

@BurntSushi BurntSushi added the rollup A PR that has been merged with many others in a rollup. label Feb 15, 2020
BurntSushi pushed a commit that referenced this pull request Feb 15, 2020
This commit adds a simple `.exists()` check for `.gitignore`,
`.ignore`, and other similar files before actually calling
`File::open(…)` in `GitIgnoreBuilder::add`.

The reason is that a simple existence check via `stat` can be faster
than actually trying to `open` the file, see
https://stackoverflow.com/a/12774387/704831. As we typically expect(?)
the number of directories *without* ignore files to be much larger
than the number of directories *with* ignore files, this leads to an
overall speedup.

The performance gain is not huge for `rg`, but can be quite significant
if more `.gitignore`-like files are added via
`add_custom_ignore_filename`. The speedup is *larger* for folders with
*low* files-per-directory ratios.

Note though that we do not do this check on Windows until a specific
analysis there suggests this is beneficial. Namely, Windows generally
has slower file system operations, so it's not clear whether this
speculative check is actually a benefit or not.

Benchmark results
-----------------

`rg --files` in my home folder (200k results, 6.5 files per directory):

| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `./rg-master --files` | 396.4 ± 3.2 | 390.9 | 400.0 | 1.05 |
| `./rg-feature --files` | 376.0 ± 3.6 | 369.3 | 383.5 | 1.00 |

`rg --files --hidden` in my home folder (800k results, 5.4
files per directory)

| Command | Mean [s] | Min [s] | Max [s] | Relative |
|:---|---:|---:|---:|---:|
| `./rg-master --files --hidden` | 1.575 ± 0.012 | 1.560 | 1.597 | 1.06 |
| `./rg-feature --files --hidden` | 1.479 ± 0.011 | 1.464 | 1.496 | 1.00 |

`rg --files` in the chromium-79.0.3915.2 source tree (300k results, 12.7 files per
directory)

| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `~/rg-master --files` | 445.2 ± 5.3 | 435.6 | 453.0 | 1.04 |
| `~/rg-feature --files` | 428.9 ± 7.0 | 418.2 | 440.0 | 1.00 |

`rg --files` in the linux-5.3 source tree (65k results, 15.1
files per directory)

| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `./rg-master --files` | 94.5 ± 1.9 | 89.8 | 98.5 | 1.02 |
| `./rg-feature --files` | 92.6 ± 2.7 | 88.4 | 98.7 | 1.00 |

Closes #1381
@sharkdp sharkdp deleted the add-existence-check branch April 2, 2020 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rollup A PR that has been merged with many others in a rollup.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants