Skip to content

Commit

Permalink
make memoization work for containers
Browse files Browse the repository at this point in the history
  • Loading branch information
smacke committed Nov 22, 2023
1 parent 3e5c928 commit 41e5812
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
28 changes: 24 additions & 4 deletions core/ipyflow/data_model/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,12 +1203,32 @@ def refresh(
if refresh_namespace_waiting:
self.namespace_waiting_symbols.clear()

def make_memoize_comparable(self):
if isinstance(self.obj, (int, str)):
return self.obj
_MAX_MEMOIZE_COMPARABLE_SIZE = 10**6

@classmethod
def make_memoize_comparable_for_obj(cls, obj: Any) -> Tuple[Any, int]:
if isinstance(obj, (int, str)):
return obj, 1
elif isinstance(obj, (dict, frozenset, list, set, tuple)):
size = 0
comparable = []
for inner in obj:
inner_comp, inner_size = cls.make_memoize_comparable_for_obj(inner)
if inner_comp is cls.NULL:
return cls.NULL, -1
size += inner_size + 1
if size > cls._MAX_MEMOIZE_COMPARABLE_SIZE:
return cls.NULL, -1
comparable.append(inner_comp)
return type(obj)(comparable), size
else:
return cls.NULL, -1

def make_memoize_comparable(self) -> Any:
if isinstance(self.stmt_node, ast.FunctionDef):
return astunparse.unparse(self.stmt_node)
return self.NULL
else:
return self.make_memoize_comparable_for_obj(self.obj)[0]


if len(_SymbolContainer) == 0:
Expand Down
1 change: 0 additions & 1 deletion core/test/test_memoization.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def test_strings():
assert flow().global_scope["y"].obj == "hello world"


@skipif_known_failing
def test_sets():
first = cells(run_cell("x = {0}", cell_id="first"))
second = cells(run_cell("%%memoize\ny = x | {1}", cell_id="second"))
Expand Down

0 comments on commit 41e5812

Please sign in to comment.