function dynamicmap_memoization
A context manager that temporarily controls memoization behavior of a callable object based on the state of associated streams, disabling memoization when transient streams are triggering.
/tf/active/vicechatdev/patches/spaces.py
661 - 673
moderate
Purpose
This context manager is used in DynamicMap operations to intelligently manage caching behavior. It temporarily disables memoization on a callable object when any of the supplied streams are both transient and currently triggering, preventing stale cached results during stream updates. After the context exits, it restores the original memoization state.
Source Code
def dynamicmap_memoization(callable_obj, streams):
"""
Determine whether the Callable should have memoization enabled
based on the supplied streams (typically by a
DynamicMap). Memoization is disabled if any of the streams require
it it and are currently in a triggered state.
"""
memoization_state = bool(callable_obj._stream_memoization)
callable_obj._stream_memoization &= not any(s.transient and s._triggering for s in streams)
try:
yield
finally:
callable_obj._stream_memoization = memoization_state
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
callable_obj |
- | - | positional_or_keyword |
streams |
- | - | positional_or_keyword |
Parameter Details
callable_obj: A callable object (typically a Callable instance) that has a '_stream_memoization' attribute controlling whether results should be cached. This object's memoization state will be temporarily modified based on stream states.
streams: An iterable collection of Stream objects that may affect the callable's execution. Each stream should have 'transient' and '_triggering' attributes. Transient streams that are currently triggering will cause memoization to be disabled.
Return Value
This is a context manager (generator function decorated with @contextmanager), so it yields control back to the caller within the context block. It does not return a meaningful value; the yield statement is used for context management. The function's effect is the temporary modification of callable_obj._stream_memoization.
Dependencies
contextlib
Required Imports
from contextlib import contextmanager
Usage Example
from contextlib import contextmanager
class MockCallable:
def __init__(self):
self._stream_memoization = True
def __call__(self, *args):
return args
class MockStream:
def __init__(self, transient=False, triggering=False):
self.transient = transient
self._triggering = triggering
# Create callable and streams
callable_obj = MockCallable()
streams = [MockStream(transient=True, triggering=True)]
print(f"Before: {callable_obj._stream_memoization}") # True
with dynamicmap_memoization(callable_obj, streams):
print(f"Inside: {callable_obj._stream_memoization}") # False (disabled due to triggering transient stream)
# Perform operations without memoization
result = callable_obj(1, 2, 3)
print(f"After: {callable_obj._stream_memoization}") # True (restored)
Best Practices
- Always use this as a context manager (with statement) to ensure memoization state is properly restored
- Ensure callable_obj has a '_stream_memoization' attribute before calling this function
- Verify that all streams in the streams parameter have the required 'transient' and '_triggering' attributes
- This is typically used internally by DynamicMap implementations rather than directly by end users
- The function uses bitwise AND (&=) operations, so _stream_memoization should be boolean or boolean-compatible
- Memoization is only disabled when streams are both transient AND triggering; non-transient or non-triggering streams do not affect memoization
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class Callable 62.3% similar
-
class DynamicMap 61.1% similar
-
function get_nested_streams 56.7% similar
-
function dimensioned_streams 56.7% similar
-
class periodic_v1 55.5% similar