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

Improve performance of pathlib.Path.[is_]absolute() #103548

Closed
barneygale opened this issue Apr 14, 2023 · 1 comment
Closed

Improve performance of pathlib.Path.[is_]absolute() #103548

barneygale opened this issue Apr 14, 2023 · 1 comment
Labels
3.12 bugs and security fixes performance Performance or resource usage topic-pathlib type-feature A feature request or enhancement

Comments

@barneygale
Copy link
Contributor

barneygale commented Apr 14, 2023

Two optimizations are possible:

  • is_absolute() can pass the unnormalized path to os.path.isabs()
  • absolute() on an empty path (or Path.cwd()) can avoid joining an empty string onto the working directory (thus avoiding a call to os.path.join()), and short-circuit the string normalization (as the result of getcwd() is fully normalized)

Linked PRs

@barneygale barneygale added type-feature A feature request or enhancement performance Performance or resource usage 3.12 bugs and security fixes topic-pathlib labels Apr 14, 2023
barneygale added a commit to barneygale/cpython that referenced this issue Apr 14, 2023
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`.
barneygale added a commit to barneygale/cpython that referenced this issue Apr 29, 2023
barneygale added a commit to barneygale/cpython that referenced this issue May 2, 2023
barneygale added a commit to barneygale/cpython that referenced this issue May 6, 2023
barneygale added a commit that referenced this issue May 6, 2023
…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.
@barneygale
Copy link
Contributor Author

Done in 3.12: #103549 / de7f694

jbower-fb pushed a commit to jbower-fb/cpython-jbowerfb that referenced this issue 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

No branches or pull requests

1 participant