Skip to content

Reduce cache bookkeeping overhead in CacheService #41

@bmurdock

Description

@bmurdock

CacheService does expensive bookkeeping work when writing large entries or evicting under pressure.

Current behavior:

  • calculateEntrySize() runs JSON.stringify(entry.data) for every non-string cached value to estimate memory usage.
  • evictLRU() copies the whole cache into an array and sorts it on every eviction pass.
  • Eviction is based on timestamp, but normal reads do not update that timestamp, so the implementation still does not provide true LRU behavior.

Why this is inefficient:

  • Large cached objects pay extra CPU and temporary memory overhead during writes.
  • Eviction becomes O(n log n) plus repeated size lookups.
  • The expensive work does not buy accurate LRU semantics.

Relevant location:

  • src/services/cache-service.ts:328-365

Suggested direction:

  • Store entry size once at insert time and reuse it on overwrite/delete/eviction.
  • If LRU behavior is desired, maintain access ordering explicitly instead of sorting the full cache.

Audit source: current inefficiencies.md rank #4.

Related history:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions