From 264b8d5efa239a215a078f50bf73a280f56b28f6 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Wed, 22 Dec 2021 17:10:42 +0530 Subject: [PATCH 1/4] optimize pathlib --- Lib/pathlib.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 621fba0e75c0f7..5d6040a1b7665c 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -8,8 +8,7 @@ import sys import warnings from _collections_abc import Sequence -from errno import EINVAL, ENOENT, ENOTDIR, EBADF, ELOOP -from operator import attrgetter +from errno import ENOENT, ENOTDIR, EBADF, ELOOP from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO from urllib.parse import quote_from_bytes as urlquote_from_bytes @@ -364,7 +363,7 @@ def group(self, path): # # Globbing helpers # - +@functools.lru_cache def _make_selector(pattern_parts, flavour): pat = pattern_parts[0] child_parts = pattern_parts[1:] @@ -378,9 +377,6 @@ def _make_selector(pattern_parts, flavour): cls = _PreciseSelector return cls(pat, child_parts, flavour) -if hasattr(functools, "lru_cache"): - _make_selector = functools.lru_cache()(_make_selector) - class _Selector: """A selector matches a specific glob pattern part against the children @@ -693,11 +689,15 @@ def __ge__(self, other): def __class_getitem__(cls, type): return cls - drive = property(attrgetter('_drv'), - doc="""The drive prefix (letter or UNC path), if any.""") + @property + def drive(self): + """The drive prefix (letter or UNC path), if any.""" + return self._drv - root = property(attrgetter('_root'), - doc="""The root of the path, if any.""") + @property + def root(self): + """The root of the path, if any.""" + return self._root @property def anchor(self): From 0698384f0adaa129ef00b5ea2fb1064cb0df77db Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Wed, 5 Jan 2022 16:31:12 +0530 Subject: [PATCH 2/4] cache with functools --- Lib/pathlib.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 200ebb67ff270c..4ec6b91c42f7b6 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -561,6 +561,7 @@ def __reduce__(self): return (self.__class__, tuple(self._parts)) @classmethod + @functools.cache def _parse_args(cls, args): # This is useful when you don't want to create an instance, just # canonicalize some constructor arguments. From 2657c1d71e27b2d11e61a2f55638a4b27f8fd652 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Wed, 5 Jan 2022 16:59:15 +0530 Subject: [PATCH 3/4] fix tests --- Lib/pathlib.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 4ec6b91c42f7b6..21424ced367653 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -857,7 +857,7 @@ def __truediv__(self, key): def __rtruediv__(self, key): try: - return self._from_parts([key] + self._parts) + return self._from_parts((key, *self._parts)) except TypeError: return NotImplemented @@ -1058,7 +1058,7 @@ def absolute(self): return self # FIXME this must defer to the specific flavour (and, under Windows, # use nt._getfullpathname()) - return self._from_parts([self._accessor.getcwd()] + self._parts) + return self._from_parts((self._accessor.getcwd(), *self._parts)) def resolve(self, strict=False): """ @@ -1437,7 +1437,7 @@ def expanduser(self): homedir = self._accessor.expanduser(self._parts[0]) if homedir[:1] == "~": raise RuntimeError("Could not determine home directory.") - return self._from_parts([homedir] + self._parts[1:]) + return self._from_parts((homedir, *self._parts[1:])) return self From 60cb8dc839f278addce1a9c09f27b78067436570 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 5 Jan 2022 11:36:29 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2022-01-05-11-36-28.bpo-46148.FCtoon.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-01-05-11-36-28.bpo-46148.FCtoon.rst diff --git a/Misc/NEWS.d/next/Library/2022-01-05-11-36-28.bpo-46148.FCtoon.rst b/Misc/NEWS.d/next/Library/2022-01-05-11-36-28.bpo-46148.FCtoon.rst new file mode 100644 index 00000000000000..1e6f2b48f4a66b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-01-05-11-36-28.bpo-46148.FCtoon.rst @@ -0,0 +1 @@ +Speedup :class:`~pathlib.Path` creation up to ``3x``. Patch by Kumar Aditya. \ No newline at end of file