Skip to content

Commit

Permalink
pythonGH-101362: Call join() only when >1 argument supplied to pathli…
Browse files Browse the repository at this point in the history
…b.PurePath() (python#101665)

pythonGH-101362: Call join() only when >1 argument supplied to pathlib.PurePath

This reduces the time taken to run `PurePath("foo")` by ~15%
  • Loading branch information
barneygale authored and hugovk committed Mar 6, 2023
1 parent 30b2feb commit 07eea3e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,12 @@ def __reduce__(self):
def _parse_parts(cls, parts):
if not parts:
return '', '', []
elif len(parts) == 1:
path = os.fspath(parts[0])
else:
path = cls._flavour.join(*parts)
sep = cls._flavour.sep
altsep = cls._flavour.altsep
path = cls._flavour.join(*parts)
if altsep:
path = path.replace(altsep, sep)
drv, root, rel = cls._flavour.splitroot(path)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speed up :class:`pathlib.PurePath` construction by calling
:func:`os.path.join` only when two or more arguments are given.

0 comments on commit 07eea3e

Please sign in to comment.