Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-103548: Improve performance of pathlib.Path.[is_]absolute() #103549

Merged
merged 10 commits into from
May 6, 2023

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented Apr 14, 2023

Pass the unnormalized path to os.path.isabs():

$ ./python -m timeit -s 'from pathlib import Path; p = Path("/etc")' 'p.is_absolute()'
1000000 loops, best of 5: 375 nsec per loop  # before
1000000 loops, best of 5: 240 nsec per loop  # after

When making an empty path absolute, avoid calling os.path.join(), and prevent pathlib from later re-normalizing the result of os.getcwd() by storing it as path._str:

$ ./python -m timeit -s 'from pathlib import Path' 'str(Path.cwd())'
50000 loops, best of 5: 9.1 usec per loop    # before
100000 loops, best of 5: 3.73 usec per loop  # after

Pass the *unnormalized* path to `os.path.isabs()`.

When making an empty path absolute, avoid calling `os.path.join()`, and
prevent pathlib from later re-normalizing the result of `os.getcwd()` by
storing it as `path._str`.
@AlexWaygood AlexWaygood added type-feature A feature request or enhancement performance Performance or resource usage 3.12 bugs and security fixes labels May 6, 2023
Lib/pathlib.py Show resolved Hide resolved
Lib/pathlib.py Outdated Show resolved Hide resolved
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@barneygale barneygale enabled auto-merge (squash) May 6, 2023 17:37
@barneygale barneygale merged commit de7f694 into python:main May 6, 2023
jbower-fb pushed a commit to jbower-fb/cpython-jbowerfb that referenced this pull request May 8, 2023
pythonGH-103549)

Improve performance of `pathlib.Path.absolute()` and `cwd()` by joining paths only when necessary. Also improve
performance of `PurePath.is_absolute()` on Posix by skipping path parsing and normalization.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes performance Performance or resource usage topic-pathlib type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants