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

Add caching to recursive simplify_once calls #797

Merged
merged 9 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions dask_expr/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Expr:
_defaults = {}

def __init__(self, *args, **kwargs):
self._simplified = {}
operands = list(args)
for parameter in type(self)._parameters[len(operands) :]:
try:
Expand Down Expand Up @@ -279,6 +280,11 @@ def simplify_once(self, dependents: defaultdict):
expr:
output expression
"""
# Check if we've already simplified for these dependents
key = _tokenize_deterministic(sorted(dependents.keys()))
if key in self._simplified:
return self._simplified[key]
Copy link
Member

Choose a reason for hiding this comment

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

Does this change anything for you? I tried the same thing and it made things actually even slower

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting. Yes, it certainly speed things up a lot for me, but I only tried Q1.

Copy link
Member

Choose a reason for hiding this comment

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

yeah, sorry. I had a bug in my cache :)


expr = self

while True:
Expand Down Expand Up @@ -319,6 +325,7 @@ def simplify_once(self, dependents: defaultdict):

break

self._simplified[key] = expr # Cache the result
return expr

def simplify(self) -> Expr:
Expand Down
4 changes: 1 addition & 3 deletions dask_expr/_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1768,9 +1768,7 @@ def vals(self):

@functools.cached_property
def _meta(self):
args = [
meta_nonempty(op._meta) if isinstance(op, Expr) else op for op in self._args
]
args = [op._meta if isinstance(op, Expr) else op for op in self._args]
return make_meta(self.operation(*args, **self._kwargs))

def _tree_repr_argument_construction(self, i, op, header):
Expand Down
1 change: 1 addition & 0 deletions dask_expr/io/_delayed.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class _DelayedExpr(Expr):
# TODO

def __init__(self, obj):
self._simplified = {}
rjzamora marked this conversation as resolved.
Show resolved Hide resolved
self.obj = obj
self.operands = [obj]

Expand Down
Loading