Skip to content

Commit

Permalink
GH-119518: Stop interning strings in pathlib GH-123356)
Browse files Browse the repository at this point in the history
Remove `sys.intern(str(x))` calls when normalizing a path in pathlib. This
speeds up `str(Path('foo/bar'))` by about 10%.
  • Loading branch information
barneygale authored Sep 2, 2024
1 parent 77a2fb4 commit 5002f17
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,7 @@ def _parse_path(cls, path):
elif len(drv_parts) == 6:
# e.g. //?/unc/server/share
root = sep
parsed = [sys.intern(str(x)) for x in rel.split(sep) if x and x != '.']
return drv, root, parsed
return drv, root, [x for x in rel.split(sep) if x and x != '.']

@property
def _raw_path(self):
Expand Down
9 changes: 0 additions & 9 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,6 @@ def test_empty_path(self):
# Special case for the empty path.
self._check_str('.', ('',))

def test_parts_interning(self):
P = self.cls
p = P('/usr/bin/foo')
q = P('/usr/local/bin')
# 'usr'
self.assertIs(p.parts[1], q.parts[1])
# 'bin'
self.assertIs(p.parts[2], q.parts[3])

def test_join_nested(self):
P = self.cls
p = P('a/b').joinpath(P('c'))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speed up normalization of :class:`pathlib.PurePath` and
:class:`~pathlib.Path` objects by not interning string parts.

0 comments on commit 5002f17

Please sign in to comment.