Repository navigation
Support for sharing state between pathlib subclasses #100479
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancement
on Dec 23, 2022 This will be needed to implement
zipfile.Pathusingpathlib. Observe that it already has a_next()method that works exactly like what I'm proposing here:Lines 296 to 297 in a68e585
def _next(self, at): return self.__class__(self.root, at) Exactly how big would the performance regression be? Have you tried running some benchmarks?
Not yet; I'll do this before I mark the PR as 'ready' and share the numbers.
Reacted by Alex WaygoodMy instinct is to say that it would be a shame to make pathlib performance worse, since it's already got a reputation in some quarters for having surprisingly bad performance in some ways compared to
os.path. But it's hard to evaluate without knowing how big the performance regression would be, and in what situations you'd hit the regression :)few folks reach to pathlib for reason of speed.
In my opinion, we should be trying to fix this rather than exacerbating the problem :)
Reacted by Barney GaleAbsolutely fair!
Do you know of particular cases of (or complaints about) pathlib being slow? I'm aware that recursive globbing in pathlib can be slow, but not much else. We're increasingly calling
os.pathfunctions, some of which have C implementations, which should improve performance a little.I'm of course keen for pathlib to be as fast as possible, but I don't want to prevent the future addition of an
AbstractPathclass either.Reacted by Alex WaygoodDo you know of particular cases of (or complaints about) pathlib being slow?
- optimized implementation of pathlib faster-cpython/ideas#194
- cache significantly slows down black due to pathlib psf/black#1950
- https://m.youtube.com/watch?v=tFrh9hKMS6Y
- https://m.youtube.com/watch?v=qiZyDLEJHh0&feature=youtu.be
I have no idea how many of these complaints, if any, still apply to the current implementation of pathlib, nor how many of these are realistically resolvable :)
Reacted by Barney GaleMega, thank you. I'll study these carefully and write benchmarks.
Some initial thoughts:
- The performance cost of constructing
Pathobjects (vs constructing strings) stands out. This would surely benefit from an optimization pass, though it's unlikely we'll ever approachstrconstruction performance. - The change to the
parentsimplementation in GH-100479: Add optionalblueprintargument topathlib.PurePath#100481 may improve performance for some of these - I'll check. - A C implementation of
pathlibwould be wonderful but I suspect quite difficult! C implementations of selectos.pathfunctions would also help. - Caching
stat()results would break user code, but there's a possible alternative: we could introduce something like astatus()method that returns a new object withexists,is_dir, etc, attributes. Users could callpath.status()once and check its attributes, rather than callingpath.is_dir(),path.is_symlink(), etc, each of which would trigger their ownstat()call.
Reacted by Alex Waygood- The performance cost of constructing
- added a commit that references this issue
on Jan 5, 2023 - added a commit that references this issue
on Apr 3, 2023 I'll try to take a look soon!
Reacted by Barney Gale- added 4 commits that reference this issue
on May 2, 2023 - added a commit that references this issue
on May 5, 2023 - added a commit that references this issue
on May 5, 2023 - added a commit that references this issue
on May 7, 2023
Feature or enhancement
This enhancement proposes that we allow state to be shared between related instances of subclasses of
pathlib.PurePathandpathlib.Path.Pitch
Now that #68320 is resolved, users can subclass
pathlib.PurePathandpathlib.Pathdirectly:However, some user implementations of classes - such as
TarPathorS3Path- would require underlyingtarfile.TarFileorbotocore.Resourceobjects to be shared between path objects (etc and etc_hosts in the example above). Such sharing of resources is presently rather difficult, as there's no single instance method used to derive new path objects.This feature request proposes that we add a new
PurePath.makepath()method, which is called whenever one path object is derived from another, such as injoinpath(),iterdir(), etc. The default implementation of this method looks something like:Users may redefine this method in a subclass, in conjunction with a customized initializer:
I propose the name "makepath" for this method due to its close relationship with the existing "joinpath" method:
a.joinpath(b) == a.makepath(a, b).Performance
edit: this change has been taken care of elsewhere, and so implementing this feature request should no longer have much affect on performance.
This change will affect the performance of some pathlib operations, because it requires us to remove the_from_parsed_parts()constructor, which is an internal optimization used in cases where path parsing and normalization can be skipped (for example, in theparentssequence). I suggest that, within the standard library, pathlib is not a particularly performance-sensitive module -- few folks reach to pathlib for reason of speed. Within pathlib itself, the savings from optimizing these "pure" methods are usually drowned out by the I/O costs of "impure" methods. With the appeal of this feature in mind, I believe the performance cost is justified.However, if the performance degradation is considered unacceptable, there's a possible alternative: add a normalize keyword argument to the path initializer and tomakepath(). This would require some serious internal surgery to make work, and might be difficult to communicate to users, so at this stage it's not my preferred route forward.Previous discussion
https://discuss.python.org/t/make-pathlib-extensible/3428/47 (and replies)
Linked PRs
blueprintargument topathlib.PurePath#100481pathlib.PurePath.with_segments()#103975