Skip to content

Commit

Permalink
GH-101362: Call join() only when >1 argument supplied to pathlib.Pure…
Browse files Browse the repository at this point in the history
…Path() (#101665)

GH-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 Mar 5, 2023
1 parent 96e1022 commit 3572c86
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 3572c86

Please sign in to comment.