https://github.com/python/cpython/commit/5002f17794a9f403540305c733698d1e01699490
commit: 5002f17794a9f403540305c733698d1e01699490
branch: main
author: Barney Gale <[email protected]>
committer: encukou <[email protected]>
date: 2024-09-02T18:14:09+02:00
summary:

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%.

files:
A Misc/NEWS.d/next/Library/2024-08-26-18-48-13.gh-issue-119518.QFYH9q.rst
M Lib/pathlib/_local.py
M Lib/test/test_pathlib/test_pathlib.py

diff --git a/Lib/pathlib/_local.py b/Lib/pathlib/_local.py
index 51abe58410bc7c..1c02e4168d3a9e 100644
--- a/Lib/pathlib/_local.py
+++ b/Lib/pathlib/_local.py
@@ -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):
diff --git a/Lib/test/test_pathlib/test_pathlib.py 
b/Lib/test/test_pathlib/test_pathlib.py
index f4449b1a40d899..b47b4a194cfaa9 100644
--- a/Lib/test/test_pathlib/test_pathlib.py
+++ b/Lib/test/test_pathlib/test_pathlib.py
@@ -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'))
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
index 00000000000000..819295f7306fa4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-08-26-18-48-13.gh-issue-119518.QFYH9q.rst
@@ -0,0 +1,2 @@
+Speed up normalization of :class:`pathlib.PurePath` and
+:class:`~pathlib.Path` objects by not interning string parts.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to