Skip to content

Conversation

@barneygale
Copy link
Contributor

@barneygalebarneygale 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.
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@barneygalebarneygale enabled auto-merge (squash) May 29, 2023 21:19

# 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.

@barneygalebarneygale 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