From 5002f17794a9f403540305c733698d1e01699490 Mon Sep 17 00:00:00 2001 From: Barney Gale Date: Mon, 2 Sep 2024 17:14:09 +0100 Subject: 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%. --- Lib/pathlib/_local.py | 3 +-- Lib/test/test_pathlib/test_pathlib.py | 9 --------- .../next/Library/2024-08-26-18-48-13.gh-issue-119518.QFYH9q.rst | 2 ++ 3 files changed, 3 insertions(+), 11 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-08-26-18-48-13.gh-issue-119518.QFYH9q.rst diff --git a/Lib/pathlib/_local.py b/Lib/pathlib/_local.py index 51abe58..1c02e41 100644 --- a/Lib/pathlib/_local.py +++ b/Lib/pathlib/_local.py @@ -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): diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index f4449b1..b47b4a1 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -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 index 0000000..819295f --- /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. -- cgit v0.12