diff options
author | Jacob Walls <jacobtylerwalls@gmail.com> | 2023-04-23 10:37:15 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-23 10:37:15 -0400 |
commit | e49bfaa2229e2ce27b7a212ad3ebe378dc17f613 (patch) | |
tree | 64204934c89ecde7607debf999ccaa7b90603002 /astroid/manager.py | |
parent | f4a1168c0fda96b6bb7d01acb83b39405e7fe07a (diff) | |
download | astroid-git-e49bfaa2229e2ce27b7a212ad3ebe378dc17f613.tar.gz |
Reduce file system access in `ast_from_file()` (#2135)
get_source_file() is needed to resolve relative to absolute paths,
but is not needed before getting a cache hit. This had the potential
to issue tens of thousands of repetitive os.path.exists() calls.
Diffstat (limited to 'astroid/manager.py')
-rw-r--r-- | astroid/manager.py | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/astroid/manager.py b/astroid/manager.py index 1f6ef482..7f62fd42 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -101,11 +101,6 @@ class AstroidManager: source: bool = False, ) -> nodes.Module: """Given a module name, return the astroid object.""" - try: - filepath = get_source_file(filepath, include_no_ext=True) - source = True - except NoSourceFile: - pass if modname is None: try: modname = ".".join(modpath_from_file(filepath)) @@ -116,6 +111,19 @@ class AstroidManager: and self.astroid_cache[modname].file == filepath ): return self.astroid_cache[modname] + # Call get_source_file() only after a cache miss, + # since it calls os.path.exists(). + try: + filepath = get_source_file(filepath, include_no_ext=True) + source = True + except NoSourceFile: + pass + # Second attempt on the cache after get_source_file(). + if ( + modname in self.astroid_cache + and self.astroid_cache[modname].file == filepath + ): + return self.astroid_cache[modname] if source: # pylint: disable=import-outside-toplevel; circular import from astroid.builder import AstroidBuilder |