Skip to content

LVEC Memory Optimization: Cache Size Limits, LRU Eviction, and TTL Support#4

Merged
MohamedElashri merged 2 commits intomainfrom
memory_optimization
Apr 12, 2025
Merged

LVEC Memory Optimization: Cache Size Limits, LRU Eviction, and TTL Support#4
MohamedElashri merged 2 commits intomainfrom
memory_optimization

Conversation

@MohamedElashri
Copy link
Owner

@MohamedElashri MohamedElashri commented Apr 10, 2025

This PR enhances the LVEC caching system with memory optimization features to improve performance and resource utilization when working with large datasets in High Energy Physics applications.

Features Added

  1. Cache Size Limits: Configurable maximum cache size to prevent memory bloat
  2. LRU (Least Recently Used) Eviction Policy: Automatically remove least-used cached values when cache reaches capacity
  3. TTL (Time-To-Live): Support for setting expiration times for cached values
  4. Extended API: Methods for managing cache expiration and statistics
  5. Comprehensive Test Suite: Tests to validate memory optimization features

Implementation Details

Property Cache Enhancements

  • Extended PropertyCache class with:

    • max_size parameter to limit cache entries
    • LRU tracking using OrderedDict
    • TTL timestamp management
    • Automatic eviction of expired or least-used entries
    • Added performance statistics tracking

LVec Integration

  • Updated LVec constructor to accept max_cache_size and default_ttl parameters
  • Added methods to control TTL for specific properties
  • Enhanced cache statistics reporting
  • Maintained backward compatibility with existing code

New Methods added

  • set_ttl(property_name, ttl_seconds): Set or update TTL for a specific property
  • clear_expired(): Remove all expired values from the cache
  • cache_stats(): Get comprehensive cache performance statistics
  • cache_hit_ratio(): Get overall cache hit ratio

Testing & Docs

1.Added a dedicated test suite (test_cache_optimization.py) with 9 test cases covering:
- Cache size limitations and LRU eviction
- TTL expiration behavior
- Combined size and TTL functionality
- Cache invalidation with optimization features
- Statistics reporting accuracy
2. Update the docs to reflect the new updates
3.

Example Usage

# Create vector with cache size limit (LRU eviction)
v1 = LVec(1.0, 2.0, 3.0, 4.0, max_cache_size=100)

# Create vector with TTL for all properties
v2 = LVec(1.0, 2.0, 3.0, 4.0, default_ttl=60)  # 60 second TTL

# Set TTL for specific property
v1.set_ttl('pt', 10)  # 10 second TTL for transverse momentum

# Clear expired properties
expired_count = v1.clear_expired()

# Check cache performance
stats = v1.cache_stats
hit_ratio = v1.cache_hit_ratio

@MohamedElashri MohamedElashri self-assigned this Apr 10, 2025
@MohamedElashri MohamedElashri merged commit a7c50bb into main Apr 12, 2025
5 checks passed
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

Comments