Skip to content

Commit

Permalink
_getconftestmodules: use functools.lru_cache
Browse files Browse the repository at this point in the history
Also renames `_path2confmods` to `_dirpath2confmods` for clarity (it is
expected to be a dirpath in `_importconftest`).

Uses an explicit maxsize, since it appears to be only relevant for a
short period [1].

Removes the lru_cache on _getconftest_pathlist, which makes no
difference when caching _getconftestmodules, at least with the
performance test of 100x10 files (pytest-dev#4237).

1: pytest-dev#4237 (comment)
  • Loading branch information
blueyed committed Oct 30, 2018
1 parent 233c2a2 commit 63d4bb2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
47 changes: 22 additions & 25 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def __init__(self):
self._conftest_plugins = set()

# state related to local conftest plugins
self._path2confmods = {}
self._dirpath2confmods = {}
self._conftestpath2mod = {}
self._confcutdir = None
self._noconftest = False
Expand Down Expand Up @@ -385,31 +385,29 @@ def _try_load_conftest(self, anchor):
if x.check(dir=1):
self._getconftestmodules(x)

@lru_cache(maxsize=128)
def _getconftestmodules(self, path):
if self._noconftest:
return []

try:
return self._path2confmods[path]
except KeyError:
if path.isfile():
directory = path.dirpath()
else:
directory = path
# XXX these days we may rather want to use config.rootdir
# and allow users to opt into looking into the rootdir parent
# directories instead of requiring to specify confcutdir
clist = []
for parent in directory.realpath().parts():
if self._confcutdir and self._confcutdir.relto(parent):
continue
conftestpath = parent.join("conftest.py")
if conftestpath.isfile():
mod = self._importconftest(conftestpath)
clist.append(mod)

self._path2confmods[path] = clist
return clist
if path.isfile():
directory = path.dirpath()
else:
directory = path

# XXX these days we may rather want to use config.rootdir
# and allow users to opt into looking into the rootdir parent
# directories instead of requiring to specify confcutdir
clist = []
for parent in directory.realpath().parts():
if self._confcutdir and self._confcutdir.relto(parent):
continue
conftestpath = parent.join("conftest.py")
if conftestpath.isfile():
mod = self._importconftest(conftestpath)
clist.append(mod)
self._dirpath2confmods[directory] = clist
return clist

def _rget_with_confmod(self, name, path):
modules = self._getconftestmodules(path)
Expand Down Expand Up @@ -450,8 +448,8 @@ def _importconftest(self, conftestpath):
self._conftest_plugins.add(mod)
self._conftestpath2mod[conftestpath] = mod
dirpath = conftestpath.dirpath()
if dirpath in self._path2confmods:
for path, mods in self._path2confmods.items():
if dirpath in self._dirpath2confmods:
for path, mods in self._dirpath2confmods.items():
if path and path.relto(dirpath) or path == dirpath:
assert mod not in mods
mods.append(mod)
Expand Down Expand Up @@ -902,7 +900,6 @@ def _getini(self, name):
assert type is None
return value

@lru_cache(maxsize=None)
def _getconftest_pathlist(self, name, path):
try:
mod, relroots = self.pluginmanager._rget_with_confmod(name, path)
Expand Down
10 changes: 5 additions & 5 deletions testing/test_conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ def test_basic_init(self, basedir):

def test_immediate_initialiation_and_incremental_are_the_same(self, basedir):
conftest = PytestPluginManager()
len(conftest._path2confmods)
len(conftest._dirpath2confmods)
conftest._getconftestmodules(basedir)
snap1 = len(conftest._path2confmods)
# assert len(conftest._path2confmods) == snap1 + 1
snap1 = len(conftest._dirpath2confmods)
# assert len(conftest._dirpath2confmods) == snap1 + 1
conftest._getconftestmodules(basedir.join("adir"))
assert len(conftest._path2confmods) == snap1 + 1
assert len(conftest._dirpath2confmods) == snap1 + 1
conftest._getconftestmodules(basedir.join("b"))
assert len(conftest._path2confmods) == snap1 + 2
assert len(conftest._dirpath2confmods) == snap1 + 2

def test_value_access_not_existing(self, basedir):
conftest = ConftestWithSetinitial(basedir)
Expand Down

0 comments on commit 63d4bb2

Please sign in to comment.