⚡️ Speed up function is_claude_desktop_installed by 7%
#145
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.
📄 7% (0.07x) speedup for
is_claude_desktop_installedinskyvern/cli/mcp.py⏱️ Runtime :
436 microseconds→408 microseconds(best of34runs)📝 Explanation and details
The optimization achieves a 6% speedup through three key changes that reduce CPU overhead:
What was optimized:
Eliminated dictionary creation and lookups - The original code created a
base_pathsdictionary on every function call (even for WSL), then performed dictionary lookups. The optimized version inlines the paths directly in each branch, avoiding the dict allocation and lookup overhead.Replaced
os.path.dirname()with string splitting - Inis_claude_desktop_installed(), changed fromos.path.dirname(get_claude_config_path())toconfig_path.rsplit(os.sep, 1)[0]. String operations are faster than filesystem path parsing functions.Preserved short-circuiting behavior - The Linux path checking logic remains unchanged, still stopping at the first existing directory.
Why it's faster:
rsplit()is a simple string operation vsos.path.dirname()which involves more complex path parsing logicImpact on workloads:
Looking at the function reference,
get_claude_config_path()is called fromsetup_claude_desktop_config()during CLI setup operations. While this isn't a hot loop, the optimization provides consistent improvement across all test cases (6-38% faster depending on the platform), with particularly good gains for error cases and unsupported systems where the function exits early.Test case performance:
The optimization shows the best results for quick-exit scenarios (unsupported systems: 26-38% faster) and solid improvements for normal cases (6-21% faster), making it beneficial across all usage patterns without changing any behavior.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-is_claude_desktop_installed-mjactf30and push.