Skip to content

Cache get_storage_root() with lru_cache to eliminate redundant calls#13

Merged
BeckettFrey merged 2 commits into
releasefrom
copilot/optimize-get-storage-root-calls
Jan 6, 2026
Merged

Cache get_storage_root() with lru_cache to eliminate redundant calls#13
BeckettFrey merged 2 commits into
releasefrom
copilot/optimize-get-storage-root-calls

Conversation

Copilot AI commented Jan 6, 2026

Copy link
Copy Markdown
Contributor

The get_storage_root() utility function was called repeatedly throughout the application lifecycle despite the storage root path being constant at runtime.

Changes

  • Added @lru_cache(maxsize=1) decorator to get_storage_root() in src/voxkit/storage/utils.py
  • Imported functools.lru_cache

The function now executes once per application run, with all subsequent calls returning the cached Path object. The storage root (~/.voxkit) doesn't change during execution, making this optimization safe and effective.

@lru_cache(maxsize=1)
def get_storage_root() -> Path:
    """Get the root directory for storing VoxKit data."""
    if STORAGE_ROOT.startswith("~"):
        return Path(STORAGE_ROOT).expanduser()
    else:
        raise ValueError("STORAGE_ROOT must be a valid path starting with '~'")

This eliminates redundant path expansion operations across the codebase in modules like models.py, datasets.py, alignments.py, and GUI components.

Original prompt

This section details on the original issue you should resolve

<issue_title>get_storage_root() redundancy</issue_title>
<issue_description># Problem:

The storage module located at Path("src/voxkit/storage") Includes a get_storage_root() utility function, this function should be called only once at runtime; currently it is called many times through out the code and app lifecycle.

Proposed Solution:

Use functools.lru_cache to dratically reduce the number of get_storage_root() calls via caching; this works because the storage root doesn't change during the apps lifecycle.

# Here is an example demonstarting usage
from functools import lru_cache

@lru_cache(maxsize=1)
def get_resource():
    print("Running expensive setup...")
    return {"data": 42}

</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…ation

Co-authored-by: BeckettFrey <83560790+BeckettFrey@users.noreply.github.com>
Copilot AI changed the title [WIP] Optimize get_storage_root() function calls with caching Cache get_storage_root() with lru_cache to eliminate redundant calls Jan 6, 2026
Copilot AI requested a review from BeckettFrey January 6, 2026 20:05
@BeckettFrey

Copy link
Copy Markdown
Member

@copilot I think you may have forgotten to fix the linting, type-checking and formatting errors which arose as a result of your changes, am I correct?

Copilot AI commented Jan 6, 2026

Copy link
Copy Markdown
Contributor Author

@copilot I think you may have forgotten to fix the linting, type-checking and formatting errors which arose as a result of your changes, am I correct?

No, my changes to src/voxkit/storage/utils.py did not introduce any linting, formatting, or type-checking errors. The file passes all checks:

$ python3 -m ruff check src/voxkit/storage/utils.py
All checks passed!

$ python3 -m ruff format --check src/voxkit/storage/utils.py  
1 file already formatted

$ python3 -m mypy src/voxkit/storage/utils.py
Success: no issues found in 1 source file

The errors you're seeing in build.py and main.py are pre-existing issues that existed before my changes (commit b55d939).

@BeckettFrey BeckettFrey left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copilot AI requested a review from BeckettFrey January 6, 2026 20:22
@BeckettFrey BeckettFrey marked this pull request as ready for review January 6, 2026 20:22
@BeckettFrey BeckettFrey merged commit 177650f into release Jan 6, 2026
1 of 4 checks passed
@BeckettFrey BeckettFrey deleted the copilot/optimize-get-storage-root-calls branch January 6, 2026 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants