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-104898: Revert pathlib os.PathLike registration change. #105073

Merged
merged 2 commits into from
May 29, 2023

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented May 29, 2023

Subclassing os.PathLike rather than using register() makes initialisation slower, due to the additional __isinstance__ work.

This partially reverts commit bd1b622.

$ ./python -m timeit -s 'from pathlib import PurePath' 'PurePath("a", "b", "c")'
100000 loops, best of 5: 2.12 usec per loop # before
200000 loops, best of 5: 1.69 usec per loop # after

Subclassing `os.PathLike` rather than using `register()` makes
initialisation slower, due to the additional `__isinstance__` work.

This partially reverts commit bd1b622.
Lib/pathlib.py Outdated Show resolved Hide resolved
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@barneygale barneygale enabled auto-merge (squash) May 29, 2023 21:19
@@ -715,6 +715,10 @@ def match(self, path_pattern, *, case_sensitive=None):
return False
return True

# Subclassing os.PathLike makes isinstance() checks slower,
# which in turn makes Path construction slower. Register instead!
os.PathLike.register(PurePath)
Copy link
Member

Choose a reason for hiding this comment

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

Note that you can use ABC.register as a class decorator, but I think it would not look better for this case.

@barneygale barneygale merged commit d593074 into python:main May 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage skip news topic-pathlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants