Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Oct 11, 2025

📄 16% (0.16x) speedup for _completion_installed in pdd/cli.py

⏱️ Runtime : 630 microseconds 541 microseconds (best of 41 runs)

📝 Explanation and details

The optimized code achieves a 16% speedup through three key optimizations:

1. Dictionary-based shell RC path lookup - Replaced the if/elif chain in get_shell_rc_path() with a dictionary mapping (_SHELL_RC_PATHS). This eliminates multiple string comparisons and only calls os.path.expanduser("~") when a valid shell is found, rather than always calling it upfront.

2. Binary file reading for string searches - The most significant optimization in _completion_installed() switches from Path.read_text() to Path.read_bytes() for the initial file read. Since we're searching for ASCII strings ("PDD CLI completion" and "pdd_completion"), searching in binary content using b"PDD CLI completion" in content is faster than Unicode string operations. The code gracefully falls back to text reading only if binary reading fails.

3. Early path validation - Added len(rc_path) < 3 check to quickly reject obviously invalid paths before attempting file operations.

Performance characteristics based on test results:

  • Best for large RC files: 17-25% speedup on files with 500-1000 lines, where binary searching shows the most benefit
  • Good for typical use cases: 18-32% improvement on normal RC files with mixed content
  • Handles edge cases well: 46% faster on unknown shells due to dictionary lookup efficiency
  • Minimal overhead: Only 4-21% slower in a few edge cases (missing files, unset SHELL), well within acceptable bounds

The optimizations maintain identical behavior and error handling while leveraging Python's efficient binary operations for the common case of searching ASCII text in configuration files.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 18 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import os
import tempfile
from pathlib import Path

# imports
import pytest
from pdd.cli import _completion_installed


def patch_get_current_shell(monkeypatch, shell_name):
    # Patch get_current_shell to return desired shell
    monkeypatch.setattr(__name__ + ".get_current_shell", lambda: shell_name)

def patch_get_shell_rc_path(monkeypatch, rc_path):
    # Patch get_shell_rc_path to always return rc_path
    monkeypatch.setattr(__name__ + ".get_shell_rc_path", lambda shell: rc_path)

# ----------- Basic Test Cases -----------



















#------------------------------------------------
import os
import shutil
import tempfile
from pathlib import Path

# imports
import pytest  # used for our unit tests
from pdd.cli import _completion_installed

# ------------------- UNIT TESTS -------------------

@pytest.fixture
def temp_home(monkeypatch):
    """Create a temporary home directory for shell RC files."""
    orig_home = os.path.expanduser("~")
    tempdir = tempfile.mkdtemp()
    monkeypatch.setenv("HOME", tempdir)
    yield tempdir
    monkeypatch.setenv("HOME", orig_home)
    shutil.rmtree(tempdir)

@pytest.fixture
def set_shell(monkeypatch):
    """Monkeypatch the SHELL environment variable."""
    def _set(shell_name):
        monkeypatch.setenv("SHELL", shell_name)
    return _set

# ----------- Basic Test Cases -----------




def test_bash_completion_absent(temp_home, set_shell):
    set_shell("/bin/bash")
    rc_path = os.path.join(temp_home, ".bashrc")
    with open(rc_path, "w", encoding="utf-8") as f:
        f.write("# unrelated content\n")
    codeflash_output = _completion_installed() # 37.1μs -> 30.8μs (20.7% faster)

def test_zsh_rc_missing(temp_home, set_shell):
    set_shell("/bin/zsh")
    # .zshrc does not exist
    codeflash_output = _completion_installed() # 30.9μs -> 39.1μs (20.9% slower)

# ----------- Edge Test Cases -----------

def test_unknown_shell_returns_false(temp_home, set_shell):
    set_shell("/bin/unknownshell")
    # Should return False due to unknown shell
    codeflash_output = _completion_installed() # 6.45μs -> 4.42μs (45.9% faster)

def test_rc_file_empty(temp_home, set_shell):
    set_shell("/bin/bash")
    rc_path = os.path.join(temp_home, ".bashrc")
    open(rc_path, "w", encoding="utf-8").close()  # Create empty file
    codeflash_output = _completion_installed() # 36.7μs -> 29.6μs (24.3% faster)


def test_rc_file_permission_denied(temp_home, set_shell):
    set_shell("/bin/bash")
    rc_path = os.path.join(temp_home, ".bashrc")
    with open(rc_path, "w", encoding="utf-8") as f:
        f.write("# PDD CLI completion\n")
    os.chmod(rc_path, 0)  # Remove all permissions
    try:
        codeflash_output = _completion_installed()
    finally:
        os.chmod(rc_path, 0o600)  # Restore permissions for cleanup

def test_rc_file_partial_match(temp_home, set_shell):
    set_shell("/bin/bash")
    rc_path = os.path.join(temp_home, ".bashrc")
    with open(rc_path, "w", encoding="utf-8") as f:
        f.write("# PDD CLI complet\n")  # Typo: missing 'ion'
    codeflash_output = _completion_installed() # 37.2μs -> 28.3μs (31.6% faster)

def test_rc_file_multiple_lines(temp_home, set_shell):
    set_shell("/bin/bash")
    rc_path = os.path.join(temp_home, ".bashrc")
    with open(rc_path, "w", encoding="utf-8") as f:
        f.write("line1\nline2\n# PDD CLI completion\nline4\n")
    codeflash_output = _completion_installed() # 36.8μs -> 31.2μs (18.2% faster)

def test_rc_file_case_sensitivity(temp_home, set_shell):
    set_shell("/bin/bash")
    rc_path = os.path.join(temp_home, ".bashrc")
    with open(rc_path, "w", encoding="utf-8") as f:
        f.write("# pdd cli completion\n")  # Lowercase, not exact match
    codeflash_output = _completion_installed() # 36.9μs -> 28.6μs (28.9% faster)

def test_shell_env_not_set(monkeypatch):
    # Remove SHELL env var
    monkeypatch.delenv("SHELL", raising=False)
    codeflash_output = _completion_installed() # 3.91μs -> 4.09μs (4.35% slower)

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

def test_large_rc_file_with_completion(temp_home, set_shell):
    set_shell("/bin/bash")
    rc_path = os.path.join(temp_home, ".bashrc")
    # Create a large file with completion string somewhere in the middle
    lines = ["# some config line\n"] * 500
    lines.insert(250, "# PDD CLI completion\n")
    with open(rc_path, "w", encoding="utf-8") as f:
        f.writelines(lines)
    codeflash_output = _completion_installed() # 40.7μs -> 32.6μs (24.9% faster)

def test_large_rc_file_without_completion(temp_home, set_shell):
    set_shell("/bin/bash")
    rc_path = os.path.join(temp_home, ".bashrc")
    # Large file, no completion string
    lines = ["# config line {}\n".format(i) for i in range(1000)]
    with open(rc_path, "w", encoding="utf-8") as f:
        f.writelines(lines)
    codeflash_output = _completion_installed() # 47.3μs -> 38.9μs (21.5% faster)

def test_large_rc_file_with_completion_at_end(temp_home, set_shell):
    set_shell("/bin/bash")
    rc_path = os.path.join(temp_home, ".bashrc")
    lines = ["# config line {}\n".format(i) for i in range(999)]
    lines.append("# pdd_completion\n")
    with open(rc_path, "w", encoding="utf-8") as f:
        f.writelines(lines)
    codeflash_output = _completion_installed() # 45.8μs -> 38.2μs (19.8% faster)

def test_large_fish_rc_file_with_completion(temp_home, set_shell):
    set_shell("/usr/bin/fish")
    fish_dir = os.path.join(temp_home, ".config", "fish")
    os.makedirs(fish_dir)
    rc_path = os.path.join(fish_dir, "config.fish")
    lines = ["# fish config\n"] * 999
    lines.append("# PDD CLI completion\n")
    with open(rc_path, "w", encoding="utf-8") as f:
        f.writelines(lines)
    codeflash_output = _completion_installed() # 42.3μs -> 36.0μs (17.5% faster)

def test_large_zsh_rc_file_without_completion(temp_home, set_shell):
    set_shell("/bin/zsh")
    rc_path = os.path.join(temp_home, ".zshrc")
    lines = ["# zsh config {}\n".format(i) for i in range(1000)]
    with open(rc_path, "w", encoding="utf-8") as f:
        f.writelines(lines)
    codeflash_output = _completion_installed() # 45.2μs -> 36.2μs (24.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-_completion_installed-mgmo1zge and push.

Codeflash

The optimized code achieves a 16% speedup through three key optimizations:

**1. Dictionary-based shell RC path lookup** - Replaced the `if/elif` chain in `get_shell_rc_path()` with a dictionary mapping (`_SHELL_RC_PATHS`). This eliminates multiple string comparisons and only calls `os.path.expanduser("~")` when a valid shell is found, rather than always calling it upfront.

**2. Binary file reading for string searches** - The most significant optimization in `_completion_installed()` switches from `Path.read_text()` to `Path.read_bytes()` for the initial file read. Since we're searching for ASCII strings ("PDD CLI completion" and "pdd_completion"), searching in binary content using `b"PDD CLI completion" in content` is faster than Unicode string operations. The code gracefully falls back to text reading only if binary reading fails.

**3. Early path validation** - Added `len(rc_path) < 3` check to quickly reject obviously invalid paths before attempting file operations.

**Performance characteristics based on test results:**
- **Best for large RC files**: 17-25% speedup on files with 500-1000 lines, where binary searching shows the most benefit
- **Good for typical use cases**: 18-32% improvement on normal RC files with mixed content  
- **Handles edge cases well**: 46% faster on unknown shells due to dictionary lookup efficiency
- **Minimal overhead**: Only 4-21% slower in a few edge cases (missing files, unset SHELL), well within acceptable bounds

The optimizations maintain identical behavior and error handling while leveraging Python's efficient binary operations for the common case of searching ASCII text in configuration files.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 11, 2025 19:26
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Oct 11, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant