FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GH-112361: Speed up pathlib by removing some temporary objects. by barneygale · Pull Request #112362 · python/cpython · GitHub

/ cpython Public

GH-112361: Speed up pathlib by removing some temporary objects. - #112362

Merged
barneygale merged 2 commits into
python:mainfrom
barneygale:optimise-pathlib-list-copies
Nov 25, 2023
Merged

GH-112361: Speed up pathlib by removing some temporary objects.#112362
barneygale merged 2 commits into
python:mainfrom
barneygale:optimise-pathlib-list-copies

Conversation

barneygale commented Nov 24, 2023
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Contributor

Construct only one new list object (using list.copy()) when creating a new path object with a modified tail. This slightly speeds up with_name(), with_suffix(), _make_child_relpath() (used in walking and globbing), and glob().

Construct only one new list object (using `list.copy()`) when creating a
new path object with a modified tail. This slightly speeds up
`with_name()`, `with_suffix()`, `_make_child_relpath()` (used in walking
and globbing), and `glob()`.

Copy link
Copy Markdown
Contributor Author

Timings:

$ ./python -m timeit -n 1000000 -s "from pathlib import Path; p = Path('foo')" "p.with_name('bar')"
1000000 loops, best of 5: 1.44 usec per loop  # before
1000000 loops, best of 5: 1.31 usec per loop  # after

$ ./python -m timeit -n 1000000 -s "from pathlib import Path; p = Path('foo.txt')" "p.with_suffix('.zip')"
1000000 loops, best of 5: 2.02 usec per loop  # before
1000000 loops, best of 5: 1.83 usec per loop  # after

$ ./python -m timeit -n 1000000 -s "from pathlib import Path; p = Path('foo.txt')" "p.with_suffix('')"
1000000 loops, best of 5: 1.88 usec per loop  # before
1000000 loops, best of 5: 1.68 usec per loop  # after

$ ./python -m timeit -n 1000000 -s "from pathlib import Path; p = Path('foo')" "p._make_child_relpath('bar')"
1000000 loops, best of 5: 1.14 usec per loop  # before
1000000 loops, best of 5: 1.11 usec per loop  # after

AlexWaygood left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Overall this looks really nice -- great work! I can reproduce the big speedups from the first three benchmarks in #112362 (comment).

I can't repro the reported speedup for _make_child_relpath, though (the fourth benchmark from #112362 (comment)) -- if anything, it looks like this PR makes it slightly slower for me locally? (I'm on Windows with a fresh PGO-optimised build from main.)

Comment thread Lib/test/test_pathlib.py

Copy link
Copy Markdown
Contributor Author

I can't repro the reported speedup for _make_child_relpath, though (the fourth benchmark from #112362 (comment)) -- if anything, it looks like this PR makes it slightly slower for me locally? (I'm on Windows with a fresh PGO-optimised build from main.)

Hm. With a fresh PGO rebuild on Linux I still see an improvement, but it's really marginal - only 2% faster. I'll take the change out of the PR.

AlexWaygood left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Looks great, thanks!

Copy link
Copy Markdown
Contributor Author

Thank you for the review Alex!

aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
…python#112362)

Construct only one new list object (using `list.copy()`) when creating a
new path object with a modified tail. This slightly speeds up
`with_name()` and `with_suffix()`
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
…python#112362)

Construct only one new list object (using `list.copy()`) when creating a
new path object with a modified tail. This slightly speeds up
`with_name()` and `with_suffix()`
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

Labels

performance Performance or resource usage topic-pathlib

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL