Improve the performance of ntpath.expanduser() #117686
Copy link
Copy link
Closed
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Description
Feature or enhancement
Proposal:
In ntpath.expanduser(), _get_bothseps() is called in every loop iteration. It should be assigned instead:
if isinstance(path, bytes):
+ seps = b'\\/'
tilde = b'~'
else:
+ seps = '\\/'
tilde = '~'
if not path.startswith(tilde):
return path
i, n = 1, len(path)
-while i < n and path[i] not in _get_bothseps(path):
+while i < n and path[i] not in seps:
i += 1Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Activity
[-]Move `_get_bothseps()` outside loop condition[/-][+]Improve the performance of ntpath.expanduser()[/+]terryjreedy commentedon Apr 10, 2024
To me, this is a clearly better.
gh-117686: Improve the performance of ntpath.expanduser() (#117690)
erlend-aasland commentedon Apr 10, 2024
Thanks!
pythongh-117686: Improve the performance of ntpath.expanduser() (pyth…