Repository navigation
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Path.iterdir()builds aPathfor every name in the archive and then filters down to direct children. If you walk the whole tree, you rescan the archive once per directory, which makes the walk O(dirs × entries).importlib.resources.as_file()on a zipped package does exactly this walk.Read-only archives already go through
FastLookup, which caches the name list. This adds a cached parent → children index next to it:class FastLookup(CompleteDirs): namelist() # cached _name_set() # cached + _children # cached: {parent_dir: [names]} Path.iterdir() if not is_dir(): raise + if root is FastLookup: + return map(_next, root._children[at]) return filter(_is_child, map(_next, root.namelist()))Same names, same order, same parent rule (
dirname(name.rstrip('/'))) as_is_child. Write-mode archives don't useFastLookup, so they keep the old path.Evidence
Full tree walk with zipp on Python 3.14, median of 7, macOS arm64.
nis the number of directories, each holding one file:Same patch applied to CPython's
zipfile._path(3.16 dev build):as_file()on a zipped package, n=1024as_file(), n=4 (100 runs, alternating, 3 batches)New test
test_recursive_iterdir_workcountsPathobjects built while walking a 32-directory archive:AssertionError: assert 2113 <= (4 * 32)tox -e py314: 115 passed, ruff clean. In CPython,test_zipfile+test_importlibwith the patch: 952 run, 42 skipped, all pass.Merge Danger
Door: two-way
The index is private, cached per
FastLookupinstance, and built on firstiterdir().Blast Radius: memory
Each read-only archive that calls
iterdir()now keeps one extra dict. On the 1,024-directory walk (CPython build), retained traversal memory went from 79 KB to 262 KB and peak from 178 KB to 364 KB. Archives that never calliterdir()don't pay anything.AI disclosure: the profiling and the patch were done with AI tools (Codex, Claude).
🤖 Generated with Claude Code