Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

majosm
Copy link
Collaborator

@majosm majosm commented Sep 24, 2024

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 override rec, as well as minimizes the extra arguments that need to be passed around for function caches when cloning new mappers.

Depends on #531 (merged).

@majosm majosm force-pushed the add-cache-class branch 3 times, most recently from a34936a to 378d439 Compare September 24, 2024 18:02
This was referenced Sep 24, 2024
@majosm majosm force-pushed the add-cache-class branch 3 times, most recently from 1cf2454 to df7db39 Compare December 20, 2024 22:49
@majosm majosm force-pushed the add-cache-class branch 4 times, most recently from d40153c to e9b4ac5 Compare January 13, 2025 16:09
@majosm majosm force-pushed the add-cache-class branch 2 times, most recently from c25dce2 to 7a556ad Compare January 16, 2025 22:32
Copy link
Collaborator Author

@majosm majosm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@inducer This is probably good for a look too now (note that it includes the changes from #531). I pointed out a few things below that I'm still not sure about.

Comment on lines 321 to 325
# 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)
Copy link
Collaborator Author

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?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(see above?)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +331 to +333
# FIXME: Figure out the right way to type annotate these
| tuple[CacheExprT, tuple[Any, ...], dict[str, Any]],
Copy link
Collaborator Author

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?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 887 to 888
# FIXME: Can this just inherit from CachedMapper?
class CombineMapper(Mapper[ResultT, FunctionResultT, []]):
Copy link
Collaborator Author

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.

@majosm majosm marked this pull request as ready for review January 16, 2025 23:08
@majosm majosm requested a review from inducer January 16, 2025 23:08
@majosm majosm force-pushed the add-cache-class branch 2 times, most recently from a60b0f3 to 387bafb Compare January 21, 2025 15:43
:arg key_func: Function to compute a hashable cache key from an input
expression and any extra arguments.
"""
self._key_func = key_func
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self._key_func = key_func
self.get_key = key_func

Comment on lines 321 to 325
# 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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(see above?)

Comment on lines 321 to 325
# 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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +331 to +333
# FIXME: Figure out the right way to type annotate these
| tuple[CacheExprT, tuple[Any, ...], dict[str, Any]],
Copy link
Owner

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(
Copy link
Owner

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)

# 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,
Copy link
Owner

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

@@ -768,40 +884,53 @@ def map_named_call_result(self, expr: NamedCallResult,

# {{{ CombineMapper

# FIXME: Can this just inherit from CachedMapper?
Copy link
Owner

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.

@majosm majosm force-pushed the add-cache-class branch 2 times, most recently from 40b2a64 to 9cbe7f4 Compare January 24, 2025 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants