Skip to content

Speed up TAR file listing - #2304

Merged
martindurant merged 1 commit into
fsspec:masterfrom
harshitgupta31415:harshitgupta31415/speed-up-tar-file-listing
Oct 27, 2026
Merged

martindurant merged 1 commit into
fsspec:masterfrom
harshitgupta31415:harshitgupta31415/speed-up-tar-file-listing

Conversation

@harshitgupta31415

Copy link
Copy Markdown
Contributor

Closes #2303.

TAR find currently lists each directory separately, and every listing scans the whole directory cache again.

This reads the existing cache directly, picks the paths under the requested folder, and sorts the result as before. It doesn't add another persistent index.

The old walk is still used for subclasses, custom walk options, unusual depth values, and archives where a file masks a directory. That keeps those cases on the same path instead of trying to guess what they should return.

I compared 2,905 calls against the old code, including cold and warm caches, metadata and ordering, missing paths, files, empty folders, links, duplicate names, literal parent components, depth limits and recursive globs. They matched, including 86 errors.

The archive checks passed: 340 passed, 28 skipped. The broader filesystem checks passed too: 579 passed, 129 skipped and one expected failure. Those broader checks give the same Windows test-server warning on both versions. I haven't run every optional remote backend.

With N cached entries and D visited folders, the repeated listing scans go from O(N × D) to one O(N) scan, plus checking ancestor paths and sorting the results. If M entries match the path and depth, H is their maximum path depth, and K entries are returned, that work is O(N + M × H + K log K), ignoring string lengths and using normal dictionary lookup costs. The result still takes O(K) space; the existing archive cache isn't duplicated. Fallback cases still use the old walk and can pay for checking the fast path first.

The benchmarks use Django's actual source archive. Warm listings and opening/indexing/listing are measured separately, so a large warm-listing gain isn't presented as the gain for the whole operation. All the results are below, including the small controls.

One real use of this path is finding NetCDF outputs inside a TAR archive. This change only speeds up listing an archive that's being read; it doesn't implement the saved index requested in that issue.

Measurements: Python 3.15.0, Windows, Intel Core i5-13420H. Each row is the median of five paired timing samples on that same CPU pair. Short calls are batched. Only one process runs, allowed on two physical cores at a time, with the pairs rotated. The masks are kept separate because this CPU has different core types. Run order alternates on successive visits to each pair.

Peak figures are medians of five separate tracemalloc runs, not total process RAM. A separate memory run is shared by the four timing rows for a workload. Small timing differences on this laptop should not be treated as guaranteed gains or losses.

Base commit: 5a22bb2. Fixture loading is outside the warm measurements. The Django archive is version 6.1.2, SHA-256 a1e92451ccb8b514e91bbb3b6d186d20b4030558f116b5d9de6535455ff210b7.

The benchmark scripts, input details and raw samples are available here. All 40 result rows are included below.

I rebased onto 16fad61 and reran the tests and comparisons. That upstream update only changes HTTP writes; the TAR, archive and generic find code used for these timings is unchanged.

All TAR listing benchmarks
Case Core mask Base time PR time Speed Base peak PR peak Memory
warm find django/core 0xf 516.020 ms 2.465 ms 209.339x faster 15.231 KiB 5.535 KiB 2.752x less
warm find django/core 0xf0 476.572 ms 2.149 ms 221.795x faster 15.231 KiB 5.535 KiB 2.752x less
warm find django/core 0x300 765.862 ms 4.470 ms 171.349x faster 15.231 KiB 5.535 KiB 2.752x less
warm find django/core 0xc00 723.727 ms 4.718 ms 153.387x faster 15.231 KiB 5.535 KiB 2.752x less
warm detailed find django/db/backends 0xf 216.339 ms 2.352 ms 91.997x faster 9.423 KiB 5.103 KiB 1.847x less
warm detailed find django/db/backends 0xf0 196.455 ms 2.408 ms 81.581x faster 9.423 KiB 5.103 KiB 1.847x less
warm detailed find django/db/backends 0x300 386.872 ms 4.664 ms 82.947x faster 9.423 KiB 5.103 KiB 1.847x less
warm detailed find django/db/backends 0xc00 344.482 ms 4.617 ms 74.612x faster 9.423 KiB 5.103 KiB 1.847x less
warm glob django/db/backends/**/*.py 0xf 239.759 ms 2.745 ms 87.331x faster 12.163 KiB 10.054 KiB 1.210x less
warm glob django/db/backends/**/*.py 0xf0 196.216 ms 2.101 ms 93.396x faster 12.163 KiB 10.054 KiB 1.210x less
warm glob django/db/backends/**/*.py 0x300 352.684 ms 5.700 ms 61.870x faster 12.163 KiB 10.054 KiB 1.210x less
warm glob django/db/backends/**/*.py 0xc00 329.183 ms 5.257 ms 62.620x faster 12.163 KiB 10.054 KiB 1.210x less
warm find package root depth=1 0xf 28.138 ms 4.017 ms 7.005x faster 5.528 KiB 1.370 KiB 4.035x less
warm find package root depth=1 0xf0 27.745 ms 3.635 ms 7.633x faster 5.528 KiB 1.370 KiB 4.035x less
warm find package root depth=1 0x300 40.241 ms 11.059 ms 3.639x faster 5.528 KiB 1.370 KiB 4.035x less
warm find package root depth=1 0xc00 45.768 ms 7.434 ms 6.157x faster 5.528 KiB 1.370 KiB 4.035x less
warm exact file (fallback) 0xf 35.500 ms 32.162 ms 1.104x faster 4.249 KiB 4.382 KiB 1.031x more
warm exact file (fallback) 0xf0 31.674 ms 34.095 ms 1.076x slower 4.249 KiB 4.382 KiB 1.031x more
warm exact file (fallback) 0x300 47.049 ms 49.025 ms 1.042x slower 4.249 KiB 4.382 KiB 1.031x more
warm exact file (fallback) 0xc00 45.384 ms 45.440 ms 1.001x slower 4.249 KiB 4.382 KiB 1.031x more
warm missing path 0xf 40.437 ms 2.283 ms 17.714x faster 4.392 KiB 0.979 KiB 4.488x less
warm missing path 0xf0 28.327 ms 1.792 ms 15.804x faster 4.392 KiB 0.979 KiB 4.488x less
warm missing path 0x300 53.929 ms 4.962 ms 10.869x faster 4.392 KiB 0.979 KiB 4.488x less
warm missing path 0xc00 51.950 ms 3.936 ms 13.200x faster 4.392 KiB 0.979 KiB 4.488x less
open/index/find django/core 0xf 1867.556 ms 1500.854 ms 1.244x faster 17065.259 KiB 17063.592 KiB same to 3 decimals
open/index/find django/core 0xf0 1846.003 ms 1452.051 ms 1.271x faster 17065.259 KiB 17063.592 KiB same to 3 decimals
open/index/find django/core 0x300 4852.222 ms 4316.012 ms 1.124x faster 17065.259 KiB 17063.592 KiB same to 3 decimals
open/index/find django/core 0xc00 4130.159 ms 4014.778 ms 1.029x faster 17065.259 KiB 17063.592 KiB same to 3 decimals
open/index/find django/db/backends 0xf 1726.290 ms 1456.687 ms 1.185x faster 17065.293 KiB 17063.605 KiB same to 3 decimals
open/index/find django/db/backends 0xf0 1706.174 ms 1527.652 ms 1.117x faster 17065.293 KiB 17063.605 KiB same to 3 decimals
open/index/find django/db/backends 0x300 3983.007 ms 3394.567 ms 1.173x faster 17065.293 KiB 17063.605 KiB same to 3 decimals
open/index/find django/db/backends 0xc00 3619.017 ms 2865.531 ms 1.263x faster 17065.293 KiB 17063.605 KiB same to 3 decimals
warm find two-file archive 0xf 0.030 ms 0.003 ms 9.598x faster 4.672 KiB 0.762 KiB 6.133x less
warm find two-file archive 0xf0 0.023 ms 0.004 ms 6.196x faster 4.672 KiB 0.762 KiB 6.133x less
warm find two-file archive 0x300 0.080 ms 0.010 ms 8.086x faster 4.672 KiB 0.762 KiB 6.133x less
warm find two-file archive 0xc00 0.077 ms 0.010 ms 8.076x faster 4.672 KiB 0.762 KiB 6.133x less
warm find empty archive 0xf 0.030 ms 0.005 ms 5.922x faster 2.500 KiB 0.734 KiB 3.404x less
warm find empty archive 0xf0 0.020 ms 0.005 ms 3.948x faster 2.500 KiB 0.734 KiB 3.404x less
warm find empty archive 0x300 0.052 ms 0.009 ms 5.541x faster 2.500 KiB 0.734 KiB 3.404x less
warm find empty archive 0xc00 0.053 ms 0.010 ms 5.446x faster 2.500 KiB 0.734 KiB 3.404x less

Comment on lines +43 to +47
type(self) is not TarFileSystem
or kwargs
or type(withdirs) is not bool
or type(detail) is not bool
or (maxdepth is not None and (type(maxdepth) is not int or maxdepth < 1))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We do not need to exhaustively check types.

Suggested change
type(self) is not TarFileSystem
or kwargs
or type(withdirs) is not bool
or type(detail) is not bool
or (maxdepth is not None and (type(maxdepth) is not int or maxdepth < 1))
kwargs
or (maxdepth is not None and maxdepth < 1)

Maybe even we're OK with kwargs too - what meaningful impact can it have?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Indeed, even in such a case, it might be fine to call _get_dirs() so that it happens exactly once before those arguments are processed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I checked the kwargs case. find("missing", on_error="raise") raises FileNotFoundError, and passing an error callback calls it once. Ignoring those options would change that behaviour. topdown doesn't change the sorted result, and TAR's ls ignores refresh.

The exact type checks are more defensive than this needs, though. I added them to keep unusual inputs and subclass overrides on the old path, rather than because the faster lookup needs them. I'm fine with simplifying that part; on_error is the option I wouldn't silently ignore.

Calling _get_dirs() up front sounds fine for the normal path too. Once the cache exists, that call just returns.

path = self._strip_protocol(path)
self._get_dirs()
if path in self.dir_cache and self.dir_cache[path]["type"] != "directory":
return super().find(path, maxdepth, withdirs, detail)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This finds a single file, fine.

But why do we need the rest of this method? If we've made a cache of all the files/directories, is that not enough for fund/ls/walk/glob to all work as-is?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That branch is just the single-file fallback. The rest is for finding files under a directory.

The cache is already enough for correct results, yes. The catch is that ls() still scans every entry in dir_cache each time it's called. The old find() walks the directories and calls ls() for each one, even when the cache was built earlier.

I just checked django/core with the cache built before either call. Both versions returned the same 111 files. The old method called ls() 15 times, each scanning the 10,296-entry cache. This method uses one pass over that cache instead. Neither version rebuilt the cache during the check.

So calling _get_dirs() once doesn't remove those repeated scans. ls() and walk() are unchanged by this PR; glob() benefits because it calls find().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For the unlikely 10296-entry tar, what speed difference do you see?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That cache comes from the Django 6.1.2 source tarball, not a generated archive. I tested find("django-6.1.2/django/core"); both versions return the same 111 files.

On one CPU pair, using the median of five runs per version:

Operation Before After Speed
find with the cache already built 516.020 ms 2.465 ms 209.3x faster
Open, index, find and close 1,867.556 ms 1,500.854 ms 1.24x faster

Across all four CPU pairs, the cached lookup was 153-222x faster. Including opening and indexing, it was only 1.03-1.27x faster. Those small differences are noisier on this laptop.

So the large gain is in queries after the archive has been indexed, not the whole first read. The 10,296 cache entries still get scanned once rather than once for each visited directory. Opening and building the index are unchanged by this PR.

The full table, including the controls and slower results, is in the PR. The runner and raw samples are linked there too.

@harshitgupta31415

Copy link
Copy Markdown
Contributor Author

Hi @martindurant,
Just following up on this to see if we can move it forward.
Since the custom find() logic bypasses the repeated dir_cache scans (giving that ~209x speedup on pre-cached listings), I’d love to get this merged into the codebase.
Please let me know if you would like me to go ahead and simplify the defensive type-checking lines as you suggested earlier, or if there is anything else you need me to adjust!

@martindurant
martindurant merged commit 43fd33a into fsspec:master Oct 27, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Speed up TAR file listing

2 participants