refactor: move context extraction to languages/python/context/#1498
Conversation
Consolidate three enricher functions (get_imported_class_definitions, get_external_base_class_inits, get_external_class_inits) into a single enrich_testgen_context that parses code context once. Extract shared helpers, unify prune_cst variants, deduplicate loop bodies, and remove dead UsedNameCollector class.
Move code_context_extractor.py and unused_definition_remover.py from codeflash/context/ to codeflash/languages/python/context/ and update all import sites.
Replace duplicate implementations in extract_code_context() and find_helper_functions() with calls to get_code_optimization_context() and get_function_sources_from_jedi() from the canonical context module.
PR Review SummaryPrek Checks✅ Passed — One formatting issue in Mypy: 191 errors across 9 files, but these are pre-existing issues (missing generic type parameters, complex union-attr patterns in Code ReviewThis is a re-review after new commits ( Remaining questions (not blockers):
No critical bugs, security issues, or breaking API changes found. All callers of the refactored Test CoverageOverall: 78.6% (PR) vs 78.7% (main) — no significant regression
Last updated: 2026-02-16T22:20Z |
Update stale context/ paths in mypy_allowlist.txt to match the languages/python/context/ move. Add assert to narrow BaseSuite to IndentedBlock in prune_cst for mypy.
Re-add graceful degradation when context exceeds token limits instead of raising ValueError immediately. Read-only context falls back to removing docstrings then removing entirely. Testgen context falls back to removing docstrings then removing enrichment before raising.
The optimization achieves a **68% runtime improvement** (23.5ms → 14.0ms) by replacing the expensive `ast.walk()` traversal with a targeted recursive collection strategy. **Key Performance Improvement:** The original code uses `ast.walk(tree)` which visits **every single node** in the AST tree (12,947 hits shown in line profiler), consuming 71.7% of total runtime. This includes unnecessary nodes like expressions, literals, and operators that can never contain `ImportFrom` statements. The optimized version implements a custom `collect_imports()` function that: 1. **Only traverses module body and control flow structures** where imports can legally appear (function/class definitions, if/while/for blocks, try/except) 2. **Skips irrelevant AST nodes** like expressions, literals, and operators entirely 3. **Recursively processes nested bodies** (body, orelse, finalbody, handlers) in a depth-first manner **Why This Works:** In Python, `from X import Y` statements can only appear: - At module level - Inside function/class definitions - Within control flow blocks (if/while/for/try) By checking `isinstance()` for only these container node types and recursively descending into their body attributes, we avoid traversing the entire AST subtree for each construct. This dramatically reduces the number of nodes visited while maintaining correctness. **Test Case Performance:** The optimization excels across all scales: - **Small imports** (single statements): 60-77% faster - **Large import lists** (100-500 items): 74-104% faster - **Many code blocks** (500-1000 lines): 70-77% faster - **Mixed code/imports** at scale: 70% faster The performance gain is particularly pronounced when the AST contains large amounts of non-import code (functions, classes, expressions), as shown by the `test_mixed_imports_and_code_large_scale` case improving from 9.31ms to 5.45ms (70.8% faster). **Impact on Workloads:** Given the function_references show this is used in code context extraction benchmarks, this optimization will significantly speed up any workflow that analyzes Python imports from large codebases or performs repeated import analysis during development workflows.
⚡️ Codeflash found optimizations for this PR📄 69% (0.69x) speedup for
|
The optimized code achieves a **350% speedup** (2.36ms → 523μs) by replacing the generic `ast.walk()` traversal with a targeted stack-based iteration that only visits nodes where class definitions can appear. **Key Performance Improvement:** The original implementation uses `ast.walk(tree)`, which performs an exhaustive depth-first traversal of **every single node** in the AST—including expressions, literals, operators, and other leaf nodes that can never contain class definitions. For a typical Python module, this means checking thousands of irrelevant nodes. The optimized version uses a stack-based approach that only descends into structural nodes (ClassDef, FunctionDef, If, For, While, With, Try blocks) where classes can actually be defined. This dramatically reduces the number of nodes visited and `isinstance()` checks performed. **Why This Matters:** From the test results, we see consistent 200-700% speedups across all scenarios: - Empty modules: 579% faster (5.37μs → 791ns) - minimal traversal overhead - Simple cases: 200-400% faster - fewer nodes to check - Complex nested structures: 405% faster (37.2μs → 7.37μs) - targeted descent pays off - Large modules (500 classes): 280% faster (869μs → 228μs) - scales better - Mixed workloads: 558% faster (799μs → 121μs) - avoids non-class nodes **Impact on Workloads:** Based on the function references showing this is called from `build_testgen_context`, this optimization benefits test generation workflows that analyze Python code structure. Since class extraction is likely performed repeatedly during code analysis, the 4x speedup directly improves overall test generation throughput. The optimization is particularly effective for large codebases with many classes and complex nesting patterns, as demonstrated by the benchmark results.
⚡️ Codeflash found optimizations for this PR📄 351% (3.51x) speedup for
|
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
…2026-02-16T20.53.40 ⚡️ Speed up function `collect_existing_class_names` by 351% in PR #1498 (`cf-simplify-context-extraction`)
|
This PR is now faster! 🚀 @KRRT7 accepted my optimizations from: |
The optimized collect_imports missed match/case statements where imports can legally appear. Add hasattr-guarded handling for ast.Match nodes. Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
…2026-02-16T20.49.33 ⚡️ Speed up function `_parse_and_collect_imports` by 69% in PR #1498 (`cf-simplify-context-extraction`)
|
This PR is now faster! 🚀 @KRRT7 accepted my optimizations from: |
Extract shared helpers and remove dead code across the language support area: - Extract `is_assignment_used()` and move `recurse_sections` to unused_definition_remover.py, replacing duplicated logic in both context files - Extract `function_sources_to_helpers()` in support.py to unify identical HelperFunction construction - Remove dead `get_comment_prefix()` method from protocol and all implementations (comment_prefix property serves all callers)
Summary
code_context_extractor.pyandunused_definition_remover.pyfromcodeflash/context/tocodeflash/languages/python/context/codeflash/context/packageThese modules are Python-specific (Jedi, ast, libcst) and belong under
languages/python/. There was duplicate code inlanguages/python/attempting to replicate this functionality with incomplete / no feature parity — this move consolidates to the canonical implementation.Test plan
pytest tests/test_code_context_extractor.py tests/test_get_read_writable_code.py tests/test_get_read_only_code.py tests/test_get_testgen_code.py tests/test_remove_unused_definitions.py tests/test_unused_helper_revert.py tests/test_get_helper_code.py— 173 passedpytest tests/test_languages/— 731 passedpytest tests/test_code_replacement.py tests/test_function_dependencies.py— 66 passed