Evaluator/Optimizer.pm

filter: Statementstmt 100% 54 / 54 Branchbran 100% 20 / 20 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 100% 89 / 89 CC 16 SCAR 20.8
CC
16
Cov
100%
_partial_execute
11.0
_flatten
6.0
BEGIN
1.0
CRAP 8.0
↑ Summary Evaluator/Lexer.pm »
1 package Math::Expression::Evaluator::Optimizer;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Math::Expression::Evaluator::Optimizer -
8 Optimize Math::Expression::Evaluator ASTs
9  
10 =head1 SYNOPSIS
11  
12 use Math::Expression::Evaluator;
13 my $m = Math::Expression::Evaluator->new("2 + 4*f");
14 $m->optimize();
15 for (0..100){
16 print $m->val({f => $_}), "\n";
17 }
18  
19 =head1 DESCRIPTION
20  
21 Math::Expression::Evaluator::Optimizer performs simple optimizations on the
22 abstract syntax tree from Math::Expression::Evaluator.
23  
24 You should not use this module directly, but interface it via
25 L<Math::Expression::Evaluator>.
26  
27 The following optimizations are implemented:
28  
29 =over
30  
31 =item *
32  
33 Constant sub expressions: C<variable + 3 * 4> is simplfied to
34 C<variable + 12>.
35  
36 =item *
37  
38 Joining of constants in mixed constant/variable expressions: C<2 + var + 3>
39 is simplified to C<var + 5>. Works only with sums and products (but internally
40 a C<2 - 3 + x> is represented as C<2 + (-3) + x>, so it actually works with
41 differences and divisions as well).
42  
43 =item *
44  
45 Flattening of nested sub expression: C<a * (3 * b)> is flattened into
46 C<a * 3 * b>. Currently this is done before any other optimization and not
47 repeated.
48  
49 =back
50  
51 =head1 PERFORMANCE CONSIDERATIONS
52  
53 C<optimize()> currently takes two full loops through the AST, copying and
54 recreating it. If you execute C<val()> only once, calling C<optimize()>
55 is in fact a performance loss.
56  
57 If the expression is optimizable, and you execute it C<$n> times, you
58 usually have a net gain over unoptimized execution if C<< $n > 15 >>.
59  
60 Of course that value depends on the complexity of the expression, and how
61 well it can be reduced by the implemented optimizations.
62  
63 Your best is to always benchmark what you do. Most of the time the compiled
64 version returned by C<< ->compiled >> is much faster than the optimized
65 (and not compiled) form.
66 =cut
67  
68 8 my %is_commutative = (
69 '+' => 1,
70 '*' => 1,
71 );
72  
73 373 ▶sub _optimize {
Subroutine _optimize: called CC=1 SCAR=0.0
74 373 my ($expr, $ast) = @_;
75 373 return _partial_execute($expr, _flatten($ast));
76 }
77  
78 # Note: if you ever want to introduce some kind of scoping that is
79 # tied to blocks, remove the '{' here.
80 8 my %flattable = map { $_ => 1 } ('{', '+', '*');
81  
82 890 ▶sub _flatten {
Subroutine _flatten: called CC=6 SCAR=17.9
83 890 my ($ast) = @_;
84 890 ▶ return $ast unless ref $ast;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ast 568 322 890
85  
86 322 my $type = shift @$ast;
87 322 my @new_nodes = ($type);
88 322 ▶ if ($flattable{$type}){
Branch: 1 branch
BranchTrueFalseTotal
if ($flattable{$type}) { } 144 178 322
89 # interpolate AST nodes with the same type
90 # e.g. ['+', 2, ['+', 3, 4]] into ['+', 2, 3, 4]
91 144 for (map { _flatten($_) } @$ast){
92 291 ▶ if (ref $_ and $_->[0] eq $type){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ and $_->[0] eq $type) { } 12 279 291
Condition100%3/3
ref $_ and $_->[0] eq $type
!ll&&!rl&&r
1839612
MC/DC100%2/2
ref $_ and $_->[0] eq $type
ref $_ $_->[0] eq $type
Truth table100%3/3
A and B
A= ref $_B= $_->[0] eq $type
ABresult
0X0
100
111
93 12 my @inner_nodes = @$_;
94 12 shift @inner_nodes;
95 12 push @new_nodes, @inner_nodes;
96 } else {
97 279 push @new_nodes, $_;
98 }
99 }
100 } else {
101 178 push @new_nodes, map { _flatten($_) } @$ast;
102 }
103 322 return \@new_nodes;
104 }
105  
106  
107 # _parital_execute reduces constant subexpressions to a minimal form.
108 373 ▶sub _partial_execute {
Subroutine _partial_execute: called CC=11 SCAR=24.0
109 373 my ($expr, $ast) = @_;
110 373 ▶ if (ref $ast){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ast) { } 178 195 373
111 178 my @nodes = @$ast;
112 178 my $type = shift @nodes;
113 178 ▶ if ($type eq '=' || $type eq '$'){
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "=" or $type eq "\$") 34 144 178
Condition100%3/3
$type eq "=" or $type eq "\$"
l!l&&r!l&&!r
133144
MC/DC100%2/2
$type eq "=" or $type eq "\$"
$type eq "=" $type eq "\$"
Truth table100%3/3
A or B
A= $type eq "="B= $type eq "\$"
ABresult
1X1
011
000
114 # XXX what to do about assignments? more thoughts needed
115 34 return $ast;
116 }
117 144 my @new_nodes = ($type);
118 144 my $tainted = 0;
119 144 for my $n (@nodes){
120 289 push @new_nodes, _optimize($expr, $n);
121 289 ▶ if (ref $new_nodes[-1]){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new_nodes[-1]) 36 253 289
122 36 $tainted = 1;
123 }
124 }
125 144 ▶ if ($tainted){
Branch: 1 branch
BranchTrueFalseTotal
if ($tainted) { } 29 115 144
126 # try to optimize things like '2 + a +3' into 'a + 5'
127 # is only allowed for commutative ops
128 29 ▶ if ($is_commutative{$type}){
Branch: 1 branch
BranchTrueFalseTotal
if ($is_commutative{$type}) { } 27 2 29
129 # print STDERR "Trying commutative optimization\n";
130 27 my @untainted = ($type);
131 27 my @tainted = ($type);
132 27 for (1..$#new_nodes) {
133 80 ▶ if (ref $new_nodes[$_]){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new_nodes[$_]) { } 33 47 80
134 33 push @tainted, $new_nodes[$_];
135 } else {
136 47 push @untainted, $new_nodes[$_];
137 }
138 }
139  
140 27 ▶ if (@untainted > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@untainted > 2) { } 17 10 27
141 # there is something to optimize
142 17 push @tainted, $expr->_execute(\@untainted);
143 17 return \@tainted;
144 } else {
145 # 'twas all in vain
146 10 return \@new_nodes;
147 }
148 } else {
149 2 return \@new_nodes;
150 }
151 } else {
152 115 return $expr->_execute(\@new_nodes);
153 }
154 } else {
155 195 return $ast;
156 }
157  
158 }
159  
160 8 1;
161  
162 # vim: sw=4 ts=4 expandtab
↑ Summary Evaluator/Lexer.pm »