Skip to content
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

Improve the performance of ntpath.expanduser() #117686

Closed
nineteendo opened this issue Apr 9, 2024 · 2 comments
Closed

Improve the performance of ntpath.expanduser() #117686

nineteendo opened this issue Apr 9, 2024 · 2 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@nineteendo
Copy link
Contributor

nineteendo commented Apr 9, 2024

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 += 1

Has 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

@nineteendo nineteendo added the type-feature A feature request or enhancement label Apr 9, 2024
@nineteendo nineteendo changed the title Move _get_bothseps() outside loop condition Improve the performance of ntpath.expanduser() Apr 9, 2024
@terryjreedy
Copy link
Member

To me, this is a clearly better.

@erlend-aasland erlend-aasland added the stdlib Python modules in the Lib dir label Apr 10, 2024
erlend-aasland pushed a commit that referenced this issue Apr 10, 2024
Refactor out _get_bothseps() call from the loop.
@erlend-aasland
Copy link
Contributor

Thanks!

diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants