Skip to content

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

Description

@nineteendo

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

Activity

  1. AlexWaygood commented on Apr 8, 2024

    @AlexWaygood
    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

  2. added
    performancePerformance or resource usage
    stdlibStandard Library Python modules in the Lib/ directory
    on Apr 8, 2024
  3. nineteendo commented on Apr 8, 2024

    @nineteendo
    ContributorAuthor

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

  4. AlexWaygood commented on Apr 8, 2024

    @AlexWaygood
    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.

  5. changed the title [-]Special case for current dir in `posixpath.abspath()`[/-] [+]Special case for "" & "." in `posixpath.abspath()`[/+] on Apr 9, 2024
  6. erlend-aasland commented on Apr 9, 2024

    @erlend-aasland
    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.

  7. nineteendo commented on Apr 9, 2024

    @nineteendo
    ContributorAuthor

    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.

  8. nineteendo commented on Apr 9, 2024

    @nineteendo
    ContributorAuthor

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

  9. AlexWaygood commented on Apr 9, 2024

    @AlexWaygood
    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().

  10. nineteendo commented on Apr 9, 2024

    @nineteendo
    ContributorAuthor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions