⚡️ Speed up function detect_frameworks_from_code by 55% in PR #1015 (gpu-sync-instrumentation)
#1017
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 #1015
If you approve this dependent PR, these changes will be merged into the original PR branch
gpu-sync-instrumentation.📄 55% (0.55x) speedup for
detect_frameworks_from_codeincodeflash/code_utils/instrument_existing_tests.py⏱️ Runtime :
16.8 milliseconds→10.8 milliseconds(best of5runs)📝 Explanation and details
The optimized code achieves a 54% speedup by replacing
ast.walk()with a more efficient manual tree traversal using adeque. Here's why this is faster:Key Optimizations
Selective Node Traversal: Instead of visiting every node in the AST (which
ast.walk()does), the optimized version only descends into node attributes that can contain statements (body,orelse,finalbody,handlers). This skips irrelevant subtrees like expression nodes, literals, and operators that can never contain import statements.Early Type Checking: By checking
isinstance(node, ast.Import)andisinstance(node, ast.ImportFrom)upfront and handling only these node types, the code avoids the overhead of traversing and type-checking thousands of irrelevant nodes thatast.walk()would visit.Framework Key Lookup Optimization: Using
if module_name in framework_keys(tuple membership test) instead of three separate string equality checks reduces comparison overhead, especially when most modules are not frameworks.Performance Impact by Test Type
The line profiler confirms the win:
ast.walk()consumed 68% of runtime (56ms) in the original, while the optimized manual traversal spends only 3.1% (0.9ms) on the queue loop itself.Context from Function References
The function is called from
inject_profiling_into_existing_test(), which processes test files during optimization workflows. Since this runs on every test file being analyzed, the 54% speedup directly reduces overall test instrumentation time, making the optimization valuable for the profiling pipeline.✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_instrument_tests.py::TestDetectFrameworksFromCode.test_alias_import_takes_precedence_over_from_importtest_instrument_tests.py::TestDetectFrameworksFromCode.test_detect_from_import_no_aliastest_instrument_tests.py::TestDetectFrameworksFromCode.test_detect_jax_standard_importtest_instrument_tests.py::TestDetectFrameworksFromCode.test_detect_jax_with_aliastest_instrument_tests.py::TestDetectFrameworksFromCode.test_detect_multiple_frameworkstest_instrument_tests.py::TestDetectFrameworksFromCode.test_detect_no_frameworkstest_instrument_tests.py::TestDetectFrameworksFromCode.test_detect_syntax_error_returns_emptytest_instrument_tests.py::TestDetectFrameworksFromCode.test_detect_tensorflow_standard_importtest_instrument_tests.py::TestDetectFrameworksFromCode.test_detect_tensorflow_with_aliastest_instrument_tests.py::TestDetectFrameworksFromCode.test_detect_torch_standard_importtest_instrument_tests.py::TestDetectFrameworksFromCode.test_detect_torch_with_alias🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1015-2026-01-07T22.54.48and push.