perf: optimize directory reconstruction in recursive find/walk - #1046
perf: optimize directory reconstruction in recursive find/walk#1046raj-prince wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request optimizes _get_dirs_and_update_cache in gcsfs/core.py by caching the root path length and avoiding redundant directory key splits and cache updates when a parent directory has already been processed. It also adds unit tests to verify the nested directory cache behavior. The reviewer suggested a further optimization to immediately break the loop when a parent has already been seen and either a prefix is set or cache updating is disabled, avoiding redundant ancestor traversals.
1103e9c to
996b652
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1046 +/- ##
==========================================
+ Coverage 90.18% 90.20% +0.01%
==========================================
Files 16 16
Lines 3740 3747 +7
==========================================
+ Hits 3373 3380 +7
Misses 367 367 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Summary
Optimizes
GCSFileSystem._get_dirs_and_update_cache(used infind(),walk(), and recursive listings) by creating directory metadata dictionaries only once per unique folder and short-circuiting ancestor traversal when a directory branch is already registered.Reduces time complexity from$\mathcal{O}(N \times \text{Depth})$ to $\mathcal{O}(N + U \times \text{Depth})$ (N: total files, U: unique directories), yielding up to a 14.2x speedup (93% less CPU time) and eliminating >99% of heap dictionary allocations.
Benchmark Results (similar to
gcsfs/tests/perf/microbenchmarks/but hierarchy generated in memory - no network calls)configs.yaml)find_flat_foldersfind_recursive_foldersfind_recursive_folders_deepfind_recursive_folders_deepChanges
parent_already_seen = parent in dirsto avoid recreating duplicate dicts for every sibling file.listingand its parent's ancestors are established.prefixis set orupdate_cache=False.root_lenandshould_update_cacheoutside loops.Testing
test_core.pyandtest_extended_hns.py.black,flake8,isort) clean.