GH-119518: Stop interning strings in pathlib GH-123356)

Remove `sys.intern(str(x))` calls when normalizing a path in pathlib. This
speeds up `str(Path('foo/bar'))` by about 10%.
This commit is contained in:
Barney Gale 2024-09-02 17:14:09 +01:00 committed by GitHub
parent 77a2fb4bf1
commit 5002f17794
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 3 additions and 11 deletions

View File

@ -272,8 +272,7 @@ class PurePath(PurePathBase):
elif len(drv_parts) == 6: elif len(drv_parts) == 6:
# e.g. //?/unc/server/share # e.g. //?/unc/server/share
root = sep root = sep
parsed = [sys.intern(str(x)) for x in rel.split(sep) if x and x != '.'] return drv, root, [x for x in rel.split(sep) if x and x != '.']
return drv, root, parsed
@property @property
def _raw_path(self): def _raw_path(self):

View File

@ -163,15 +163,6 @@ class PurePathTest(test_pathlib_abc.DummyPurePathTest):
# Special case for the empty path. # Special case for the empty path.
self._check_str('.', ('',)) 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): def test_join_nested(self):
P = self.cls P = self.cls
p = P('a/b').joinpath(P('c')) p = P('a/b').joinpath(P('c'))

View File

@ -0,0 +1,2 @@
Speed up normalization of :class:`pathlib.PurePath` and
:class:`~pathlib.Path` objects by not interning string parts.