8000 gh-117349: Micro-optimize a few `os.path` functions by nineteendo · Pull Request #117350 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-117349: Micro-optimize a few os.path functions #117350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 50 commits into from
Apr 2, 2024
Merged
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
c90a883
Speed up `posixpath.ismount`
nineteendo Mar 28, 2024
5984959
Speed up `posixpath.expanduser`
nineteendo Mar 28, 2024
833ddc9
Speed up `posixpath.normpath`
nineteendo Mar 28, 2024
a4d9fcb
Refactor `posixpath.expandvars` & `ntpath.commonpath`
nineteendo Mar 28, 2024
b853d4d
Remove start- & endswith
nineteendo Mar 29, 2024
a8984dc
Remove `startswith`
nineteendo Mar 29, 2024
78929b0
Remove `isabs` calls
nineteendo Mar 29, 2024
afc7bbc
Rename result of `splitroot`
nineteendo Mar 29, 2024
f5bbaf6
Refactor `os.path`
nineteendo Mar 29, 2024
e740f10
fix unbound variable
nineteendo Mar 29, 2024
d02c726
hardcode constants like documented
nineteendo Mar 29, 2024
b794897
📜🤖 Added by blurb_it.
blurb-it[bot] Mar 29, 2024
c2c04bf
Fix typo
nineteendo Mar 29, 2024
7c9dcae
exclude stylistic-only changes
nineteendo Mar 29, 2024
d35783c
Revert renaming
nineteendo Mar 29, 2024
eb24723
Revert unnesting
nineteendo Mar 29, 2024
5ca9ea3
Revert further changes
nineteendo Mar 29, 2024
3e4d0e3
Revert renaming
nineteendo Mar 29, 2024
72babad
Remove newline
nineteendo Mar 29, 2024
abfe46c
Remove slice
nineteendo Mar 30, 2024
2a055ba
Speedup `posixpath.ismount`
nineteendo Mar 30, 2024
759b189
Update Lib/posixpath.py
nineteendo Mar 30, 2024
cca16ba
Revert unnesting
nineteendo Mar 30, 2024
7e21192
Merge branch 'speedup-os.path' of https://github.com/nineteendo/cpyth…
nineteendo Mar 30, 2024
581862e
Remove line breaks
nineteendo Mar 30, 2024
d2987fd
Revert `ntpath.expandvars`
nineteendo Mar 30, 2024
1866544
Unexpose `posixpath._normpath_fallback`
nineteendo Mar 30, 2024
4a01123
Update Lib/posixpath.py
nineteendo Mar 30, 2024
67f0620
Indent for git blame
nineteendo Mar 30, 2024
9905730
Merge branch 'speedup-os.path' of https://github.com/nineteendo/cpyth…
nineteendo Mar 30, 2024
9d18f8c
Move back in except
nineteendo Mar 30, 2024
9520cc6
Revert changing comment
nineteendo Mar 30, 2024
de4b09e
Split try except blocks
nineteendo Mar 30, 2024
6f8e7d7
Update Misc/NEWS.d/next/Core and Builtins/2024-03-29-15-04-13.gh-issu…
nineteendo Mar 30, 2024
47a4b57
Update ACKS
nineteendo Mar 30, 2024
48f2971
Remove `_get_sep()` call
nineteendo Mar 30, 2024
33259d9
Revert unnecessary change
nineteendo Mar 30, 2024
4568cd1
Revert insignificant changes
nineteendo Mar 30, 2024
1382f49
Fix incorrect change
nineteendo Mar 30, 2024
a81dc6e
Revert unnesting
nineteendo Mar 30, 2024
e6cb4a3
Move to new pull request
nineteendo Mar 30, 2024
67ec9a6
assign `devnull` first
nineteendo Mar 30, 2024
87d43b8
speedup `posixpath.realpath`
nineteendo Mar 31, 2024
6c8901a
Speedup `ntpath.isreserved()`
nineteendo Apr 1, 2024
7940a64
Revert starts- & endswith
nineteendo Apr 1, 2024
e637698
Revert `len()` call
nineteendo Apr 1, 2024
b5fdd27
;(
nineteendo Apr 1, 2024
c923e49
Merge branch 'main' into speedup-os.path
nineteendo Apr 2, 2024
887f3c1
Replace definition of colon
nineteendo Apr 2, 2024
e3ace2f
Merge branch 'speedup-os.path' of https://github.com/nineteendo/cpyth…
nineteendo Apr 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename result of splitroot
  • Loading branch information
nineteendo committed Mar 29, 2024
commit afc7bbcac0a5355fa12c0495d21b57efd6ff3701
66 changes: 34 additions & 32 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,34 +110,36 @@ def join(path, *paths):
try:
if not paths:
path[:0] + sep #23780: Ensure compatible data type even if p is null.
result_drive, result_root, result_path = splitroot(path)
result_drive, result_root, result_tail = splitroot(path)
for p in map(os.fspath, paths):
p_drive, p_root, p_path = splitroot(p)
p_drive, p_root, p_tail = splitroot(p)
if p_root:
# Second path is absolute
if p_drive or not result_drive:
result_drive = p_drive
result_root = p_root
result_path = p_path
result_tail = p_tail
continue
elif p_drive and p_drive != result_drive:
if p_drive.lower() != result_drive.lower():
# Different drives => ignore the first path entirely
result_drive = p_drive
result_root = p_root
result_path = p_path
result_tail = p_tail
continue
# Same drive in different case
result_drive = p_drive
# Second path is relative to the first
if result_path and result_path[-1] not in seps:
result_path = result_path + sep
result_path = result_path + p_path
if result_tail and result_tail[-1] not in seps:
result_tail = result_tail + sep
result_tail = result_tail + p_tail
## add separator between UNC and non-absolute path
if (result_path and not result_root and
result_drive and result_drive[-1:] not in colon + seps):
return result_drive + sep + result_path
return result_drive + result_root + result_path
if (
result_tail and not result_root
and result_drive and result_drive[-1] not in colon + seps
):
return result_drive + sep + result_tail
return result_drive + result_root + result_tail
except (TypeError, AttributeError, BytesWarning):
genericpath._check_arg_types('join', path, *paths)
raise
Expand Down Expand Up @@ -233,13 +235,13 @@ def split(p):
Either part may be empty."""
p = os.fspath(p)
seps = _get_bothseps(p)
d, r, p = splitroot(p)
drive, root, tail = splitroot(p)
# set i to index beyond p's last slash
i = len(p)
while i and p[i-1] not in seps:
i = len(tail)
while i and tail[i-1] not in seps:
i -= 1
head, tail = p[:i], p[i:] # now tail has no slashes
return d + r + head.rstrip(seps), tail
head, tail = tail[:i], tail[i:] # now tail has no slashes
return drive + root + head.rstrip(seps), tail


# Split a path in root and extension.
Expand Down Expand Up @@ -305,10 +307,10 @@ def ismount(path):
path = os.fspath(path)
seps = _get_bothseps(path)
path = abspath(path)
drive, root, rest = splitroot(path)
drive, root, tail = splitroot(path)
if drive and drive[0] in seps:
return not rest
if root and not rest:
return not tail
if root and not tail:
return True

if _getvolumepathname:
Expand Down Expand Up @@ -555,9 +557,9 @@ def normpath(path):
curdir = '.'
pardir = '..'
path = path.replace(altsep, sep)
drive, root, path = splitroot(path)
drive, root, tail = splitroot(path)
prefix = drive + root
comps = path.split(sep)
comps = tail.split(sep)
i = 0
while i < len(comps):
if not comps[i] or comps[i] == curdir:
Expand Down Expand Up @@ -763,14 +765,14 @@ def realpath(path, *, strict=False):
# For UNC paths, the prefix will actually be \\?\UNC\
# Handle that case as well.
if path[:8] == unc_prefix:
spath = new_unc_prefix + path[len(unc_prefix):]
spath = new_unc_prefix + path[8:]
else:
spath = path[len(prefix):]
spath = path[4:]
# Ensure that the non-prefixed path resolves to the same path
try:
if _getfinalpathname(spath) == path:
path = spath
except ValueError as ex:
except ValueError:
# Unexpected, as an invalid path should not have gained a prefix
# at any point, but we ignore this error just in case.
pass
Expand Down Expand Up @@ -807,14 +809,14 @@ def relpath(path, start=None):
try:
start_abs = abspath(normpath(start))
path_abs = abspath(normpath(path))
start_drive, _, start_rest = splitroot(start_abs)
path_drive, _, path_rest = splitroot(path_abs)
start_drive, _, start_tail = splitroot(start_abs)
path_drive, _, path_tail = 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 = [x for x in start_tail.split(sep) if x]
path_list = [x for x in path_tail.split(sep) if x]
# Work out how much of the filepath is shared by start and path.
i = 0
for e1, e2 in zip(start_list, path_list):
Expand Down Expand Up @@ -863,18 +865,18 @@ def commonpath(paths):
# Check that all drive letters or UNC paths match. The check is made
# only now otherwise type errors for mixing strings and bytes would not
# be caught.
if len({d for d, _, _ in rootsplits}) != 1:
if len({drive for drive, _, _ in rootsplits}) != 1:
raise ValueError("Paths don't have the same drive")

if len({r for _, r, _ in rootsplits}) != 1:
if len({root for _, root, _ in rootsplits}) != 1:
raise ValueError("Can't mix absolute and relative paths")

drive, root, tail = splitroot(paths[0].replace(altsep, sep))
common = [c for c in tail.split(sep) if c and c != curdir]

split_paths = [
[c for c in t.split(sep) if c and c != curdir]
for _, _, t in rootsplits
[c for c in tail.split(sep) if c and c != curdir]
for _, _, tail in rootsplits
]
s1 = min(split_paths)
s2 = max(split_paths)
Expand Down
0