Skip to content

Commit

Permalink
Cache _has_init calls to avoid repeated stats (#2429)
Browse files Browse the repository at this point in the history
_has_init can end up checking for the presence of the same files
over and over.

For example, when running pylint's import-error checks on a
codebase like yt-dlp, ~43,000 redundant stats were performed prior
to caching.

Closes pylint-dev/pylint#9613.
  • Loading branch information
correctmost authored May 17, 2024
1 parent 16da308 commit e43e045
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
2 changes: 2 additions & 0 deletions astroid/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from astroid.modutils import (
NoSourceFile,
_cache_normalize_path_,
_has_init,
file_info_from_modpath,
get_source_file,
is_module_name_part_of_extension_package_whitelist,
Expand Down Expand Up @@ -469,6 +470,7 @@ def clear_cache(self) -> None:
for lru_cache in (
LookupMixIn.lookup,
_cache_normalize_path_,
_has_init,
util.is_namespace,
ObjectModel.attributes,
ClassDef._metaclass_lookup_attribute,
Expand Down
1 change: 1 addition & 0 deletions astroid/modutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ def _is_python_file(filename: str) -> bool:
return filename.endswith((".py", ".pyi", ".so", ".pyd", ".pyw"))


@lru_cache(maxsize=1024)
def _has_init(directory: str) -> str | None:
"""If the given directory has a valid __init__ file, return its path,
else return None.
Expand Down

0 comments on commit e43e045

Please sign in to comment.