]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-101362: Call join() only when >1 argument supplied to pathlib.PurePath() (#101665)
authorBarney Gale <barney.gale@gmail.com>
Sun, 5 Mar 2023 22:00:56 +0000 (22:00 +0000)
committerGitHub <noreply@github.com>
Sun, 5 Mar 2023 22:00:56 +0000 (22:00 +0000)
GH-101362: Call join() only when >1 argument supplied to pathlib.PurePath

This reduces the time taken to run `PurePath("foo")` by ~15%

Lib/pathlib.py
Misc/NEWS.d/next/Library/2023-02-07-21-16-41.gh-issue-101362.KMQllM.rst [new file with mode: 0644]

index dde573592fddced830d4cf62f8b58cd94a385a40..ed0f2cc73dfa691a9615c37a0f600ef5af5b40b0 100644 (file)
@@ -275,9 +275,12 @@ class PurePath(object):
     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)
diff --git a/Misc/NEWS.d/next/Library/2023-02-07-21-16-41.gh-issue-101362.KMQllM.rst b/Misc/NEWS.d/next/Library/2023-02-07-21-16-41.gh-issue-101362.KMQllM.rst
new file mode 100644 (file)
index 0000000..af4ee9a
--- /dev/null
@@ -0,0 +1,2 @@
+Speed up :class:`pathlib.PurePath` construction by calling
+:func:`os.path.join` only when two or more arguments are given.