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 all 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
15 changes: 12 additions & 3 deletions dask_expr/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def rewrite(self, kind: str):

return expr

def simplify_once(self, dependents: defaultdict):
def simplify_once(self, dependents: defaultdict, simplified: dict):
"""Simplify an expression

This leverages the ``._simplify_down`` and ``._simplify_up``
Expand All @@ -278,12 +278,18 @@ def simplify_once(self, dependents: defaultdict):

dependents: defaultdict[list]
The dependents for every node.
simplified: dict
Cache of simplified expressions for these dependents.

Returns
-------
expr:
output expression
"""
# Check if we've already simplified for these dependents
if self._name in simplified:
return simplified[self._name]

expr = self

while True:
Expand Down Expand Up @@ -314,7 +320,10 @@ def simplify_once(self, dependents: defaultdict):
if isinstance(operand, Expr):
# Bandaid for now, waiting for Singleton
dependents[operand._name].append(weakref.ref(expr))
new = operand.simplify_once(dependents=dependents)
new = operand.simplify_once(
dependents=dependents, simplified=simplified
)
simplified[operand._name] = new
if new._name != operand._name:
changed = True
else:
Expand All @@ -332,7 +341,7 @@ def simplify(self) -> Expr:
expr = self
while True:
dependents = collect_dependents(expr)
new = expr.simplify_once(dependents=dependents)
new = expr.simplify_once(dependents=dependents, simplified={})
if new._name == expr._name:
break
expr = new
Expand Down
4 changes: 1 addition & 3 deletions dask_expr/_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1742,9 +1742,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
Loading