⚡️ Speed up method ImportAnalyzer.visit_ImportFrom by 240% in PR #355 (filter_test_files_by_imports_bug_fix)
#356
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
⚡️ This pull request contains optimizations for PR #355
If you approve this dependent PR, these changes will be merged into the original PR branch
filter_test_files_by_imports_bug_fix.📄 240% (2.40x) speedup for
ImportAnalyzer.visit_ImportFromincodeflash/discovery/discover_unit_tests.py⏱️ Runtime :
9.87 milliseconds→2.90 milliseconds(best of189runs)📝 Explanation and details
Here is an optimized version of your program, focusing on the key bottlenecks identified in the profiler.
Major improvements:
any(...)calls, we build sets/maps to batch-check function names needing exact and prefix matching.Summary of changes:
any()generator expressions over a set in favor of direct set lookups and for-loops over a prefiltered small candidate list.This should be significantly faster, especially when the set of names is large and there are many aliases per import.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr355-2025-06-20T22.22.32and push.