-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use a class for CachedMapper
-derived mappers instead of a dict
#549
base: main
Are you sure you want to change the base?
Conversation
a34936a
to
378d439
Compare
1cf2454
to
df7db39
Compare
d40153c
to
e9b4ac5
Compare
c25dce2
to
7a556ad
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pytato/transform/__init__.py
Outdated
# FIXME: Can this be inlined? | ||
def get_key( | ||
self, expr: CacheExprT, *args: P.args, **kwargs: P.kwargs) -> CacheKeyT: | ||
"""Compute the key for an input expression.""" | ||
return self._key_func(expr, *args, **kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possibly worth inlining this to avoid the Python call overhead? How would I go about doing that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(see above?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# FIXME: Figure out the right way to type annotate these | ||
| tuple[CacheExprT, tuple[Any, ...], dict[str, Any]], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How should I annotate the args
tuple and kwargs
dict here so that it matches *args: P.args, **kwargs: P.kwargs
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pytato/transform/__init__.py
Outdated
# FIXME: Can this just inherit from CachedMapper? | ||
class CombineMapper(Mapper[ResultT, FunctionResultT, []]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was there a reason that CombineMapper
doesn't inherit from CachedMapper
? Seems like we could plausibly do that now, I just wanted to ask before I take a stab at it.
a60b0f3
to
387bafb
Compare
pytato/transform/__init__.py
Outdated
:arg key_func: Function to compute a hashable cache key from an input | ||
expression and any extra arguments. | ||
""" | ||
self._key_func = key_func |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self._key_func = key_func | |
self.get_key = key_func |
pytato/transform/__init__.py
Outdated
# FIXME: Can this be inlined? | ||
def get_key( | ||
self, expr: CacheExprT, *args: P.args, **kwargs: P.kwargs) -> CacheKeyT: | ||
"""Compute the key for an input expression.""" | ||
return self._key_func(expr, *args, **kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(see above?)
pytato/transform/__init__.py
Outdated
# FIXME: Can this be inlined? | ||
def get_key( | ||
self, expr: CacheExprT, *args: P.args, **kwargs: P.kwargs) -> CacheKeyT: | ||
"""Compute the key for an input expression.""" | ||
return self._key_func(expr, *args, **kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# FIXME: Figure out the right way to type annotate these | ||
| tuple[CacheExprT, tuple[Any, ...], dict[str, Any]], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""Compute the key for an input expression.""" | ||
return self._key_func(expr, *args, **kwargs) | ||
|
||
def add( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternative to add/retrieve: "just give me the thing from the cache and OBTW here's a function to compute it if you don't have it"
(No strong preference from my end)
pytato/transform/__init__.py
Outdated
# Arrays are cached separately for each call stack frame, but | ||
# functions are cached globally | ||
_function_cache: dict[Hashable, FunctionResultT] | None = None | ||
_cache: CachedMapper._CacheT[ResultT, P] | None = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pyright doesn't seem to like this. As yet I'm unclear why.
pip install pyright
Access to generic instance variable through class is ambiguous
pytato/transform/__init__.py
Outdated
@@ -768,40 +884,53 @@ def map_named_call_result(self, expr: NamedCallResult, | |||
|
|||
# {{{ CombineMapper | |||
|
|||
# FIXME: Can this just inherit from CachedMapper? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my POV: no reason why it shouldn't.
40b2a64
to
9cbe7f4
Compare
9cbe7f4
to
8546893
Compare
Adds classes to represent the caches of
CachedMapper
-derived mappers, which will be useful for adding array duplication checks and result deduplication (#550). Both of these features add additional cache dictionaries and cache retrieval/addition logic; this change minimizes the amount of logic that must be duplicated when mappers overriderec
, as well as minimizes the extra arguments that need to be passed around for function caches when cloning new mappers.Depends on #531(merged).