Repository navigation
Support infinite TTL for Multitons - #3
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.