Skip to content

Support infinite TTL for Multitons - #3

Merged
sjperkins merged 3 commits into
mainfrom
infinite-ttl
Jun 4, 2026
Merged

sjperkins merged 3 commits into
mainfrom
infinite-ttl

Conversation

@sjperkins

Copy link
Copy Markdown
Member

Add with_ttl(ttl) and with_infinite_ttl() to cache instances eternally (ttl=math.inf), keeping with_args(ttl=) as the core setter they delegate to.

Eternal entries are pushed onto the expiry heap like any other, but their inf deadline never satisfies the sweep condition, so they would accumulate stale heap tuples unboundedly on every access. Bound the heap with _compact_heap: once it grows past max(_HEAP_COMPACT_MIN, _HEAP_COMPACT_FACTOR * len(cache)), rebuild it from the live cache, structurally discarding both inf tuples and finite tuples whose seq no longer matches their cache entry (stale TTL resets and releases the pop loop can't reach).

Validate ttl in with_args: reject non-Real and nan, which would never satisfy the expiry comparison and leak in the heap.

Add with_ttl(ttl) and with_infinite_ttl() to cache instances eternally
(ttl=math.inf), keeping with_args(ttl=) as the core setter they delegate to.

Eternal entries are pushed onto the expiry heap like any other, but their inf
deadline never satisfies the sweep condition, so they would accumulate stale
heap tuples unboundedly on every access. Bound the heap with _compact_heap:
once it grows past max(_HEAP_COMPACT_MIN, _HEAP_COMPACT_FACTOR * len(cache)),
rebuild it from the live cache, structurally discarding both inf tuples and
finite tuples whose seq no longer matches their cache entry (stale TTL resets
and releases the pop loop can't reach).

Validate ttl in with_args: reject non-Real and nan, which would never satisfy
the expiry comparison and leak in the heap.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
An inf deadline can never satisfy the sweep condition, so pushing
eternal entries onto the expiry heap only bloated it and forced
periodic compaction to reclaim them. Skip the push entirely in
_write_entry; the heap now holds only finite-TTL tuples.

Update the class and method docstrings to match, and prove the
behavior: test_multiton_eternal_ttl_never_grows_heap asserts the heap
stays empty across repeated eternal-entry access, and the mixed
finite/eternal test now checks the eternal entry contributes no heap
tuple.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@sjperkins
sjperkins merged commit ec40070 into main Jun 4, 2026
@sjperkins
sjperkins deleted the infinite-ttl branch June 4, 2026 10:12
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