@@ -825,18 +825,26 @@ class FindModuleCache:
825825
826826 def __init__ (self , fscache : Optional [FileSystemMetaCache ] = None ) -> None :
827827 self .fscache = fscache or FileSystemMetaCache ()
828- self .find_lib_path_dirs = functools .lru_cache (maxsize = None )(self ._find_lib_path_dirs )
829- self .find_module = functools .lru_cache (maxsize = None )(self ._find_module )
828+ # Cache find_lib_path_dirs: (dir_chain, lib_path)
829+ self .dirs = {} # type: Dict[Tuple[str, Tuple[str, ...]], List[str]]
830+ # Cache find_module: (id, lib_path, python_version) -> result.
831+ self .results = {} # type: Dict[Tuple[str, Tuple[str, ...], Optional[str]], Optional[str]]
830832
831833 def clear (self ) -> None :
832- self .find_module . cache_clear ()
833- self .find_lib_path_dirs . cache_clear ()
834+ self .results . clear ()
835+ self .dirs . clear ()
834836
835- def _find_lib_path_dirs (self , dir_chain : str , lib_path : Tuple [str , ...]) -> List [str ]:
837+ def find_lib_path_dirs (self , dir_chain : str , lib_path : Tuple [str , ...]) -> List [str ]:
836838 # Cache some repeated work within distinct find_module calls: finding which
837839 # elements of lib_path have even the subdirectory they'd need for the module
838840 # to exist. This is shared among different module ids when they differ only
839841 # in the last component.
842+ key = (dir_chain , lib_path )
843+ if key not in self .dirs :
844+ self .dirs [key ] = self ._find_lib_path_dirs (dir_chain , lib_path )
845+ return self .dirs [key ]
846+
847+ def _find_lib_path_dirs (self , dir_chain : str , lib_path : Tuple [str , ...]) -> List [str ]:
840848 dirs = []
841849 for pathitem in lib_path :
842850 # e.g., '/usr/lib/python3.4/foo/bar'
@@ -845,9 +853,16 @@ def _find_lib_path_dirs(self, dir_chain: str, lib_path: Tuple[str, ...]) -> List
845853 dirs .append (dir )
846854 return dirs
847855
856+ def find_module (self , id : str , lib_path : Tuple [str , ...],
857+ python_executable : Optional [str ]) -> Optional [str ]:
858+ """Return the path of the module source file, or None if not found."""
859+ key = (id , lib_path , python_executable )
860+ if key not in self .results :
861+ self .results [key ] = self ._find_module (id , lib_path , python_executable )
862+ return self .results [key ]
863+
848864 def _find_module (self , id : str , lib_path : Tuple [str , ...],
849865 python_executable : Optional [str ]) -> Optional [str ]:
850- """Return the path of the module source file, or None if not found."""
851866 fscache = self .fscache
852867
853868 # If we're looking for a module like 'foo.bar.baz', it's likely that most of the
@@ -2167,14 +2182,12 @@ def dispatch(sources: List[BuildSource], manager: BuildManager) -> Graph:
21672182 graph = load_graph (sources , manager )
21682183
21692184 t1 = time .time ()
2170- fm_cache_size = manager .find_module_cache .find_module .cache_info ().currsize
2171- fm_dir_cache_size = manager .find_module_cache .find_lib_path_dirs .cache_info ().currsize
21722185 manager .add_stats (graph_size = len (graph ),
21732186 stubs_found = sum (g .path is not None and g .path .endswith ('.pyi' )
21742187 for g in graph .values ()),
21752188 graph_load_time = (t1 - t0 ),
2176- fm_cache_size = fm_cache_size ,
2177- fm_dir_cache_size = fm_dir_cache_size ,
2189+ fm_cache_size = len ( manager . find_module_cache . results ) ,
2190+ fm_dir_cache_size = len ( manager . find_module_cache . dirs ) ,
21782191 )
21792192 if not graph :
21802193 print ("Nothing to do?!" )
0 commit comments