Skip to content

Commit 74ae499

Browse files
committed
cache_expr to Definitions
1 parent c3cebc2 commit 74ae499

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

mathics/builtin/datentime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ class Timing(Builtin):
211211

212212
def apply(self, expr, evaluation):
213213
"Timing[expr_]"
214-
evaluation.cache_result = False
215214
start = time.process_time()
216215
result = expr.evaluate(evaluation)
216+
evaluation.cache_result = False
217217
stop = time.process_time()
218218
return Expression("List", Real(stop - start), result)
219219

mathics/builtin/system.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,18 +626,18 @@ class ClearSystemCache(Builtin):
626626
def apply_clear(self, evaluation):
627627
"ClearSystemCache[]"
628628
evaluation.cache_result = False
629-
evaluation.cache_expr = {}
629+
evaluation.definitions.cache_eval = {}
630630
return
631631

632632
def apply_clear_symbolic(self, evaluation):
633633
'ClearSystemCache["Symbolic"]'
634634
evaluation.cache_result = False
635-
evaluation.cache_expr = {}
635+
evaluation.definitions.cache_eval = {}
636636
return
637637

638638
def apply_clear_numeric(self, evaluation):
639639
'ClearSystemCache["Numeric"]'
640640
evaluation.cache_result = False
641-
evaluation.cache_expr = {}
641+
evaluation.definitions.cache_eval = {}
642642
return
643643

mathics/core/definitions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def __init__(
8484
self.proxy = defaultdict(set)
8585
self.now = 0 # increments whenever something is updated
8686
self._packages = []
87+
self.cache_eval = {}
8788

8889
if add_builtin:
8990
from mathics.builtin import modules, contribute

mathics/core/evaluation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ def __init__(
238238
) -> None:
239239
from mathics.core.definitions import Definitions
240240
from mathics.core.expression import Symbol
241-
242241
if definitions is None:
243242
definitions = Definitions()
244243
self.definitions = definitions
@@ -334,11 +333,13 @@ def evaluate():
334333
self.last_eval = Expression("System`$Pre", query).evaluate(self)
335334
else:
336335
self.last_eval = query.evaluate(self)
337-
338336
if check_io_hook("System`$Post"):
339337
self.last_eval = Expression("System`$Post", self.last_eval).evaluate(
340338
self
341339
)
340+
# From there, it is all about format. Do not catch evaluations.
341+
self.cache_result = False
342+
342343
if history_length > 0:
343344
if self.predetermined_out is not None:
344345
out_result = self.predetermined_out

mathics/core/expression.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,12 +1319,12 @@ def evaluate(self, evaluation) -> typing.Union["Expression", "Symbol"]:
13191319
# of a branch. Once it is set to false, the result is not cached,
13201320
# and hence, not used.
13211321
if evaluation.cache_result:
1322-
expr_hash = self.__hash__()
1322+
expr_hash = str(self.__hash__())
13231323
else:
13241324
expr_hash = None
13251325

13261326
if expr_hash:
1327-
cache_expr_result = evaluation.cache_eval.get(expr_hash, None)
1327+
cache_expr_result = evaluation.definitions.cache_eval.get(expr_hash, None)
13281328
if cache_expr_result is not None:
13291329
expr = cache_expr_result[0]
13301330
if not expr.has_changed(definitions):
@@ -1342,7 +1342,6 @@ def evaluate(self, evaluation) -> typing.Union["Expression", "Symbol"]:
13421342

13431343
if hasattr(expr, "options") and expr.options:
13441344
evaluation.options = expr.options
1345-
13461345
expr, reevaluate = expr.evaluate_next(evaluation)
13471346
if not reevaluate:
13481347
break
@@ -1372,7 +1371,8 @@ def evaluate(self, evaluation) -> typing.Union["Expression", "Symbol"]:
13721371
evaluation.dec_recursion_depth()
13731372

13741373
if evaluation.cache_result:
1375-
evaluation.cache_eval[expr_hash] = (self, expr)
1374+
self._timestamp_cache(evaluation)
1375+
evaluation.definitions.cache_eval[expr_hash] = (self, expr)
13761376
return expr
13771377

13781378
def evaluate_next(self, evaluation) -> typing.Tuple["Expression", bool]:
@@ -1426,7 +1426,6 @@ def eval_range(indices):
14261426
evaluation.cache_result = up_cache_result
14271427

14281428
# rest_range(range(0, 0))
1429-
14301429
new = Expression(head)
14311430
new._leaves = tuple(leaves)
14321431

@@ -1497,7 +1496,7 @@ def rules():
14971496
for rule in rules():
14981497
result = rule.apply(new, evaluation, fully=False)
14991498
if result is not None:
1500-
evaluation.cache_result = cache_result
1499+
evaluation.cache_result = cache_result and evaluation.cache_result
15011500
if isinstance(result, BoxConstruct):
15021501
return result, False
15031502
if result.sameQ(new):
@@ -1522,6 +1521,7 @@ def rules():
15221521
new.unformatted = self.unformatted
15231522
new._timestamp_cache(evaluation)
15241523
evaluation.cache_result = cache_result
1524+
15251525
return new, False
15261526

15271527
def evaluate_leaves(self, evaluation) -> "Expression":
@@ -1531,7 +1531,7 @@ def evaluate_leaves(self, evaluation) -> "Expression":
15311531
for leaf in self._leaves:
15321532
leaves.append(leaf.evaluate(evaluation))
15331533
cache_result = cache_result and evaluation.cache_result
1534-
evaluation.cache_result = up_cache_result
1534+
evaluation.cache_result = up_cache_result
15351535
head = self._head.evaluate_leaves(evaluation)
15361536
evaluation.cache_result = cache_result and evaluation.cache_result
15371537
return Expression(head, *leaves)

0 commit comments

Comments
 (0)