]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
GH-113225: Speed up `pathlib.Path.walk(top_down=False)` (#113693)
authorBarney Gale <barney.gale@gmail.com>
Sat, 20 Jan 2024 03:06:00 +0000 (03:06 +0000)
committerGitHub <noreply@github.com>
Sat, 20 Jan 2024 03:06:00 +0000 (03:06 +0000)
commit1e610fb05fa4ba61a759b68461f1a9aed07622fc
tree6844d00162d805f022f6f5b69ead966c04e32549
parent6313cdde58f34648a430d2830357c9d2a5b67b87
GH-113225: Speed up `pathlib.Path.walk(top_down=False)` (#113693)

Use `_make_child_entry()` rather than `_make_child_relpath()` to retrieve
path objects for directories to visit. This saves the allocation of one
path object per directory in user subclasses of `PathBase`, and avoids a
second loop.

This trick does not apply when walking top-down, because users can affect
the walk by modifying *dirnames* in-place.

A side effect of this change is that, in bottom-up mode, subdirectories of
each directory are visited in reverse order, and that this order doesn't
match that of the names in *dirnames*. I suspect this is fine as the
order is arbitrary anyway.
Lib/pathlib/_abc.py
Misc/NEWS.d/next/Library/2024-01-04-20-58-17.gh-issue-113225.-nyJM4.rst [new file with mode: 0644]