]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-119518: Stop interning strings in pathlib GH-123356)
authorBarney Gale <barney.gale@gmail.com>
Mon, 2 Sep 2024 16:14:09 +0000 (17:14 +0100)
committerGitHub <noreply@github.com>
Mon, 2 Sep 2024 16:14:09 +0000 (18:14 +0200)
Remove `sys.intern(str(x))` calls when normalizing a path in pathlib. This
speeds up `str(Path('foo/bar'))` by about 10%.

Lib/pathlib/_local.py
Lib/test/test_pathlib/test_pathlib.py
Misc/NEWS.d/next/Library/2024-08-26-18-48-13.gh-issue-119518.QFYH9q.rst [new file with mode: 0644]

index 51abe58410bc7c832e76bc68e0d5af0996c76835..1c02e4168d3a9e74ebc7930852295df3fc1ced30 100644 (file)
@@ -272,8 +272,7 @@ class PurePath(PurePathBase):
             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):
index f4449b1a40d899449ea5125f9c2cab15f084e67b..b47b4a194cfaa91aac162daf181bffc8f3686056 100644 (file)
@@ -163,15 +163,6 @@ class PurePathTest(test_pathlib_abc.DummyPurePathTest):
         # 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'))
diff --git a/Misc/NEWS.d/next/Library/2024-08-26-18-48-13.gh-issue-119518.QFYH9q.rst b/Misc/NEWS.d/next/Library/2024-08-26-18-48-13.gh-issue-119518.QFYH9q.rst
new file mode 100644 (file)
index 0000000..819295f
--- /dev/null
@@ -0,0 +1,2 @@
+Speed up normalization of :class:`pathlib.PurePath` and
+:class:`~pathlib.Path` objects by not interning string parts.