⚡️ Speed up function extract_dependent_function by 197% in PR #1457 (fix-coverage-qualified-name) - #1458
Merged
KRRT7 merged 1 commit intoFeb 12, 2026
Conversation
The optimized code achieves a **197% speedup (28.5ms → 9.57ms)** through three strategic optimizations that dramatically reduce expensive AST parsing operations: ## Key Optimizations **1. Early String Filtering (74% time reduction in parsing)** The optimization adds a lightweight heuristic check `if "def" not in code_string.code` before calling `ast.parse()`. Since function definitions require the `def` keyword, strings without it can be skipped entirely. In the profiler results, this reduced AST parsing from 32.5ms (80.5% of original runtime) to 9.9ms (74.2% of optimized runtime). The test results show dramatic improvements for large-scale scenarios: - `test_large_scale_many_code_strings_single_dependent_function`: **6839% faster** (4.45ms → 64.1μs) - `test_large_scale_with_preexisting_objects_and_many_irrelevant_entries`: **4193% faster** (2.26ms → 52.7μs) **2. Hoisted Main Function Name Computation** Moving `bare_main` calculation outside the loop (from line 13 to line 10) eliminates redundant string operations that were executed once per code string. This simple reordering saves repeated `rsplit()` calls. **3. Early Exit on Multiple Dependencies** The optimization checks `if len(dependent_functions) > 1: return False` immediately after adding each function name, rather than waiting until all code strings are processed. This allows the function to short-circuit as soon as it detects the failure condition, avoiding unnecessary AST parsing of remaining code strings. ## Why This Matters Based on the function references, `extract_dependent_function` is called during test generation workflows where it processes potentially hundreds or thousands of code strings. The optimization is particularly effective when: - Most code strings don't contain function definitions (common in test contexts with imports, variables, etc.) - Multiple dependent functions exist (early exit prevents wasted parsing) - Code bases have many test-related code strings that aren't function definitions The optimizations preserve exact behavior while intelligently avoiding expensive operations, making the code significantly more efficient in real-world usage patterns where the function processes large volumes of code strings.
2 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 #1457
If you approve this dependent PR, these changes will be merged into the original PR branch
fix-coverage-qualified-name.📄 197% (1.97x) speedup for
extract_dependent_functionincodeflash/code_utils/coverage_utils.py⏱️ Runtime :
28.5 milliseconds→9.57 milliseconds(best of13runs)📝 Explanation and details
The optimized code achieves a 197% speedup (28.5ms → 9.57ms) through three strategic optimizations that dramatically reduce expensive AST parsing operations:
Key Optimizations
1. Early String Filtering (74% time reduction in parsing)
The optimization adds a lightweight heuristic check
if "def" not in code_string.codebefore callingast.parse(). Since function definitions require thedefkeyword, strings without it can be skipped entirely. In the profiler results, this reduced AST parsing from 32.5ms (80.5% of original runtime) to 9.9ms (74.2% of optimized runtime). The test results show dramatic improvements for large-scale scenarios:test_large_scale_many_code_strings_single_dependent_function: 6839% faster (4.45ms → 64.1μs)test_large_scale_with_preexisting_objects_and_many_irrelevant_entries: 4193% faster (2.26ms → 52.7μs)2. Hoisted Main Function Name Computation
Moving
bare_maincalculation outside the loop (from line 13 to line 10) eliminates redundant string operations that were executed once per code string. This simple reordering saves repeatedrsplit()calls.3. Early Exit on Multiple Dependencies
The optimization checks
if len(dependent_functions) > 1: return Falseimmediately after adding each function name, rather than waiting until all code strings are processed. This allows the function to short-circuit as soon as it detects the failure condition, avoiding unnecessary AST parsing of remaining code strings.Why This Matters
Based on the function references,
extract_dependent_functionis called during test generation workflows where it processes potentially hundreds or thousands of code strings. The optimization is particularly effective when:The optimizations preserve exact behavior while intelligently avoiding expensive operations, making the code significantly more efficient in real-world usage patterns where the function processes large volumes of code strings.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1457-2026-02-12T04.58.15and push.