Skip to content

Special case for "" & "." in posixpath.abspath() #117639

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

Closed
nineteendo opened this issue Apr 8, 2024 · 8 comments
Closed

Special case for "" & "." in posixpath.abspath() #117639

nineteendo opened this issue Apr 8, 2024 · 8 comments
Labels
performance Performance or resource usage stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@nineteendo
Copy link
Contributor

nineteendo commented Apr 8, 2024

Feature or enhancement

Proposal:

We could add a special case for the current directory and return it without normalising. This speeds up posixpath.relpath() with one argument.

 def abspath(path):
     """Return an absolute path."""
     path = os.fspath(path)
     if isinstance(path, bytes):
-        if not path.startswith(b'/'):
-            path = join(os.getcwdb(), path)
+        sep = b'/'
+        curdir = b'.'
+        getcwd = os.getcwdb
     else:
-        if not path.startswith('/'):
-            path = join(os.getcwd(), path)
+        sep = '/'
+        curdir = '.'
+        getcwd = os.getcwd
+    if not path.startswith(sep):
+        if not path or path == curdir:
+            return getcwd()
+        path = join(getcwd(), path)
     return normpath(path)

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 8, 2024
@AlexWaygood
Copy link
Member

How often does real code call abspath on the current working directory? I'm somewhat sceptical that making the code more complicated here is worth it for such an edge case

@AlexWaygood AlexWaygood added performance Performance or resource usage stdlib Python modules in the Lib dir labels Apr 8, 2024
@nineteendo
Copy link
Contributor Author

nineteendo commented Apr 8, 2024

This speeds up posixpath.relpath() with 1 argument at no cost.

@AlexWaygood
Copy link
Member

This speeds up posixpath.relpath() with 1 argument at no cost.

I see. That would be useful information to edit into the issue description.

@nineteendo nineteendo changed the title Special case for current dir in posixpath.abspath() Special case for "" & "." in posixpath.abspath() Apr 9, 2024
@erlend-aasland
Copy link
Contributor

This speeds up posixpath.relpath() with 1 argument at no cost.

How much is that speedup? According to your PR there is no speedup for the general call with one argument.

@nineteendo
Copy link
Contributor Author

I'll do a benchmark soon. The pull request currently doesn't compare the performance of posixpath.relpath(), but it will be at most 13% faster.

@nineteendo
Copy link
Contributor Author

This special case makes posixpath.relpath() with a single argument 4-6% faster.

@AlexWaygood
Copy link
Member

This special case makes posixpath.relpath() with a single argument 4-6% faster.

Hmm. In my opinion, this particular optimisation is not worth it for a speedup of that magnitude. The refactor makes the code of this function look quite odd (the kind of thing that somebody keen on "refactoring" or "simplifying" the code of os.path might propose rewriting in a PR), and it's quite unobvious to a reader of the code that it's been deliberately written that way in order to speedup relpath().

@nineteendo
Copy link
Contributor Author

OK, I was planning for a larger speedup anyway, that would apply to all relative paths: #117587.

@nineteendo nineteendo closed this as not planned Won't fix, can't repro, duplicate, stale Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage 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