-
Notifications
You must be signed in to change notification settings - Fork 21
feat: extract imported class definitions for testgen context #1014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0312c37
feat: extract imported class definitions for testgen context
KRRT7 a4f84a8
Merge branch 'main' into feat/extract-imported-class-definitions
KRRT7 b3f3097
hooks
KRRT7 4748a75
temp
KRRT7 c1ac25f
if stack walking didn't find test info, fall back to environment vari…
KRRT7 40191e7
revert changes here
KRRT7 fdb1d61
Merge branch 'main' into feat/extract-imported-class-definitions
KRRT7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⚡️Codeflash found 70% (0.70x) speedup for
_extract_imports_for_classincodeflash/context/code_context_extractor.py⏱️ Runtime :
5.37 milliseconds→3.16 milliseconds(best of5runs)⚡️ This change will improve the performance of the following benchmarks:
🔻 This change will degrade the performance of the following benchmarks:
{benchmark_info_degraded}
📝 Explanation and details
The optimized code achieves a 70% speedup through three key optimizations that work synergistically:
Key Optimizations
1. Early Exit for Empty Base Classes
When a class has no base classes (or only built-in bases like
object), the function immediately returns without scanning the AST or splitting the source. This optimization shows dramatic gains in test cases liketest_no_base_class(91% faster) andtest_performance_with_many_unused_imports(3432% faster).2. Early Termination via Tracking Remaining Names
The optimized version maintains a
remaining_namesset that shrinks as imports are found:This allows the loop to exit as soon as all needed imports are found, rather than scanning the entire AST. For large modules with many imports at the top, this cuts iteration count roughly in half (29,681 → 14,715 hits on the main loop).
3. Deferred Source Splitting
The original code splits
module_sourceinto lines immediately, costing ~6% of runtime even when no imports are found. The optimized version:Performance Characteristics
Based on the annotated tests, the optimization excels when:
The optimization is slightly slower (12-35%) for small, simple cases due to the overhead of copying
needed_namesand checkingremaining_names. However, these cases are extremely fast in absolute terms (microseconds), making the slowdown negligible.Impact on Workloads
Looking at the
function_references, this function is called fromget_imported_class_definitions(), which processes every imported class name in a code context. The optimization is particularly beneficial because:The optimization transforms this from an O(imports × classes) operation to one that terminates early in most practical cases, making it well-suited for the hot path where it's used to extract class context for code analysis.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
⏪ Click to see Replay Tests
benchmarks/codeflash_replay_tests_fuodcj9h/test_tests_benchmarks_test_benchmark_code_extract_code_context__replay_test_0.py::test_codeflash_context_code_context_extractor__extract_imports_for_class_test_benchmark_extractTo test or edit this optimization locally
git merge codeflash/optimize-pr1014-2026-01-07T21.53.54Click to see suggested changes