summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-04-20 16:46:52 (GMT)
committerGitHub <noreply@github.com>2024-04-20 16:46:52 (GMT)
commit15fbd53ba96be4b6a5abd94ceada684493c36bdd (patch)
treef01a3b0fa201846117295ca3540e267f28136bcf
parentd8f350309ded3130c43f0d2809dcb8ec13112320 (diff)
downloadcpython-15fbd53ba96be4b6a5abd94ceada684493c36bdd.zip
cpython-15fbd53ba96be4b6a5abd94ceada684493c36bdd.tar.gz
cpython-15fbd53ba96be4b6a5abd94ceada684493c36bdd.tar.bz2
GH-112855: Speed up `pathlib.PurePath` pickling (#112856)
The second item in the tuple returned from `__reduce__()` is a tuple of arguments to supply to path constructor. Previously we returned the `parts` tuple here, which entailed joining, parsing and normalising the path object, and produced a compact pickle representation. With this patch, we instead return a tuple of paths that were originally given to the path constructor. This makes pickling much faster (at the expense of compactness). It's worth noting that, in the olden times, pathlib performed this parsing/normalization up-front in every case, and so using `parts` for pickling was almost free. Nowadays pathlib only parses/normalises paths when it's necessary or advantageous to do so (e.g. computing a path parent, or iterating over a directory, respectively).
-rw-r--r--Lib/pathlib/__init__.py4
-rw-r--r--Misc/NEWS.d/next/Library/2023-12-07-20-05-54.gh-issue-112855.ph4ehh.rst2
2 files changed, 3 insertions, 3 deletions
diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py
index a4721fb..f03f317 100644
--- a/Lib/pathlib/__init__.py
+++ b/Lib/pathlib/__init__.py
@@ -169,9 +169,7 @@ class PurePath(_abc.PurePathBase):
return NotImplemented
def __reduce__(self):
- # Using the parts tuple helps share interned path parts
- # when pickling related paths.
- return (self.__class__, self.parts)
+ return self.__class__, tuple(self._raw_paths)
def __repr__(self):
return "{}({!r})".format(self.__class__.__name__, self.as_posix())
diff --git a/Misc/NEWS.d/next/Library/2023-12-07-20-05-54.gh-issue-112855.ph4ehh.rst b/Misc/NEWS.d/next/Library/2023-12-07-20-05-54.gh-issue-112855.ph4ehh.rst
new file mode 100644
index 0000000..6badc7a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-12-07-20-05-54.gh-issue-112855.ph4ehh.rst
@@ -0,0 +1,2 @@
+Speed up pickling of :class:`pathlib.PurePath` objects. Patch by Barney
+Gale.