Fix lru_cache(...) resetting when cell is rerun#9609
Open
leoadberg wants to merge 1 commit into
Open
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
recheck |
Contributor
There was a problem hiding this comment.
No issues found across 2 files
Architecture diagram
sequenceDiagram
participant User
participant Kernel as Marimo Kernel
participant ExecReq as ExecReqProvider
participant Cell as Cell Executor
participant LRUCache as @mo.lru_cache Decorator
participant CacheLogic as _cache_call._set_context
participant Globals as Kernel Globals
Note over User,Globals: LRU Cache Persistence Across Cell Reruns
User->>Kernel: Run cell with @lru_cache(maxsize=128)
Kernel->>ExecReq: Build execution request
ExecReq-->>Kernel: Execution request with cell code
Kernel->>Cell: Execute cell code
Cell->>LRUCache: Apply @lru_cache(maxsize=128)
alt Decorator called with parentheses (callable=True)
LRUCache->>LRUCache: _frame_offset=1
else Decorator called as @lru_cache (callable=False)
LRUCache->>LRUCache: _frame_offset=2
end
LRUCache->>CacheLogic: _set_context() to determine scope
alt Frame offset correct (global scope detected)
CacheLogic-->>LRUCache: Context set as global
LRUCache->>Globals: Register cached function
Cell->>LRUCache: Execute foo()
LRUCache->>Globals: Check cache miss
Globals-->>LRUCache: No cache entry
LRUCache->>Cell: Execute foo() body (print "ran")
Cell-->>LRUCache: Return result
LRUCache->>Globals: Store result in cache
Cell->>LRUCache: Execute foo() again
LRUCache->>Globals: Check cache hit
Globals-->>LRUCache: Cached result
LRUCache-->>Cell: Return cached result (no print)
else Frame offset wrong (local scope detected)
CacheLogic-->>LRUCache: Context set as local
Note over LRUCache,Globals: This path caused cache reset on rerun
end
Cell-->>Kernel: Cell execution complete
Kernel-->>User: Cell output (printed "ran" once)
Note over User,Kernel: Cell rerun (same cell)
User->>Kernel: Rerun the same cell
Kernel->>Cell: Execute cell code again
Cell->>LRUCache: Re-apply decorator
LRUCache->>CacheLogic: _set_context() with corrected frame offset
alt Correct frame offset (PR fix)
CacheLogic-->>LRUCache: Context set as global
LRUCache->>Globals: Reuse existing cached function
Cell->>LRUCache: Execute foo()
LRUCache->>Globals: Check cache hit
Globals-->>LRUCache: Previous cache entry exists
LRUCache-->>Cell: Return cached result (no re-execution)
Note over Cell,Globals: foo() body not re-run, "ran" not printed again
Cell->>LRUCache: Execute foo() again
LRUCache->>Globals: Another cache hit
Globals-->>LRUCache: Cached result
LRUCache-->>Cell: Return cached result
end
Cell-->>Kernel: Cell execution complete
Kernel-->>User: No additional output (cache persisted across rerun)
Author
|
I have read the CLA Document and I hereby sign the CLA |
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.
📝 Summary
Previously
@mo.lru_cache(maxsize=128)(or similar) would reset after the cell is rerun because the extra wrapping call caused the frame offset to point one frame too high, causing the_cache_call._set_contextto think the function isn't global.📋 Pre-Review Checklist
✅ Merge Checklist
I have read the CLA Document and I hereby sign the CLA