Skip to content

main: avoid Path(Path(...)) calls, they're slow #9147

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

Merged
merged 1 commit into from
Oct 1, 2021

Conversation

bluetech
Copy link
Member

@bluetech bluetech commented Oct 1, 2021

See for reference #9125. From my measurements it is not that extreme, however I can confirm it shaves off ~5s from pandas collection time.


def gethookproxy(self, fspath: "os.PathLike[str]"):
# Optimization: Path(Path(...)) is much slower than isinstance.
path = fspath if isinstance(fspath, Path) else Path(fspath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps create a make_path function (or something like that)? I understand only two places do this right now, however having a function brings some advantages:

  1. We have place to put that comment about optimization.
  2. Encourages others in the future to use the function, instead of copying/pasting the same two lines.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it, but decided against it:

  1. I think in most cases it is not performance critical, so would just obfuscate the code
  2. The function call itself might have some overhead

So I just went with repeating in these two places. However, if there is ever a third we probably should.

@bluetech bluetech merged commit 8af5587 into pytest-dev:main Oct 1, 2021
@bluetech bluetech deleted the avoid-path-path branch October 1, 2021 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants