⚡️ Speed up function call_graph_summary by 151% in PR #1460 (call-graphee)#1462
Merged
KRRT7 merged 2 commits intocall-grapheefrom Feb 12, 2026
Merged
Conversation
The optimized code achieves a **151% speedup** (from 7.86 to 3.12 microseconds) primarily through three key optimizations:
## 1. Module-Level Import Hoisting
Moving `from rich.panel import Panel` from inside `call_graph_summary()` to the top-level module imports eliminates repeated import overhead on every function call. The line profiler shows this import took ~30,000 ns in the original (0.5% of total time). While seemingly small, this overhead is completely eliminated in the optimized version.
## 2. C-Level Aggregation with Built-in `sum()`
The optimization replaces Python-level accumulation loops with native `sum()` calls that execute at C speed:
**Original approach** (manual accumulation):
```python
total_callees = 0
with_context = 0
for count in callee_counts.values():
total_callees += count
if count > 0:
with_context += 1
```
This loop incurred ~828,000 ns across 2,005 iterations (234,973 + 301,448 + 292,402 ns).
**Optimized approach** (C-level sum):
```python
total_callees = sum(callee_counts.values())
with_context = sum(1 for count in callee_counts.values() if count > 0)
```
The new approach completes in ~405,000 ns total (16,399 + 389,145 ns) - nearly **2x faster** for the aggregation logic alone.
## 3. Leveraging `map()` for Initial Summation
Using `sum(map(len, file_to_funcs.values()))` instead of a generator expression provides a minor efficiency gain by pushing the iteration into C-level code, though the improvement here is marginal (34,533 ns → 24,396 ns).
## Performance Characteristics
Based on the annotated tests, these optimizations excel when:
- **Large-scale scenarios**: The `test_large_scale_many_functions_single_file` (1000 functions) and `test_large_scale_multiple_files_distribution` (1000 functions across 10 files) benefit most from reduced per-iteration overhead
- **Frequent invocations**: If `call_graph_summary()` is called multiple times in a session, the eliminated import overhead compounds savings
- **Non-empty function sets**: The optimization's impact is proportional to the number of callees being aggregated
The changes preserve all behavior - same summary text, same Panel display, same LSP handling - while delivering substantial runtime improvements through strategic use of Python's built-in functions that leverage optimized C implementations.
2 tasks
Contributor
PR Review SummaryPrek Checks✅ Passed (after auto-fix) Fixed 2 issues:
Committed and pushed as Mypy
Code Review✅ No critical issues found This is a codeflash optimization PR targeting
All changes are behavior-preserving. No bugs, security issues, or breaking API changes. Test Coverage
Last updated: 2026-02-12 |
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 #1460
If you approve this dependent PR, these changes will be merged into the original PR branch
call-graphee.📄 151% (1.51x) speedup for
call_graph_summaryincodeflash/cli_cmds/console.py⏱️ Runtime :
7.86 microseconds→3.12 microseconds(best of32runs)📝 Explanation and details
The optimized code achieves a 151% speedup (from 7.86 to 3.12 microseconds) primarily through three key optimizations:
1. Module-Level Import Hoisting
Moving
from rich.panel import Panelfrom insidecall_graph_summary()to the top-level module imports eliminates repeated import overhead on every function call. The line profiler shows this import took ~30,000 ns in the original (0.5% of total time). While seemingly small, this overhead is completely eliminated in the optimized version.2. C-Level Aggregation with Built-in
sum()The optimization replaces Python-level accumulation loops with native
sum()calls that execute at C speed:Original approach (manual accumulation):
This loop incurred ~828,000 ns across 2,005 iterations (234,973 + 301,448 + 292,402 ns).
Optimized approach (C-level sum):
The new approach completes in ~405,000 ns total (16,399 + 389,145 ns) - nearly 2x faster for the aggregation logic alone.
3. Leveraging
map()for Initial SummationUsing
sum(map(len, file_to_funcs.values()))instead of a generator expression provides a minor efficiency gain by pushing the iteration into C-level code, though the improvement here is marginal (34,533 ns → 24,396 ns).Performance Characteristics
Based on the annotated tests, these optimizations excel when:
test_large_scale_many_functions_single_file(1000 functions) andtest_large_scale_multiple_files_distribution(1000 functions across 10 files) benefit most from reduced per-iteration overheadcall_graph_summary()is called multiple times in a session, the eliminated import overhead compounds savingsThe changes preserve all behavior - same summary text, same Panel display, same LSP handling - while delivering substantial runtime improvements through strategic use of Python's built-in functions that leverage optimized C implementations.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1460-2026-02-12T06.40.48and push.