Cache the results of is_exe_in_path.

Review Request #11823 — Created Sept. 27, 2021 and submitted

Information

RBTools
master

Reviewers

is_exe_in_path() may be called multiple times with the same executable
name, particularly when running from unit tests. Since an executable is
unlikely to appear or disappear within the lifetime of a process run in
a way that impacts RBTools, it's not worth re-scanning the whole path
every time we need to know if an executable is available.

This change adds caching behavior to is_exe_in_path(). Now, a repeated
call with the same executable name (with or without a .exe file
extension) will return the prior cached result.

All unit tests pass.

Summary ID
Cache the results of is_eze_in_path.
`is_exe_in_path()` may be called multiple times with the same executable name, particularly when running from unit tests. Since an executable is unlikely to appear or disappear within the lifetime of a process run in a way that impacts RBTools, it's not worth re-scanning the whole path every time we need to know if an executable is available. This change adds caching behavior to `is_exe_in_path()`. Now, a repeated call with the same executable name (with or without a `.exe` file extension) will return the prior cached result.
f90f6317d91f94af75fe1b11001d58d03b6986b9
Description From Last Updated

Might be a little simpler as: if name not in _exe_in_path_cache: ... _exe_in_path_cache[name] = found return _exe_in_path_cache[name]

daviddavid
david
  1. 
      
  2. rbtools/utils/filesystem.py (Diff revision 1)
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    Show all issues

    Might be a little simpler as:

    if name not in _exe_in_path_cache:
        ...
        _exe_in_path_cache[name] = found
    
    return _exe_in_path_cache[name]
    
    1. This is a pretty common pattern when using a dictionary as a cache. You avoid the repeated lookups in the dictionary by assuming the key is present, and then only writing to the dictionary if not. Saves a dictionary lookup. This code isn't a bottleneck, but the pattern's a good one.

      Another option is I could switch to @lru_cache, but it's only in Python 3 (without depending on a backport for 2).

    2. OK, sure.

  3. 
      
david
  1. Ship It!
  2. 
      
david
  1. Ship It!
  2. 
      
chipx86
Review request changed
Status:
Completed
Change Summary:
Pushed to master (dc70d35)