Skip to content

GH-104898: Revert pathlib os.PathLike registration change. - #105073

Merged
barneygale merged 2 commits into
python:mainfrom
barneygale:gh-104898-partial-revert
May 29, 2023
Merged

GH-104898: Revert pathlib os.PathLike registration change.#105073
barneygale merged 2 commits into
python:mainfrom
barneygale:gh-104898-partial-revert

Conversation

@barneygale

@barneygalebarneygale commented May 29, 2023

Copy link
Copy Markdown
Contributor

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.
Comment threadLib/pathlib.py Outdated
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@barneygale
barneygale enabled auto-merge (squash) May 29, 2023 21:19
Comment threadLib/pathlib.py

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

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.

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:mainMay 29, 2023
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performancePerformance or resource usageskip newstopic-pathlib

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@barneygale@merwok@AlexWaygood@bedevere-bot