Skip to content

Optimize pathlib.PurePath.relative_to walk up case #143630

@heikkitoivonen

Description

@heikkitoivonen

Feature or enhancement

Proposal:

pathlib.PurePath.relative_to method has a loop, which builds parents through every iteration of the loop. This makes it slow if we end up looping more than once. We could improve performance by caching the parents when looping multiple times.

The difficult part is how to make the changes so that they do not slow down two special cases:

  • path == self
  • we loop exactly once

So far all my attempts to improve this either slow down one or both of the above cases, which is why I have not yet done a PR. But if we could figure out ways to not slow down the above two, then the savings would be significant.

The best I have been able to come up with (I can make a PR if this looks good enough):

No changes if path == self
break on 1st loop, walk_up=False: 1.01x slower
1 level up: 1.09x slower
2 levels up: 1.25x faster
3 levels up: 1.46x faster

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:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions