Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Dec 17, 2025

📄 7% (0.07x) speedup for is_claude_desktop_installed in skyvern/cli/mcp.py

⏱️ Runtime : 436 microseconds 408 microseconds (best of 34 runs)

📝 Explanation and details

The optimization achieves a 6% speedup through three key changes that reduce CPU overhead:

What was optimized:

  1. Eliminated dictionary creation and lookups - The original code created a base_paths dictionary 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.

  2. Replaced os.path.dirname() with string splitting - In is_claude_desktop_installed(), changed from os.path.dirname(get_claude_config_path()) to config_path.rsplit(os.sep, 1)[0]. String operations are faster than filesystem path parsing functions.

  3. Preserved short-circuiting behavior - The Linux path checking logic remains unchanged, still stopping at the first existing directory.

Why it's faster:

  • Dictionary operations have overhead for creation, hashing, and lookup that's eliminated by direct string usage
  • rsplit() is a simple string operation vs os.path.dirname() which involves more complex path parsing logic
  • Removing the unnecessary dict creation saves ~40 nanoseconds per call based on the profiler data

Impact on workloads:
Looking at the function reference, get_claude_config_path() is called from setup_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:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 31 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import os
import shutil
import sys
import tempfile
# Helper to monkeypatch get_windows_appdata_roaming in this module
import types

# imports
import pytest
from skyvern.cli.mcp import is_claude_desktop_installed

# --- Basic Test Cases ---

def test_darwin_claude_installed(tmp_path, monkeypatch):
    # Simulate Claude installed on macOS by creating the expected directory
    claude_dir = tmp_path / "Library" / "Application Support" / "Claude"
    claude_dir.mkdir(parents=True)
    # Patch expanduser to use tmp_path as home
    monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmp_path / p.lstrip("~\\/")))
    codeflash_output = is_claude_desktop_installed("darwin") # 13.0μs -> 11.1μs (16.6% faster)

def test_darwin_claude_not_installed(monkeypatch):
    # Simulate Claude not installed on macOS (dir does not exist)
    monkeypatch.setattr(os.path, "expanduser", lambda p: "/tmp/nonexistent" + p)
    codeflash_output = is_claude_desktop_installed("darwin") # 7.28μs -> 6.06μs (20.0% faster)

def test_linux_claude_installed_first_path(tmp_path, monkeypatch):
    # Simulate Claude installed at ~/.config/Claude
    claude_dir = tmp_path / ".config" / "Claude"
    claude_dir.mkdir(parents=True)
    monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmp_path / p.lstrip("~\\/")))
    codeflash_output = is_claude_desktop_installed("linux") # 13.6μs -> 12.4μs (10.2% faster)

def test_linux_claude_installed_second_path(tmp_path, monkeypatch):
    # Simulate Claude installed at ~/.local/share/Claude
    claude_dir = tmp_path / ".local" / "share" / "Claude"
    claude_dir.mkdir(parents=True)
    monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmp_path / p.lstrip("~\\/")))
    # Remove .config/Claude if it exists to force second path
    config_path = tmp_path / ".config" / "Claude"
    if config_path.exists():
        shutil.rmtree(config_path)
    codeflash_output = is_claude_desktop_installed("linux") # 18.1μs -> 16.5μs (9.42% faster)

def test_linux_claude_not_installed(monkeypatch):
    # No Claude directories exist
    monkeypatch.setattr(os.path, "expanduser", lambda p: "/tmp/nonexistent" + p)
    codeflash_output = is_claude_desktop_installed("linux") # 7.79μs -> 7.32μs (6.44% faster)

def test_unsupported_host_system():
    # Any unknown host_system should return False
    codeflash_output = is_claude_desktop_installed("windows") # 2.36μs -> 1.72μs (37.5% faster)
    codeflash_output = is_claude_desktop_installed("freebsd") # 674ns -> 535ns (26.0% faster)
    codeflash_output = is_claude_desktop_installed("") # 597ns -> 446ns (33.9% faster)

# --- Edge Test Cases ---

def test_linux_multiple_paths(tmp_path, monkeypatch):
    # Both ~/.config/Claude and ~/.local/share/Claude exist, should pick the first
    config_dir = tmp_path / ".config" / "Claude"
    local_dir = tmp_path / ".local" / "share" / "Claude"
    config_dir.mkdir(parents=True)
    local_dir.mkdir(parents=True)
    monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmp_path / p.lstrip("~\\/")))
    codeflash_output = is_claude_desktop_installed("linux") # 13.8μs -> 12.1μs (14.5% faster)

def test_linux_config_file_but_no_dir(tmp_path, monkeypatch):
    # Config file exists but not the directory
    config_file = tmp_path / ".config" / "Claude" / "claude_desktop_config.json"
    config_file.parent.mkdir(parents=True)
    config_file.touch()
    # Remove the directory after creating the file
    shutil.rmtree(config_file.parent)
    monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmp_path / p.lstrip("~\\/")))
    codeflash_output = is_claude_desktop_installed("linux") # 25.0μs -> 24.3μs (2.86% faster)

def test_linux_symlinked_claude_dir(tmp_path, monkeypatch):
    # Claude dir is a symlink, should still be detected
    real_dir = tmp_path / "real_Claude"
    real_dir.mkdir()
    config_dir = tmp_path / ".config"
    config_dir.mkdir()
    symlink_path = config_dir / "Claude"
    symlink_path.symlink_to(real_dir, target_is_directory=True)
    monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmp_path / p.lstrip("~\\/")))
    codeflash_output = is_claude_desktop_installed("linux") # 13.0μs -> 11.7μs (10.6% faster)

def test_linux_permission_error(monkeypatch):
    # Simulate os.path.exists raising a PermissionError
    def raise_perm(path):
        raise PermissionError("No access")
    monkeypatch.setattr(os.path, "expanduser", lambda p: "/tmp/Claude")
    monkeypatch.setattr(os.path, "exists", raise_perm)
    codeflash_output = is_claude_desktop_installed("linux") # 2.65μs -> 2.13μs (24.1% faster)

def test_linux_many_dirs_none_correct(tmp_path, monkeypatch):
    # 999 dummy dirs, none is correct
    for i in range(999):
        (tmp_path / f"dummy_{i}").mkdir()
    monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmp_path / p.lstrip("~\\/")))
    codeflash_output = is_claude_desktop_installed("linux") # 35.8μs -> 34.8μs (2.88% faster)
import os
import shutil
import tempfile
from pathlib import Path

# imports
import pytest
from skyvern.cli.mcp import is_claude_desktop_installed

# ----------- Basic Test Cases -----------
def test_linux_config_dir_exists(monkeypatch):
    # Test: Linux, ~/.config/Claude exists
    config_dir = os.path.expanduser("~/.config/Claude")
    os.makedirs(config_dir, exist_ok=True)
    monkeypatch.setattr(os.path, "exists", lambda p: p == config_dir)
    codeflash_output = is_claude_desktop_installed("linux") # 5.01μs -> 4.13μs (21.5% faster)

def test_linux_local_share_dir_exists(monkeypatch):
    # Test: Linux, ~/.local/share/Claude exists
    share_dir = os.path.expanduser("~/.local/share/Claude")
    os.makedirs(share_dir, exist_ok=True)
    monkeypatch.setattr(os.path, "exists", lambda p: p == share_dir)
    codeflash_output = is_claude_desktop_installed("linux") # 5.41μs -> 4.83μs (12.2% faster)

def test_linux_claude_dir_exists(monkeypatch):
    # Test: Linux, ~/Claude exists
    home_dir = os.path.expanduser("~/Claude")
    os.makedirs(home_dir, exist_ok=True)
    monkeypatch.setattr(os.path, "exists", lambda p: p == home_dir)
    codeflash_output = is_claude_desktop_installed("linux") # 6.00μs -> 5.51μs (8.94% faster)

def test_linux_no_config_dirs(monkeypatch):
    # Test: Linux, no config dirs exist
    monkeypatch.setattr(os.path, "exists", lambda p: False)
    codeflash_output = is_claude_desktop_installed("linux") # 6.32μs -> 5.70μs (10.8% faster)

def test_darwin_config_dir_exists(monkeypatch):
    # Test: macOS, config dir exists
    darwin_dir = os.path.expanduser("~/Library/Application Support/Claude")
    os.makedirs(darwin_dir, exist_ok=True)
    monkeypatch.setattr(os.path, "exists", lambda p: p == darwin_dir)
    codeflash_output = is_claude_desktop_installed("darwin") # 3.91μs -> 3.38μs (15.7% faster)

def test_darwin_config_dir_not_exists(monkeypatch):
    # Test: macOS, config dir does not exist
    monkeypatch.setattr(os.path, "exists", lambda p: False)
    codeflash_output = is_claude_desktop_installed("darwin") # 4.93μs -> 4.12μs (19.7% faster)

def test_unsupported_host_system():
    # Test: Unsupported host system
    codeflash_output = is_claude_desktop_installed("solaris") # 2.41μs -> 1.78μs (35.9% faster)

# ----------- Edge Test Cases -----------
def test_linux_multiple_dirs(monkeypatch):
    # Test: Linux, multiple config dirs exist, should return True for first found
    config_dir = os.path.expanduser("~/.config/Claude")
    share_dir = os.path.expanduser("~/.local/share/Claude")
    home_dir = os.path.expanduser("~/Claude")
    monkeypatch.setattr(os.path, "exists", lambda p: p in [config_dir, share_dir, home_dir])
    codeflash_output = is_claude_desktop_installed("linux") # 5.21μs -> 3.78μs (37.6% faster)

def test_linux_config_dir_permission_denied(monkeypatch):
    # Test: Linux, config dir exists but permission denied (simulate by raising OSError)
    config_dir = os.path.expanduser("~/.config/Claude")
    def fake_exists(p):
        if p == config_dir:
            raise OSError("Permission denied")
        return False
    monkeypatch.setattr(os.path, "exists", fake_exists)
    codeflash_output = is_claude_desktop_installed("linux") # 4.38μs -> 3.71μs (18.2% faster)

def test_darwin_config_dir_permission_denied(monkeypatch):
    # Test: macOS, config dir exists but permission denied (simulate by raising OSError)
    darwin_dir = os.path.expanduser("~/Library/Application Support/Claude")
    def fake_exists(p):
        if p == darwin_dir:
            raise OSError("Permission denied")
        return False
    monkeypatch.setattr(os.path, "exists", fake_exists)
    codeflash_output = is_claude_desktop_installed("darwin") # 5.53μs -> 4.05μs (36.3% faster)

# ----------- Large Scale Test Cases -----------

def test_deterministic_result(monkeypatch):
    # Test: Calling twice with same config should give same result
    config_dir = os.path.expanduser("~/.config/Claude")
    os.makedirs(config_dir, exist_ok=True)
    monkeypatch.setattr(os.path, "exists", lambda p: p == config_dir)
    codeflash_output = is_claude_desktop_installed("linux"); result1 = codeflash_output # 5.00μs -> 4.25μs (17.6% faster)
    codeflash_output = is_claude_desktop_installed("linux"); result2 = codeflash_output # 2.20μs -> 1.96μs (12.1% faster)

# ----------- Mutation Test Case -----------
def test_mutation_behavior(monkeypatch):
    # If function always returns True, this should fail for missing dirs
    monkeypatch.setattr(os.path, "exists", lambda p: False)
    codeflash_output = is_claude_desktop_installed("linux") # 6.45μs -> 6.07μs (6.24% faster)
    codeflash_output = is_claude_desktop_installed("darwin") # 3.25μs -> 2.62μs (24.1% faster)
    codeflash_output = is_claude_desktop_installed("wsl") # 185μs -> 184μs (0.416% faster)
    codeflash_output = is_claude_desktop_installed("solaris") # 1.23μs -> 898ns (36.9% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

To edit these changes git checkout codeflash/optimize-is_claude_desktop_installed-mjactf30 and push.

Codeflash Static Badge

The optimization achieves a **6% speedup** through three key changes that reduce CPU overhead:

**What was optimized:**
1. **Eliminated dictionary creation and lookups** - The original code created a `base_paths` dictionary 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.

2. **Replaced `os.path.dirname()` with string splitting** - In `is_claude_desktop_installed()`, changed from `os.path.dirname(get_claude_config_path())` to `config_path.rsplit(os.sep, 1)[0]`. String operations are faster than filesystem path parsing functions.

3. **Preserved short-circuiting behavior** - The Linux path checking logic remains unchanged, still stopping at the first existing directory.

**Why it's faster:**
- Dictionary operations have overhead for creation, hashing, and lookup that's eliminated by direct string usage
- `rsplit()` is a simple string operation vs `os.path.dirname()` which involves more complex path parsing logic
- Removing the unnecessary dict creation saves ~40 nanoseconds per call based on the profiler data

**Impact on workloads:**
Looking at the function reference, `get_claude_config_path()` is called from `setup_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.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 December 17, 2025 18:37
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash labels Dec 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant