Closed
Labels
performancePerformance or resource usagePerformance or resource usagetopic-pathlibtype-featureA feature request or enhancementA feature request or enhancement
Description
Pathlib is slow. One of the most obvious symptoms is that pathlib.PurePath objects are slow to construct. We should be able to speed construction up without making other parts of pathlib slower.
Two possible approaches:
- Optimize the existing machinary of path construction:
__new__(),_from_parts(),_parse_parts(),_parse_args(). - Perform less work in the constructor: defer parsing, joining and normalization until needed.
Linked PRs
- GH-101362: Optimise pathlib by deferring path normalisation #101560
- GH-101362: Check pathlib.Path flavour compatibility at import time #101664
- GH-101362: Call join() only when >1 argument supplied to pathlib.PurePath() #101665
- GH-101362: Optimise PurePath(PurePath(...)) #101667
- GH-101362: Omit path anchor from
pathlib.PurePath()._parts#102476
Reacted by Alex Waygood, sobolevn, Jacob Nilsson, ProgramRipper, sigma67, Gabriele N. Tornetta and tblazejczyk-qblox
Metadata
Metadata
Assignees
Labels
performancePerformance or resource usagePerformance or resource usagetopic-pathlibtype-featureA feature request or enhancementA feature request or enhancement
Activity
barneygale commentedon Jan 29, 2023
I'd like to land #101363 before I put the first PR up for this issue.
pythonGH-101362 - Optimise pathlib by deferring path normalisation
pythonGH-101362: Check pathlib.Path flavour compatibility at import time
pythonGH-101362: Check pathlib.Path flavour compatibility at import time
pythonGH-101362: Call join() only when >1 argument supplied to pathli…
pythonGH-101362: Optimise PurePath(PurePath(...))
zooba commentedon Feb 22, 2023
Does the PR cope with
WindowsPurePathandPosixPurePathand converting between them?barneygale commentedon Feb 22, 2023
Could you clarify? The PRs maintain the behaviour that attempting to instantiate
pathlib.PurePathwill give you aPureWindowsPathor aPurePosixPathdepending on your platform, but pathlib doesn't support converting between Windows and POSIX paths as I understand it.zooba commentedon Feb 24, 2023
This behaviour should be preserved:
barneygale commentedon Feb 24, 2023
Right! That will be broken by #101667 as things stand:
It doesn't appear to be documented or tested behaviour, and it feels odd to me that
PurePosixPath(p)can give a different result toPurePosixPath(fspath(p)). I'll try to find a good way forwards!barneygale commentedon Feb 24, 2023
This feature doesn't really work with drives or roots:
As far as I can tell, no one has ever logged a bug about it.
However, using
PurePosixPath(PureWindowsPath(...).as_posix())works everywhere:So I'm tempted to conclude that converting with
PurePosixPath(PureWindowsPath(...))alone is not supported, and that it only happened to work in some limited circumstances. Users can add.as_posix()to signal their intent and make it work reliably. What do you reckon?zooba commentedon Feb 24, 2023
I think the direct conversion should either be consistent with
fspath(p)orp.parts, but no strong preference which. As you say, neither makes a huge amount of sense, and the workaround (.as_posix()) has been there for a long time.Might be worth adding a short note to the docs warning that the constructor can't reliably convert from different
pathlibobjects, and maybe specify whether it willfspathor.partsthem.On another perf note, is it possible that parsing the path up front isn't necessary? Obviously it'll save the most time to keep a single string literal around and parse it later, but I don't personally have a good feel for whether that's common or not. (Obviously if it's available pre-parsed then keep it.)
7 remaining items
pythonGH-101362: Check pathlib.Path flavour compatibility at import t…
pythonGH-101362: Optimise PurePath(PurePath(...)) (pythonGH-101667)
pythonGH-101362: Omit path anchor from `pathlib.PurePath()._parts`
Merge branch 'main' into nodeaddict
Merge branch 'main' into inlinecomp2
Merge branch 'main' into pythongh-101362-exclude-anchor-from-parts
barneygale commentedon Mar 22, 2023
For anyone following along, I think this ticket can be resolved if/when this PR lands:
pathlib.PurePath()._parts#102476Merge branch 'main' into pythongh-101362-exclude-anchor-from-parts
Merge branch 'main' into pythongh-101362-exclude-anchor-from-parts
GH-101362: Omit path anchor from `pathlib.PurePath()._parts` (GH-102476)
barneygale commentedon Apr 9, 2023
Resolving this issue. It's now much cheaper to construct
pathlib.PurePathandPathobjects. Some operations likestr()andjoinpath()are also cheaper.Future work:
pathlib.PurePath.__fspath__()#102783pythonGH-101362: Omit path anchor from `pathlib.PurePath()._parts` (p…
pythonGH-101362: Omit path anchor from `pathlib.PurePath()._parts` (p…
Replace regex approach to ls-files output parsing