Skip to content

⚡ Optimize calculator caching with LRU eviction - #12

Merged
niklashoelter merged 3 commits into
masterfrom
optimize-calculator-cache-9066723556091271429
Feb 2, 2026
Merged

niklashoelter merged 3 commits into
masterfrom
optimize-calculator-cache-9066723556091271429

Conversation

@niklashoelter

Copy link
Copy Markdown
Owner
  • 💡 What: Replaced the unbounded _CALCULATOR_CACHE dictionary with functools.lru_cache(maxsize=1).
  • 🎯 Why: The previous implementation stored every loaded calculator in a global dictionary, leading to potential memory leaks when running with multiple configurations. FAIRChemCalculator instances are memory-intensive (GPU/CPU), so keeping old instances is inefficient.
  • 📊 Measured Improvement:
    • Baseline: Cache size grew linearly with unique configurations (unbounded).
    • Improvement: Cache size is now strictly bounded to 1. Old models are evicted and garbage collected.
    • Verification: Added tests/test_cache.py which verifies the bounded behavior and hit/miss stats. Confirmed existing tests pass.

PR created automatically by Jules for task 9066723556091271429 started by @niklashoelter

- Replace `_CALCULATOR_CACHE` dict with `functools.lru_cache` on a helper function.
- Limit cache size to 1 to prevent memory leaks with heavy ML models.
- Add `tests/test_cache.py` to verify cache behavior.
- Ensure Config object is reconstructed correctly for the loader.

Co-authored-by: niklashoelter <83964137+niklashoelter@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

google-labs-jules Bot and others added 2 commits January 30, 2026 13:53
- Fix unused imports and import sorting in `tests/test_cache.py`.
- Fix line length violation in `src/gpuma/io_handler.py`.
- Verify fix with `ruff check`.

Co-authored-by: niklashoelter <83964137+niklashoelter@users.noreply.github.com>
@niklashoelter
niklashoelter merged commit 9348b6d into master Feb 2, 2026
0 of 3 checks passed
@niklashoelter
niklashoelter deleted the optimize-calculator-cache-9066723556091271429 branch February 2, 2026 13:35
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.

1 participant