Skip to content

Improve performance of pathlib.Path.absolute() #100562

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

Closed
barneygale opened this issue Dec 28, 2022 · 0 comments · Fixed by #100563
Closed

Improve performance of pathlib.Path.absolute() #100562

barneygale opened this issue Dec 28, 2022 · 0 comments · Fixed by #100563
Labels
3.12 only security fixes performance Performance or resource usage topic-pathlib type-feature A feature request or enhancement

Comments

@barneygale
Copy link
Contributor

barneygale commented Dec 28, 2022

The current implementation of pathlib.Path.absolute() calls self.cwd() rather than os.getcwd(), and so constructs two Path objects rather than one. As path objects are slow to construct, this has a performance impact.

Linked PRs

@barneygale barneygale added the type-feature A feature request or enhancement label Dec 28, 2022
@AlexWaygood AlexWaygood added performance Performance or resource usage topic-pathlib 3.12 only security fixes labels Dec 28, 2022
barneygale added a commit to barneygale/cpython that referenced this issue Dec 28, 2022
Increase performance of the `absolute()` method by calling `os.getcwd()`
directly, rather than using the `Path.cwd()` class method. This avoids
constructing an extra `Path` object (and the parsing/normalization that
comes with it).

Decrease performance of the `cwd()` class method by calling the
`Path.absolute()` method, rather than using `os.getcwd()` directly. This
involves constructing an extra `Path` object. We do this to maintain a
longstanding pattern where `os` functions are called from only one place,
which allows them to be more readily replaced by users. As `cwd()` is
generally called at most once within user programs, it's a good bargain.
AlexWaygood added a commit to barneygale/cpython that referenced this issue Jan 5, 2023
miss-islington pushed a commit that referenced this issue Jan 5, 2023
Increase performance of the `absolute()` method by calling `os.getcwd()` directly, rather than using the `Path.cwd()` class method. This avoids constructing an extra `Path` object (and the parsing/normalization that comes with it).

Decrease performance of the `cwd()` class method by calling the `Path.absolute()` method, rather than using `os.getcwd()` directly. This involves constructing an extra `Path` object. We do this to maintain a longstanding pattern where `os` functions are called from only one place, which allows them to be more readily replaced by users. As `cwd()` is generally called at most once within user programs, it's a good bargain.

```shell
# before
$ ./python -m timeit -s 'from pathlib import Path; p = Path("foo", "bar")' 'p.absolute()'
50000 loops, best of 5: 9.04 usec per loop
# after
$ ./python -m timeit -s 'from pathlib import Path; p = Path("foo", "bar")' 'p.absolute()'
50000 loops, best of 5: 5.02 usec per loop
```

Automerge-Triggered-By: GH:AlexWaygood
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes performance Performance or resource usage topic-pathlib type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants