From 07cb8ff405e4decf827a581772fca2a9bec7cf93 Mon Sep 17 00:00:00 2001 From: nineteendo Date: Sun, 7 Apr 2024 20:33:06 +0200 Subject: [PATCH 1/8] Speedup `os.path.relpath()` --- Lib/ntpath.py | 21 ++++++++++++--------- Lib/posixpath.py | 12 +++++++++--- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/Lib/ntpath.py b/Lib/ntpath.py index f9f6c78566e8ed..b7d23e5b08d272 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -785,33 +785,36 @@ def realpath(path, *, strict=False): def relpath(path, start=None): """Return a relative version of a path""" path = os.fspath(path) + if not path: + raise ValueError("no path specified") + if isinstance(path, bytes): sep = b'\\' curdir = b'.' pardir = b'..' + getcwd = os.getcwdb else: sep = '\\' curdir = '.' pardir = '..' + getcwd = os.getcwd if start is None: start = curdir + else: + start = os.fspath(start) - if not path: - raise ValueError("no path specified") - - start = os.fspath(start) try: - start_abs = abspath(normpath(start)) - path_abs = abspath(normpath(path)) + start_abs = getcwd() if not start or start == curdir else abspath(start) + path_abs = abspath(path) start_drive, _, start_rest = splitroot(start_abs) path_drive, _, path_rest = splitroot(path_abs) if normcase(start_drive) != normcase(path_drive): raise ValueError("path is on mount %r, start on mount %r" % ( path_drive, start_drive)) - start_list = [x for x in start_rest.split(sep) if x] - path_list = [x for x in path_rest.split(sep) if x] + start_list = start_rest.split(sep) if start_rest else [] + path_list = path_rest.split(sep) if path_rest else [] # Work out how much of the filepath is shared by start and path. i = 0 for e1, e2 in zip(start_list, path_list): @@ -822,7 +825,7 @@ def relpath(path, start=None): rel_list = [pardir] * (len(start_list)-i) + path_list[i:] if not rel_list: return curdir - return join(*rel_list) + return sep.join(rel_list) except (TypeError, ValueError, AttributeError, BytesWarning, DeprecationWarning): genericpath._check_arg_types('relpath', path, start) raise diff --git a/Lib/posixpath.py b/Lib/posixpath.py index b7fbdff20cac99..4211b06f58576d 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -510,10 +510,12 @@ def relpath(path, start=None): curdir = b'.' sep = b'/' pardir = b'..' + getcwd = os.getcwdb else: curdir = '.' sep = '/' pardir = '..' + getcwd = os.getcwd if start is None: start = curdir @@ -521,15 +523,19 @@ def relpath(path, start=None): start = os.fspath(start) try: - start_list = [x for x in abspath(start).split(sep) if x] - path_list = [x for x in abspath(path).split(sep) if x] + start_abs = getcwd() if not start or start == curdir else abspath(start) + path_abs = abspath(path) + start_tail = start_abs.lstrip(sep) + path_tail = path_abs.lstrip(sep) + start_list = start_tail.split(sep) if start_tail else [] + path_list = path_tail.split(sep) if path_tail else [] # Work out how much of the filepath is shared by start and path. i = len(commonprefix([start_list, path_list])) rel_list = [pardir] * (len(start_list)-i) + path_list[i:] if not rel_list: return curdir - return join(*rel_list) + return sep.join(rel_list) except (TypeError, AttributeError, BytesWarning, DeprecationWarning): genericpath._check_arg_types('relpath', path, start) raise From 93f26b823ab4099ed2b500fdf24a1f133fb26aa8 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 7 Apr 2024 18:42:10 +0000 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024-04-07-18-42-09.gh-issue-117607.C978BD.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst new file mode 100644 index 00000000000000..0302072a40ec18 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst @@ -0,0 +1 @@ +Speedup :func:`os.path.relpath()` by up to 1.9 times on Windows, up to ?.? times on Unix. From 592f64bd4a447fb555a35f4e53489b6da9de9879 Mon Sep 17 00:00:00 2001 From: nineteendo Date: Sun, 7 Apr 2024 23:25:19 +0200 Subject: [PATCH 3/8] Unix compatibility --- Lib/ntpath.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/ntpath.py b/Lib/ntpath.py index b7d23e5b08d272..5cca09c01467a1 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -790,11 +790,13 @@ def relpath(path, start=None): if isinstance(path, bytes): sep = b'\\' + alt_sep = b'/' curdir = b'.' pardir = b'..' getcwd = os.getcwdb else: sep = '\\' + alt_sep = '/' curdir = '.' pardir = '..' getcwd = os.getcwd @@ -805,7 +807,11 @@ def relpath(path, start=None): start = os.fspath(start) try: - start_abs = getcwd() if not start or start == curdir else abspath(start) + if not start or start == curdir: + # Replacing / with \ is necessary for compatibily on Unix + start_abs = getcwd().replace(alt_sep, sep) + else: + start_abs = abspath(start) path_abs = abspath(path) start_drive, _, start_rest = splitroot(start_abs) path_drive, _, path_rest = splitroot(path_abs) From 94b03aac861f67c63ea5ba2e3c4fead0e679886b Mon Sep 17 00:00:00 2001 From: nineteendo Date: Mon, 8 Apr 2024 10:08:47 +0200 Subject: [PATCH 4/8] Remove getcwd optimisation --- Lib/ntpath.py | 10 +--------- Lib/posixpath.py | 8 ++------ 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 5cca09c01467a1..10f653cb0b8ec8 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -790,16 +790,12 @@ def relpath(path, start=None): if isinstance(path, bytes): sep = b'\\' - alt_sep = b'/' curdir = b'.' pardir = b'..' - getcwd = os.getcwdb else: sep = '\\' - alt_sep = '/' curdir = '.' pardir = '..' - getcwd = os.getcwd if start is None: start = curdir @@ -807,11 +803,7 @@ def relpath(path, start=None): start = os.fspath(start) try: - if not start or start == curdir: - # Replacing / with \ is necessary for compatibily on Unix - start_abs = getcwd().replace(alt_sep, sep) - else: - start_abs = abspath(start) + start_abs = abspath(start) path_abs = abspath(path) start_drive, _, start_rest = splitroot(start_abs) path_drive, _, path_rest = splitroot(path_abs) diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 4211b06f58576d..d8e64c7345399c 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -510,12 +510,10 @@ def relpath(path, start=None): curdir = b'.' sep = b'/' pardir = b'..' - getcwd = os.getcwdb else: curdir = '.' sep = '/' pardir = '..' - getcwd = os.getcwd if start is None: start = curdir @@ -523,10 +521,8 @@ def relpath(path, start=None): start = os.fspath(start) try: - start_abs = getcwd() if not start or start == curdir else abspath(start) - path_abs = abspath(path) - start_tail = start_abs.lstrip(sep) - path_tail = path_abs.lstrip(sep) + start_tail = abspath(start).lstrip(sep) + path_tail = abspath(path).lstrip(sep) start_list = start_tail.split(sep) if start_tail else [] path_list = path_tail.split(sep) if path_tail else [] # Work out how much of the filepath is shared by start and path. From a89b8de416598795c80cbea11a1f4d6b2bfa503b Mon Sep 17 00:00:00 2001 From: Nice Zombies Date: Mon, 8 Apr 2024 16:22:29 +0200 Subject: [PATCH 5/8] Update 2024-04-07-18-42-09.gh-issue-117607.C978BD.rst --- .../2024-04-07-18-42-09.gh-issue-117607.C978BD.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst index 0302072a40ec18..6f4c38d6ceedd2 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst @@ -1 +1 @@ -Speedup :func:`os.path.relpath()` by up to 1.9 times on Windows, up to ?.? times on Unix. +Speedup :func:`os.path.relpath()` by up to 1.9 times on Windows, up to 1.1 times on Unix. From d44841d8580b34ff239a59b91f93ad965f1aaae4 Mon Sep 17 00:00:00 2001 From: Nice Zombies Date: Mon, 8 Apr 2024 16:36:03 +0200 Subject: [PATCH 6/8] Update 2024-04-07-18-42-09.gh-issue-117607.C978BD.rst --- .../2024-04-07-18-42-09.gh-issue-117607.C978BD.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst index 6f4c38d6ceedd2..f617e092a84bfa 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst @@ -1 +1 @@ -Speedup :func:`os.path.relpath()` by up to 1.9 times on Windows, up to 1.1 times on Unix. +Speedup :func:`os.path.relpath()` by up to 83% on Windows and 7% on Unix. From 491f98e7a1badeb8f2c945d1819a132e0e940de0 Mon Sep 17 00:00:00 2001 From: Nice Zombies Date: Tue, 9 Apr 2024 22:04:20 +0200 Subject: [PATCH 7/8] Update 2024-04-07-18-42-09.gh-issue-117607.C978BD.rst --- .../2024-04-07-18-42-09.gh-issue-117607.C978BD.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst index f617e092a84bfa..9c0297c2f04950 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst @@ -1 +1 @@ -Speedup :func:`os.path.relpath()` by up to 83% on Windows and 7% on Unix. +Speedup :func:`os.path.relpath()`. From 2a24ecce79bd44d3d5e689a59a5d24926d5aecee Mon Sep 17 00:00:00 2001 From: Nice Zombies Date: Thu, 11 Apr 2024 11:35:13 +0200 Subject: [PATCH 8/8] Update Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst Co-authored-by: Erlend E. Aasland --- .../2024-04-07-18-42-09.gh-issue-117607.C978BD.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst index 9c0297c2f04950..0c17dfd95ec0ec 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-07-18-42-09.gh-issue-117607.C978BD.rst @@ -1 +1 @@ -Speedup :func:`os.path.relpath()`. +Speedup :func:`os.path.relpath`.