Skip to content

Commit

Permalink
improved CSE (common subexpression elimination) dict c.ReduceFuncs
Browse files Browse the repository at this point in the history
  • Loading branch information
westandskif committed Sep 16, 2024
1 parent a65ec41 commit 7be600d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.14.3 (2024-09-04)

- improved CSE (common subexpression elimination) dict c.ReduceFuncs

## 1.14.2 (2024-09-04)

- improved CSE (common subexpression elimination) for `c.group_by` mode
Expand Down
2 changes: 1 addition & 1 deletion src/convtools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
from ._dt import DateGrid, DateTimeGrid


__version__ = "1.14.2"
__version__ = "1.14.3"
3 changes: 3 additions & 0 deletions src/convtools/_aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ast import Is as AstIs
from ast import Name as AstName
from ast import Pass as AstPass
from ast import Subscript as AstSubscript
from ast import parse as ast_parse
from collections import defaultdict
from decimal import Decimal
Expand Down Expand Up @@ -169,6 +170,8 @@ def no_side_effects_test(x):
x = x.left
elif isinstance(x, AstCall):
x = x.func
elif isinstance(x, AstSubscript):
x = x.value
if isinstance(x, AstAttribute):
x = x.value
return isinstance(x, AstName) and x.id.startswith("agg_data_")
Expand Down
18 changes: 16 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,12 @@ def test_optimizer():
_(f"agg_data_8 = {e}", 0)
_(f"agg_data_9 = {e}", 0)
_("assert agg_data_8 is not agg_data_9, 'check4'", 0)
e = expr("l + 5")
_("agg_data_10 = Obj({'a': 0})", 0)
_("agg_data_11 = Obj({'a': 0})", 0)
_(f"agg_data_10.v['a'] += {e}", 0)
_(f"agg_data_11.v['a'] += {e}", 0)

# fmt: on
_("", 0)
_("", 0)
Expand All @@ -528,6 +534,8 @@ def test_optimizer():
_("RESULTS.append(agg_data_3)", 0)
_("RESULTS.append(agg_data_4)", 0)
_("RESULTS.append(agg_data_5)", 0)
_("RESULTS.append(agg_data_10.v['a'])", 0)
_("RESULTS.append(agg_data_11.v['a'])", 0)
_("RESULTS.append(comp1)", 0)
_("RESULTS.append(comp2)", 0)
code_str = code.to_string(0)
Expand All @@ -542,10 +550,15 @@ def test_optimizer():
)
optimized_code_str = ast_unparse(optimizer.tree)
print(optimized_code_str)
ctx = {}

class Obj:
def __init__(self, v):
self.v = v

ctx = {"Obj": Obj}
exec(code_str, ctx, ctx)
results_of_non_optimized = ctx["RESULTS"]
ctx = {}
ctx = {"Obj": Obj}
exec(optimized_code_str, ctx, ctx)
results_of_optimized = ctx["RESULTS"]
assert results_of_non_optimized == results_of_optimized
Expand All @@ -559,3 +572,4 @@ def test_optimizer():
if PY_VERSION >= (3, 8):
assert optimized_code_str.count("v + 3") == 2
assert optimized_code_str.count("l + 4") == 4
assert optimized_code_str.count("l + 5") == 1

0 comments on commit 7be600d

Please sign in to comment.