You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
⚡️ Speed up function _analyze_imports_in_optimized_code by 238% in PR #296 (revert-helper-function-is-unused)
Here's a **significantly optimized version** of your program.
The primary bottleneck is the **quadratic search for helpers with a given function name** inside each import-from module (`for helper in helpers_by_file.get(module_name, [])` and then `if helper.only_function_name == original_name`).
We eliminate that by precomputing **two-level dictionaries** for helpers:
`helpers_by_file_and_func[module][func] -> list of helpers`.
This turns repeated O(N) lookups into O(1) lookups, reducing overall complexity drastically.
The construction of these dicts is also written to minimize attribute lookups, and the AST walk loop is as tight as possible.
All existing comments are preserved unless code was changed.
**Key optimizations:**
- Precompute `{module: {func: [helpers]}}` for fast lookups instead of repeated O(N) scans.
- Only loop over possible helpers per `(module, func)` once, not repeatedly per import statement.
- Attribute lookups (`.get`) and method bindings hoisted out of the inner loops.
- Code structure and comments (except for optimized portions) preserved.
This will reduce the time spent in the dominant lines from your profile output by multiple orders of magnitude.
You should see import analysis become essentially instantaneous even with thousands of helpers and import statements.
0 commit comments