⚡️ Speed up function code_print by 71% in PR #1618 (agent-mode-flag)#1620
Closed
codeflash-ai[bot] wants to merge 2 commits intoagent-mode-flagfrom
Closed
⚡️ Speed up function code_print by 71% in PR #1618 (agent-mode-flag)#1620codeflash-ai[bot] wants to merge 2 commits intoagent-mode-flagfrom
code_print by 71% in PR #1618 (agent-mode-flag)#1620codeflash-ai[bot] wants to merge 2 commits intoagent-mode-flagfrom
Conversation
This optimization achieves a **71% runtime improvement** (from 796ms to 465ms) by eliminating repeated environment variable lookups and function call overhead. **What Changed:** The optimization replaces `@lru_cache` decorated functions with module-level constants that capture environment variables once at import time: - `is_LSP_enabled()` and `is_agent_mode()` now return pre-computed boolean constants (`_LSP_ENABLED`, `_AGENT_MODE`) instead of invoking `lru_cache` lookup machinery on every call. **Why This Is Faster:** 1. **Eliminates LRU cache overhead**: Even with cached results, `lru_cache` incurs function call overhead, cache key computation, and dictionary lookups on every invocation. The line profiler shows these checks executing hundreds of times (391-392 hits), making this overhead significant. 2. **Reduces to simple variable access**: Returning a module-level constant is effectively a single memory read versus function call + cache lookup + return. Python's global variable access is extremely fast compared to function invocation. 3. **Environment variables are static**: Since `CODEFLASH_LSP` and `CODEFLASH_AGENT_MODE` don't change during execution, evaluating them once at import time is semantically equivalent but computationally cheaper. **Test Results:** The annotated tests show consistent small improvements (1-8%) across individual calls, which compounds dramatically when these functions are called repeatedly. The `test_large_scale_many_calls_performance_and_stability` test with 500 iterations particularly benefits from this optimization, as the cumulative overhead savings multiply. Tests like `test_code_print_deeply_nested_code` show 8.33% improvement, and `test_code_print_many_function_calls` (100 iterations) shows 2.14% improvement. **Impact:** Since these helper functions guard important code paths (LSP mode, agent mode checks), they're invoked frequently throughout the codebase. The optimization is especially beneficial in scenarios with many sequential operations, as evidenced by the 71% overall speedup in the measured workload.
Contributor
PR Review SummaryPrek Checks
Code ReviewNo critical issues found. The optimization is safe:
Test Coverage
Missing lines (29-36, 40-52, 56-63, 68-70) are in unrelated functions ( Test suite: 2411 passed, 8 failed (all in Last updated: 2026-02-20 |
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 #1618
If you approve this dependent PR, these changes will be merged into the original PR branch
agent-mode-flag.📄 71% (0.71x) speedup for
code_printincodeflash/cli_cmds/console.py⏱️ Runtime :
796 milliseconds→465 milliseconds(best of8runs)📝 Explanation and details
This optimization achieves a 71% runtime improvement (from 796ms to 465ms) by eliminating repeated environment variable lookups and function call overhead.
What Changed:
The optimization replaces
@lru_cachedecorated functions with module-level constants that capture environment variables once at import time:is_LSP_enabled()andis_agent_mode()now return pre-computed boolean constants (_LSP_ENABLED,_AGENT_MODE) instead of invokinglru_cachelookup machinery on every call.Why This Is Faster:
Eliminates LRU cache overhead: Even with cached results,
lru_cacheincurs function call overhead, cache key computation, and dictionary lookups on every invocation. The line profiler shows these checks executing hundreds of times (391-392 hits), making this overhead significant.Reduces to simple variable access: Returning a module-level constant is effectively a single memory read versus function call + cache lookup + return. Python's global variable access is extremely fast compared to function invocation.
Environment variables are static: Since
CODEFLASH_LSPandCODEFLASH_AGENT_MODEdon't change during execution, evaluating them once at import time is semantically equivalent but computationally cheaper.Test Results:
The annotated tests show consistent small improvements (1-8%) across individual calls, which compounds dramatically when these functions are called repeatedly. The
test_large_scale_many_calls_performance_and_stabilitytest with 500 iterations particularly benefits from this optimization, as the cumulative overhead savings multiply. Tests liketest_code_print_deeply_nested_codeshow 8.33% improvement, andtest_code_print_many_function_calls(100 iterations) shows 2.14% improvement.Impact:
Since these helper functions guard important code paths (LSP mode, agent mode checks), they're invoked frequently throughout the codebase. The optimization is especially beneficial in scenarios with many sequential operations, as evidenced by the 71% overall speedup in the measured workload.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1618-2026-02-20T20.33.15and push.