Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15fbd53

Browse files
authored
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).
1 parent d8f3503 commit 15fbd53

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

Lib/pathlib/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,7 @@ def __rtruediv__(self, key):
169169
return NotImplemented
170170

171171
def __reduce__(self):
172-
# Using the parts tuple helps share interned path parts
173-
# when pickling related paths.
174-
return (self.__class__, self.parts)
172+
return self.__class__, tuple(self._raw_paths)
175173

176174
def __repr__(self):
177175
return "{}({!r})".format(self.__class__.__name__, self.as_posix())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Speed up pickling of :class:`pathlib.PurePath` objects. Patch by Barney
2+
Gale.

0 commit comments

Comments
 (0)