Skip to content

Commit c3cebc2

Browse files
committed
implementing cache at the level of the leaves
1 parent 37ad64d commit c3cebc2

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

mathics/builtin/lists.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2906,6 +2906,7 @@ class AppendTo(Builtin):
29062906

29072907
def apply(self, s, item, evaluation):
29082908
"AppendTo[s_, item_]"
2909+
evaluation.cache_result = False
29092910
resolved_s = s.evaluate(evaluation)
29102911
if s == resolved_s:
29112912
return evaluation.message("AppendTo", "rvalue", s)
@@ -3008,6 +3009,7 @@ class PrependTo(Builtin):
30083009

30093010
def apply(self, s, item, evaluation):
30103011
"PrependTo[s_, item_]"
3012+
evaluation.cache_result = False
30113013
resolved_s = s.evaluate(evaluation)
30123014
if s == resolved_s:
30133015
return evaluation.message("PrependTo", "rvalue", s)

mathics/core/expression.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,11 @@ def evaluate(self, evaluation) -> typing.Union["Expression", "Symbol"]:
13781378
def evaluate_next(self, evaluation) -> typing.Tuple["Expression", bool]:
13791379
from mathics.builtin.base import BoxConstruct
13801380

1381+
up_cache_result = evaluation.cache_result
1382+
cache_result = up_cache_result
13811383
head = self._head.evaluate(evaluation)
1384+
cache_result = cache_result and evaluation.cache_result
1385+
evaluation.cache_result = up_cache_result
13821386
attributes = head.get_attributes(evaluation.definitions)
13831387
leaves = self.get_mutable_leaves()
13841388

@@ -1389,7 +1393,11 @@ def rest_range(indices):
13891393
for index in indices:
13901394
leaf = leaves[index]
13911395
if leaf.has_form("Evaluate", 1):
1396+
up_cache_result = evaluation.cache_result
1397+
cache_result = up_cache_result
13921398
leaves[index] = leaf.evaluate(evaluation)
1399+
cache_result = cache_result and evaluation.cache_result
1400+
evaluation.cache_result = up_cache_result
13931401

13941402
def eval_range(indices):
13951403
for index in indices:
@@ -1405,11 +1413,18 @@ def eval_range(indices):
14051413
elif "System`HoldFirst" in attributes:
14061414
rest_range(range(0, min(1, len(leaves))))
14071415
eval_range(range(1, len(leaves)))
1416+
cache_result = cache_result and evaluation.cache_result
1417+
evaluation.cache_result = up_cache_result
14081418
elif "System`HoldRest" in attributes:
14091419
eval_range(range(0, min(1, len(leaves))))
1420+
cache_result = cache_result and evaluation.cache_result
1421+
evaluation.cache_result = up_cache_result
14101422
rest_range(range(1, len(leaves)))
14111423
else:
14121424
eval_range(range(len(leaves)))
1425+
cache_result = cache_result and evaluation.cache_result
1426+
evaluation.cache_result = up_cache_result
1427+
14131428
# rest_range(range(0, 0))
14141429

14151430
new = Expression(head)
@@ -1454,6 +1469,7 @@ def flatten_callback(new_leaves, old):
14541469
if "System`Listable" in attributes:
14551470
done, threaded = new.thread(evaluation)
14561471
if done:
1472+
evaluation.cache_result = cache_result
14571473
if threaded.sameQ(new):
14581474
new._timestamp_cache(evaluation)
14591475
return new, False
@@ -1481,6 +1497,7 @@ def rules():
14811497
for rule in rules():
14821498
result = rule.apply(new, evaluation, fully=False)
14831499
if result is not None:
1500+
evaluation.cache_result = cache_result
14841501
if isinstance(result, BoxConstruct):
14851502
return result, False
14861503
if result.sameQ(new):
@@ -1504,11 +1521,19 @@ def rules():
15041521

15051522
new.unformatted = self.unformatted
15061523
new._timestamp_cache(evaluation)
1524+
evaluation.cache_result = cache_result
15071525
return new, False
15081526

15091527
def evaluate_leaves(self, evaluation) -> "Expression":
1510-
leaves = [leaf.evaluate(evaluation) for leaf in self._leaves]
1528+
up_cache_result = evaluation.cache_result
1529+
cache_result = up_cache_result
1530+
leaves = []
1531+
for leaf in self._leaves:
1532+
leaves.append(leaf.evaluate(evaluation))
1533+
cache_result = cache_result and evaluation.cache_result
1534+
evaluation.cache_result = up_cache_result
15111535
head = self._head.evaluate_leaves(evaluation)
1536+
evaluation.cache_result = cache_result and evaluation.cache_result
15121537
return Expression(head, *leaves)
15131538

15141539
def __str__(self) -> str:

0 commit comments

Comments
 (0)