Skip to content

⚡️ Speed up function templates_path by 1,265%#2

Open
codeflash-ai[bot] wants to merge 1 commit into
mainfrom
codeflash/optimize-templates_path-mglknz07
Open

⚡️ Speed up function templates_path by 1,265%#2
codeflash-ai[bot] wants to merge 1 commit into
mainfrom
codeflash/optimize-templates_path-mglknz07

Conversation

@codeflash-ai
Copy link
Copy Markdown

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

📄 1,265% (12.65x) speedup for templates_path in higgsfield/internal/util.py

⏱️ Runtime : 205 microseconds 15.0 microseconds (best of 78 runs)

📝 Explanation and details

The optimization implements function-level caching to avoid repeated Path object construction. Instead of creating a new Path(ROOT_DIR) / "static" / "templates" object on every call, the optimized version:

  1. Caches the Path object as a function attribute (_cached_path) after the first call
  2. Reuses the cached instance on subsequent calls, simply returning the pre-constructed Path

This eliminates the overhead of:

  • Path object instantiation (constructor calls)
  • String concatenation operations for path joining
  • Internal pathlib validation and normalization

The line profiler shows the dramatic impact: the original version spent 833,913ns total across 23 calls (36,257ns per call), while the optimized version spent only 81,330ns total with most time on the first call (39,470ns) and subsequent calls taking just ~611ns each.

This optimization is particularly effective for utility functions that return static paths - functions likely to be called multiple times but always return the same result. The test results show consistent 10-16x speedups across all scenarios, from basic usage to edge cases with deep nesting and large project structures, making it beneficial for any codebase that frequently accesses template paths.

Correctness verification report:

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

# imports
import pytest  # used for our unit tests
from higgsfield.internal.util import templates_path

# unit tests

@pytest.fixture
def setup_test_environment(tmp_path, monkeypatch):
    """
    Sets up a temporary directory structure for testing.
    Returns the simulated __file__ path and the expected templates path.
    """
    # Create a fake project structure: /tmp/project/higgsfield/internal/util.py
    project_root = tmp_path / "project"
    internal_dir = project_root / "higgsfield" / "internal"
    internal_dir.mkdir(parents=True)
    util_py = internal_dir / "util.py"
    util_py.write_text("# dummy util.py file")

    # The __file__ should be .../project/higgsfield/internal/util.py
    fake_file = str(util_py)

    # The ROOT_DIR should be .../project
    expected_templates_path = project_root / "static" / "templates"

    # Patch __file__ in the module's global namespace
    monkeypatch.setitem(sys.modules[__name__].__dict__, "__file__", fake_file)

    return fake_file, expected_templates_path


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

def test_templates_path_returns_path_object(setup_test_environment):
    """
    Test that templates_path returns a Path object.
    """
    _, _ = setup_test_environment
    codeflash_output = templates_path(); result = codeflash_output # 12.3μs -> 705ns (1648% faster)

def test_templates_path_correct_location(setup_test_environment):
    """
    Test that templates_path returns the correct path based on __file__ location.
    """
    _, expected_path = setup_test_environment
    codeflash_output = templates_path(); result = codeflash_output # 11.6μs -> 673ns (1620% faster)

def test_templates_path_string_conversion(setup_test_environment):
    """
    Test that the returned Path can be converted to string and matches expected.
    """
    _, expected_path = setup_test_environment
    codeflash_output = templates_path(); result = codeflash_output # 11.7μs -> 606ns (1827% faster)

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

def test_templates_path_when_static_templates_do_not_exist(setup_test_environment):
    """
    Test that the function returns the correct path even if the directories don't exist.
    """
    _, expected_path = setup_test_environment
    codeflash_output = templates_path(); result = codeflash_output # 11.5μs -> 672ns (1618% faster)

def test_templates_path_with_symlinked_project_root(tmp_path, monkeypatch):
    """
    Test that the function works correctly if the project root is a symlink.
    """
    # Create real project root and a symlink to it
    real_root = tmp_path / "real_project"
    internal_dir = real_root / "higgsfield" / "internal"
    internal_dir.mkdir(parents=True)
    util_py = internal_dir / "util.py"
    util_py.write_text("# dummy util.py file")
    symlink_root = tmp_path / "symlink_project"
    symlink_root.symlink_to(real_root, target_is_directory=True)

    # __file__ will be under the symlinked path
    fake_file = str(symlink_root / "higgsfield" / "internal" / "util.py")
    expected_path = symlink_root / "static" / "templates"

    # Patch __file__ in the module's global namespace
    monkeypatch.setitem(sys.modules[__name__].__dict__, "__file__", fake_file)

    codeflash_output = templates_path(); result = codeflash_output # 9.04μs -> 715ns (1164% faster)

def test_templates_path_when_file_is_at_root_level(tmp_path, monkeypatch):
    """
    Test that the function works if __file__ is only one directory deep (edge case).
    """
    # /tmp/project/util.py
    project_root = tmp_path / "project"
    project_root.mkdir()
    util_py = project_root / "util.py"
    util_py.write_text("# dummy util.py file")

    fake_file = str(util_py)
    # ROOT_DIR will be /tmp (since os.path.dirname(os.path.dirname(file)))
    expected_path = tmp_path / "static" / "templates"

    monkeypatch.setitem(sys.modules[__name__].__dict__, "__file__", fake_file)

    codeflash_output = templates_path(); result = codeflash_output # 8.76μs -> 697ns (1157% faster)

def test_templates_path_when_file_is_deeply_nested(tmp_path, monkeypatch):
    """
    Test that the function works with deep nesting (more than two levels).
    """
    # /tmp/project/a/b/c/d/e/f/g/util.py
    deep_dir = tmp_path / "project" / "a" / "b" / "c" / "d" / "e" / "f" / "g"
    deep_dir.mkdir(parents=True)
    util_py = deep_dir / "util.py"
    util_py.write_text("# dummy util.py file")

    fake_file = str(util_py)
    # ROOT_DIR will be /tmp/project/a/b/c/d/e/f
    expected_path = tmp_path / "project" / "a" / "b" / "c" / "d" / "e" / "static" / "templates"

    monkeypatch.setitem(sys.modules[__name__].__dict__, "__file__", fake_file)

    codeflash_output = templates_path(); result = codeflash_output # 9.29μs -> 766ns (1113% faster)

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

def test_templates_path_with_many_sibling_dirs(tmp_path, monkeypatch):
    """
    Test performance and correctness with many sibling directories under root.
    """
    # /tmp/project/higgsfield/internal/util.py
    project_root = tmp_path / "project"
    internal_dir = project_root / "higgsfield" / "internal"
    internal_dir.mkdir(parents=True)
    util_py = internal_dir / "util.py"
    util_py.write_text("# dummy util.py file")

    # Create many sibling directories under project_root
    for i in range(500):
        (project_root / f"sibling_{i}").mkdir()

    fake_file = str(util_py)
    expected_path = project_root / "static" / "templates"
    monkeypatch.setitem(sys.modules[__name__].__dict__, "__file__", fake_file)

    codeflash_output = templates_path(); result = codeflash_output # 11.0μs -> 967ns (1043% faster)

def test_templates_path_with_long_path_names(tmp_path, monkeypatch):
    """
    Test the function with very long directory names (close to OS path length limits).
    """
    # Create a long directory name (but not exceeding OS limits)
    long_name = "a" * 50
    deep_dir = tmp_path
    for _ in range(10):  # 10*50 = 500 chars
        deep_dir = deep_dir / long_name
        deep_dir.mkdir()
    util_py = deep_dir / "util.py"
    util_py.write_text("# dummy util.py file")

    fake_file = str(util_py)
    # ROOT_DIR is two levels up
    expected_path = deep_dir.parent.parent / "static" / "templates"

    monkeypatch.setitem(sys.modules[__name__].__dict__, "__file__", fake_file)

    codeflash_output = templates_path(); result = codeflash_output # 9.63μs -> 738ns (1204% faster)




#------------------------------------------------
import os
# function to test
# (This is the function under test, as described in the prompt)
import sys
from pathlib import Path

# imports
import pytest
from higgsfield.internal.util import templates_path


@pytest.mark.basic
def test_templates_path_normal(monkeypatch, tmp_path):
    """
    Basic test: templates_path returns the correct path when __file__ is in a standard location.
    """
    # Arrange: Simulate a directory structure: /tmp/project/higgsfield/internal/util.py
    project_root = tmp_path / "project"
    internal_dir = project_root / "higgsfield" / "internal"
    internal_dir.mkdir(parents=True)
    util_py = internal_dir / "util.py"
    util_py.write_text("# dummy file")
    # We want ROOT_DIR to be /tmp/project/higgsfield

    # Place static/templates under the root
    static_templates = project_root / "static" / "templates"
    static_templates.mkdir(parents=True)

    # Patch __file__ to point to util.py
    monkeypatch.setattr(sys.modules[__name__], "__file__", str(util_py))

    # Act
    codeflash_output = templates_path(); result = codeflash_output # 9.41μs -> 782ns (1104% faster)

    # Assert
    expected = project_root / "static" / "templates"

@pytest.mark.basic
def test_templates_path_returns_path_object(monkeypatch, tmp_path):
    """
    Basic test: templates_path returns a Path object, not a string.
    """
    # Arrange
    project_root = tmp_path / "myproj"
    internal_dir = project_root / "higgsfield" / "internal"
    internal_dir.mkdir(parents=True)
    util_py = internal_dir / "util.py"
    util_py.write_text("# dummy file")
    monkeypatch.setattr(sys.modules[__name__], "__file__", str(util_py))
    (project_root / "static" / "templates").mkdir(parents=True)

    # Act
    codeflash_output = templates_path(); result = codeflash_output # 9.08μs -> 725ns (1152% faster)

@pytest.mark.basic
def test_templates_path_with_trailing_slash(monkeypatch, tmp_path):
    """
    Basic test: __file__ with a trailing slash in path should not affect result.
    """
    project_root = tmp_path / "proj"
    internal_dir = project_root / "higgsfield" / "internal"
    internal_dir.mkdir(parents=True)
    util_py = internal_dir / "util.py"
    util_py.write_text("# dummy file")
    monkeypatch.setattr(sys.modules[__name__], "__file__", str(util_py) + "/")
    (project_root / "static" / "templates").mkdir(parents=True)

    codeflash_output = templates_path(); result = codeflash_output # 9.31μs -> 744ns (1151% faster)
    expected = project_root / "static" / "templates"

# Edge Cases

@pytest.mark.edge
def test_templates_path_static_templates_missing(monkeypatch, tmp_path):
    """
    Edge: static/templates directory does not exist.
    The function should still return the correct path, even if it doesn't exist.
    """
    project_root = tmp_path / "proj"
    internal_dir = project_root / "higgsfield" / "internal"
    internal_dir.mkdir(parents=True)
    util_py = internal_dir / "util.py"
    util_py.write_text("# dummy file")
    monkeypatch.setattr(sys.modules[__name__], "__file__", str(util_py))

    codeflash_output = templates_path(); result = codeflash_output # 9.50μs -> 763ns (1145% faster)
    expected = project_root / "static" / "templates"

@pytest.mark.edge
def test_templates_path_deeply_nested(monkeypatch, tmp_path):
    """
    Edge: __file__ is several directories deep, should still compute correct path.
    """
    # /tmp/foo/bar/baz/qux/higgsfield/internal/util.py
    root = tmp_path / "foo" / "bar" / "baz" / "qux"
    internal_dir = root / "higgsfield" / "internal"
    internal_dir.mkdir(parents=True)
    util_py = internal_dir / "util.py"
    util_py.write_text("# dummy file")
    monkeypatch.setattr(sys.modules[__name__], "__file__", str(util_py))
    (root / "static" / "templates").mkdir(parents=True)

    codeflash_output = templates_path(); result = codeflash_output # 9.48μs -> 781ns (1114% faster)
    expected = root / "static" / "templates"

@pytest.mark.edge
def test_templates_path_symlink(monkeypatch, tmp_path):
    """
    Edge: __file__ is a symlink; function should resolve to the real path.
    """
    # Arrange
    project_root = tmp_path / "proj"
    internal_dir = project_root / "higgsfield" / "internal"
    internal_dir.mkdir(parents=True)
    util_py = internal_dir / "util.py"
    util_py.write_text("# dummy file")
    symlink_py = tmp_path / "util_link.py"
    symlink_py.symlink_to(util_py)
    monkeypatch.setattr(sys.modules[__name__], "__file__", str(symlink_py))
    (project_root / "static" / "templates").mkdir(parents=True)

    # Act
    codeflash_output = templates_path(); result = codeflash_output # 9.36μs -> 695ns (1247% faster)

    # Assert
    expected = project_root / "static" / "templates"

@pytest.mark.edge
def test_templates_path_file_is_relative(monkeypatch, tmp_path):
    """
    Edge: __file__ is a relative path.
    """
    project_root = tmp_path / "proj"
    internal_dir = project_root / "higgsfield" / "internal"
    internal_dir.mkdir(parents=True)
    util_py = internal_dir / "util.py"
    util_py.write_text("# dummy file")
    rel_path = os.path.relpath(util_py, os.getcwd())
    monkeypatch.setattr(sys.modules[__name__], "__file__", rel_path)
    (project_root / "static" / "templates").mkdir(parents=True)

    codeflash_output = templates_path(); result = codeflash_output # 9.29μs -> 696ns (1235% faster)
    expected = project_root / "static" / "templates"

@pytest.mark.edge
def test_templates_path_file_is_dot(monkeypatch, tmp_path):
    """
    Edge: __file__ is '.' (current directory), which is nonsensical but should not crash.
    """
    monkeypatch.setattr(sys.modules[__name__], "__file__", ".")
    # The result will depend on the current working directory
    ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(".")))
    expected = Path(ROOT_DIR) / "static" / "templates"
    codeflash_output = templates_path(); result = codeflash_output # 7.59μs -> 652ns (1063% faster)

# Large Scale

@pytest.mark.large
def test_templates_path_large_project(monkeypatch, tmp_path):
    """
    Large scale: Project with many directories/files, ensure function is not affected by size.
    """
    # Create a large directory structure
    project_root = tmp_path / "bigproj"
    internal_dir = project_root / "higgsfield" / "internal"
    internal_dir.mkdir(parents=True)
    util_py = internal_dir / "util.py"
    util_py.write_text("# dummy file")

    # Add many sibling directories/files under project_root
    for i in range(500):
        (project_root / f"dir_{i}").mkdir()
        (project_root / f"file_{i}.txt").write_text("x")

    # Add many files under static/templates
    static_templates = project_root / "static" / "templates"
    static_templates.mkdir(parents=True)
    for i in range(500):
        (static_templates / f"template_{i}.html").write_text("<html></html>")

    monkeypatch.setattr(sys.modules[__name__], "__file__", str(util_py))

    codeflash_output = templates_path(); result = codeflash_output # 12.0μs -> 1.03μs (1061% faster)
    expected = static_templates

@pytest.mark.large

def test_templates_path_performance(monkeypatch, tmp_path):
    """
    Large scale: Performance test with many files in static/templates.
    """
    project_root = tmp_path / "proj"
    internal_dir = project_root / "higgsfield" / "internal"
    internal_dir.mkdir(parents=True)
    util_py = internal_dir / "util.py"
    util_py.write_text("# dummy file")
    static_templates = project_root / "static" / "templates"
    static_templates.mkdir(parents=True)
    for i in range(999):
        (static_templates / f"file_{i}.txt").write_text("data")

    monkeypatch.setattr(sys.modules[__name__], "__file__", str(util_py))

    codeflash_output = templates_path(); result = codeflash_output # 12.0μs -> 1.05μs (1045% faster)
    expected = static_templates
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from higgsfield.internal.util import templates_path

def test_templates_path():
    templates_path()
🔎 Concolic Coverage Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
codeflash_concolic_k2mznyx6/tmpzxxsp4c8/test_concolic_coverage.py::test_templates_path 13.4μs 579ns 2215%✅

To edit these changes git checkout codeflash/optimize-templates_path-mglknz07 and push.

Codeflash

The optimization implements **function-level caching** to avoid repeated Path object construction. Instead of creating a new `Path(ROOT_DIR) / "static" / "templates"` object on every call, the optimized version:

1. **Caches the Path object** as a function attribute (`_cached_path`) after the first call
2. **Reuses the cached instance** on subsequent calls, simply returning the pre-constructed Path

This eliminates the overhead of:
- Path object instantiation (constructor calls)
- String concatenation operations for path joining
- Internal pathlib validation and normalization

The line profiler shows the dramatic impact: the original version spent 833,913ns total across 23 calls (36,257ns per call), while the optimized version spent only 81,330ns total with most time on the first call (39,470ns) and subsequent calls taking just ~611ns each.

This optimization is particularly effective for **utility functions that return static paths** - functions likely to be called multiple times but always return the same result. The test results show consistent 10-16x speedups across all scenarios, from basic usage to edge cases with deep nesting and large project structures, making it beneficial for any codebase that frequently accesses template paths.
@codeflash-ai codeflash-ai Bot requested a review from mashraf-222 October 11, 2025 01:03
@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.

0 participants