|
20 | 20 | # Imperical number that seems to work. |
21 | 21 | # We have to be able to match mpmath values with sympy values |
22 | 22 | COMPARE_PREC = 50 |
| 23 | +# Expressions that should not be cached |
| 24 | +NO_CACHE_EXPR = [ |
| 25 | + "System`Return", |
| 26 | + "System`Run", |
| 27 | + "System`GetEnvironment", |
| 28 | +] |
23 | 29 |
|
24 | 30 |
|
25 | 31 | def fully_qualified_symbol_name(name) -> bool: |
@@ -1301,6 +1307,21 @@ def evaluate(self, evaluation) -> typing.Union["Expression", "Symbol"]: |
1301 | 1307 |
|
1302 | 1308 | old_options = evaluation.options |
1303 | 1309 | evaluation.inc_recursion_depth() |
| 1310 | + |
| 1311 | + if self.get_head_name() in NO_CACHE_EXPR: |
| 1312 | + expr_hash = None |
| 1313 | + else: |
| 1314 | + expr_hash = self.hash() |
| 1315 | + |
| 1316 | + if expr_hash: |
| 1317 | + cache_expr_result = evaluation.cache_eval.get(expr_hash, None) |
| 1318 | + if cache_expr_result is not None: |
| 1319 | + expr = cache_expr_result[0] |
| 1320 | + if not expr.has_changed(definitions): |
| 1321 | + expr = cache_expr_result[1] |
| 1322 | + if not expr.has_changed(definitions): |
| 1323 | + return expr |
| 1324 | + |
1304 | 1325 | try: |
1305 | 1326 | while reevaluate: |
1306 | 1327 | # changed before last evaluated? |
@@ -1340,6 +1361,8 @@ def evaluate(self, evaluation) -> typing.Union["Expression", "Symbol"]: |
1340 | 1361 | evaluation.options = old_options |
1341 | 1362 | evaluation.dec_recursion_depth() |
1342 | 1363 |
|
| 1364 | + if expr_hash: |
| 1365 | + evaluation.cache_eval[expr_hash] = (self, expr) |
1343 | 1366 | return expr |
1344 | 1367 |
|
1345 | 1368 | def evaluate_next(self, evaluation) -> typing.Tuple["Expression", bool]: |
|
0 commit comments