Unify caching mechanisms and separate their management from Interpretations - #743
Closed
eb8680 wants to merge 13 commits into
Closed
Unify caching mechanisms and separate their management from Interpretations#743eb8680 wants to merge 13 commits into
Interpretations#743eb8680 wants to merge 13 commits into
Conversation
eb8680
marked this pull request as ready for review
August 4, 2026 16:04
Contributor
Author
|
Splitting into two separate PRs for easier review |
This was referenced Aug 4, 2026
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.
Addresses #633
This PR implements a new
weakref-based caching system that unifies and generalizes those introduced in #726 and #594Specifically, it adds a new context manager
effectful.internals.runtime.cachesuch that within acache()-delimited block, repeated calls toevaluatewith the same expression or data structure and active interpretation are cached.This new mechanism is backed by a new standalone utility module
effectful.internals.weak, based on a similar utility moduletorch.utils.weakin PyTorch, which implements a couple of different cache data structures generalizingweakref.WeakKeyDictionary:WeakIdKeyDictionarybehaves likeWeakKeyDictionary(including forbidding keys which cannot beweakref.refed, liketuples andints) but uses object identity rather than object equality/hashing to discriminate between keys. (This is the main thing provided bytorch.utils.weak)IdKeyDictionary, which stores strong references to keys that it discriminates between using identityAutoIdKeyDictionary, which extendsWeakIdKeyDictionaryandIdKeyDictionary's identity-based semantics by attempting to use a weak reference for a new key and falling back to a strong reference when that is disallowed by the key type, and ensuring that weak reference and strong reference keys cannot conflict with one another.weak_memoize, a helper function that can use these data structures to memoize a single-argument function analogous tofunctools.cacheThe PR also includes a number of small caching-related semantics-preserving performance fixes, and a benchmark derived from a RoboTL issue illustrating both asymptotic and constant-factor improvements on a representative workload.