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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,18 @@ def pytest_runtest_logreport(
pytest_collectreport = pytest_runtest_logreport

def isinitpath(self, path: Union[str, "os.PathLike[str]"]) -> bool:
return Path(path) in self._initialpaths
# Optimization: Path(Path(...)) is much slower than isinstance.
path_ = path if isinstance(path, Path) else Path(path)
return path_ in self._initialpaths

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.

pm = self.config.pluginmanager
# Check if we have the common case of running
# hooks with all conftest.py files.
pm = self.config.pluginmanager
my_conftestmodules = pm._getconftestmodules(
Path(fspath),
path,
self.config.getoption("importmode"),
rootpath=self.config.rootpath,
)
Expand Down