⚡️ Speed up function extract_imports_for_class by 396% in PR #1166 (skyvern-grace)#1168
Closed
codeflash-ai[bot] wants to merge 1 commit into
Closed
Conversation
The optimized code achieves a **396% speedup** (3.23ms → 650μs) by replacing an expensive AST traversal with a direct iteration over class body nodes. ## Key Optimization **Replaced `ast.walk(class_node)` with direct `class_node.body` iteration** (line 33): - **Original**: Used `ast.walk(class_node)` which recursively traverses ALL nodes in the class AST (3,785 hits), including method bodies, nested statements, and deeply nested expressions. This accounted for **70.5% of total runtime**. - **Optimized**: Directly iterates over `class_node.body`, which contains only the top-level class members (553 hits) - a **7x reduction** in nodes visited. ## Why This Works The function only needs to inspect **field definitions** at the class level to collect type annotation names. Method bodies and nested structures are irrelevant for extracting imports. By iterating only `class_node.body`: - We examine just the annotated field assignments (`ast.AnnAssign`) and field calls needed for import extraction - We skip irrelevant AST nodes like method definitions, nested statements, and expression details inside methods - The reduction from 3,785 to 553 node checks directly translates to the observed speedup ## Performance Characteristics Based on the test results, the optimization excels across all scenarios: - **Simple classes**: 176-362% speedup (basic imports/decorators) - **Complex nested annotations**: 280-586% speedup (Dict[List[Optional[...]]]) - **Large-scale scenarios**: Up to **1991% speedup** for classes with 100+ methods and fields (where the original's deep traversal penalty was most severe) ## Impact Assessment The function is called from `get_imported_class_definitions()` which extracts class definitions for LLM context during code optimization. This is in a **hot path** that processes every imported class in the codebase being analyzed. With the 4-5x speedup, code context extraction becomes significantly faster, improving the overall optimization pipeline's responsiveness, especially for large codebases with many dataclass-style classes. The optimization preserves exact functionality - it still collects all needed import names from base classes, decorators, and type annotations, just by examining the relevant nodes directly rather than walking the entire AST tree.
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 #1166
If you approve this dependent PR, these changes will be merged into the original PR branch
skyvern-grace.📄 396% (3.96x) speedup for
extract_imports_for_classincodeflash/context/code_context_extractor.py⏱️ Runtime :
3.23 milliseconds→650 microseconds(best of5runs)📝 Explanation and details
The optimized code achieves a 396% speedup (3.23ms → 650μs) by replacing an expensive AST traversal with a direct iteration over class body nodes.
Key Optimization
Replaced
ast.walk(class_node)with directclass_node.bodyiteration (line 33):ast.walk(class_node)which recursively traverses ALL nodes in the class AST (3,785 hits), including method bodies, nested statements, and deeply nested expressions. This accounted for 70.5% of total runtime.class_node.body, which contains only the top-level class members (553 hits) - a 7x reduction in nodes visited.Why This Works
The function only needs to inspect field definitions at the class level to collect type annotation names. Method bodies and nested structures are irrelevant for extracting imports. By iterating only
class_node.body:ast.AnnAssign) and field calls needed for import extractionPerformance Characteristics
Based on the test results, the optimization excels across all scenarios:
Impact Assessment
The function is called from
get_imported_class_definitions()which extracts class definitions for LLM context during code optimization. This is in a hot path that processes every imported class in the codebase being analyzed. With the 4-5x speedup, code context extraction becomes significantly faster, improving the overall optimization pipeline's responsiveness, especially for large codebases with many dataclass-style classes.The optimization preserves exact functionality - it still collects all needed import names from base classes, decorators, and type annotations, just by examining the relevant nodes directly rather than walking the entire AST tree.
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_code_context_extractor.py::TestExtractImportsForClass.test_extracts_base_class_importstest_code_context_extractor.py::TestExtractImportsForClass.test_extracts_decorator_importstest_code_context_extractor.py::TestExtractImportsForClass.test_extracts_field_function_importstest_code_context_extractor.py::TestExtractImportsForClass.test_extracts_type_annotation_importstest_code_context_extractor.py::TestExtractImportsForClass.test_no_duplicate_imports🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1166-2026-01-24T09.06.26and push.