Skip to content

GH-113225: Speed up pathlib.Path.walk(top_down=False)#113693

Merged
barneygale merged 4 commits into
python:mainfrom
barneygale:gh-113225-walk-bottom-up
Jan 20, 2024
Merged

GH-113225: Speed up pathlib.Path.walk(top_down=False)#113693
barneygale merged 4 commits into
python:mainfrom
barneygale:gh-113225-walk-bottom-up

Conversation

@barneygale

@barneygale barneygale commented Jan 3, 2024

Copy link
Copy Markdown
Contributor

Use _make_child_entry() rather than _make_child_relpath() to retrieve path objects for directories to visit.

In user subclasses of PathBase, this saves the allocation of one path object per directory. In Path it saves a small amount path string generation.

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.

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.
@barneygale barneygale changed the title GH-113225: Speed up pathlib._abc.PathBase.walk(top_down=False) GH-113225: Speed up pathlib.Path.walk(top_down=False) Jan 4, 2024
@barneygale

Copy link
Copy Markdown
Contributor Author

About 2% faster:

$ ./python -m timeit -s "from pathlib import Path" "list(Path().walk(top_down=False))"
10 loops, best of 5: 27.5 msec per loop  # before
10 loops, best of 5: 27   msec per loop  # after

$  ./python -m timeit -s "from pathlib import Path" "list(Path.cwd().walk(top_down=False))"
10 loops, best of 5: 28.2 msec per loop  # before
10 loops, best of 5: 27.5 msec per loop  # after

@barneygale
barneygale enabled auto-merge (squash) January 20, 2024 02:47
@barneygale
barneygale merged commit 1e610fb into python:main Jan 20, 2024
kulikjak pushed a commit to kulikjak/cpython that referenced this pull request Jan 22, 2024
…#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.
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
…#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.
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
…#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance or resource usage topic-pathlib

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant