Add some minor optimizations.
authorRichard Kreckel <kreckel@ginac.de>
Thu, 5 Nov 2015 12:41:18 +0000 (13:41 +0100)
committerRichard Kreckel <kreckel@ginac.de>
Thu, 5 Nov 2015 12:41:18 +0000 (13:41 +0100)
ginac/add.cpp
ginac/expairseq.cpp
ginac/mul.cpp
ginac/power.cpp

index 74c8d81..fd83de1 100644 (file)
@@ -331,7 +331,7 @@ ex add::coeff(const ex & s, int n) const
 ex add::eval(int level) const
 {
        epvector evaled = evalchildren(level);
-       if (!evaled.empty()) {
+       if (unlikely(!evaled.empty())) {
                // do more evaluation later
                return (new add(std::move(evaled), overall_coeff))->
                        setflag(status_flags::dynallocated);
index 67099f5..0e67ec9 100644 (file)
@@ -32,6 +32,7 @@
 #include "utils.h"
 #include "hash_seed.h"
 #include "indexed.h"
+#include "compiler.h"
 
 #include <algorithm>
 #include <iostream>
@@ -1053,7 +1054,7 @@ epvector expairseq::expandchildren(unsigned options) const
  *    had to be changed. */
 epvector expairseq::evalchildren(int level) const
 {
-       if (level==1)
+       if (likely(level==1))
                return epvector();  // nothing had to be evaluated
        
        if (level == -max_recursion_level)
index 943095b..f8ddd9b 100644 (file)
@@ -469,7 +469,7 @@ ex mul::coeff(const ex & s, int n) const
 ex mul::eval(int level) const
 {
        epvector evaled = evalchildren(level);
-       if (!evaled.empty()) {
+       if (unlikely(!evaled.empty())) {
                // do more evaluation later
                return (new mul(std::move(evaled), overall_coeff))->
                        setflag(status_flags::dynallocated);
index b2622d6..485fa06 100644 (file)
@@ -1207,7 +1207,6 @@ ex power::expand_add(const add & a, int n, unsigned options) const
                                numeric factor = coeff;
                                for (unsigned i = 0; i < exponent.size(); ++i) {
                                        const ex & r = a.seq[i].rest;
-                                       const ex & c = a.seq[i].coeff;
                                        GINAC_ASSERT(!is_exactly_a<add>(r));
                                        GINAC_ASSERT(!is_exactly_a<power>(r) ||
                                                     !is_exactly_a<numeric>(ex_to<power>(r).exponent) ||
@@ -1215,15 +1214,19 @@ ex power::expand_add(const add & a, int n, unsigned options) const
                                                     !is_exactly_a<add>(ex_to<power>(r).basis) ||
                                                     !is_exactly_a<mul>(ex_to<power>(r).basis) ||
                                                     !is_exactly_a<power>(ex_to<power>(r).basis));
+                                       GINAC_ASSERT(is_exactly_a<numeric>(a.seq[i].coeff));
+                                       const numeric & c = ex_to<numeric>(a.seq[i].coeff);
                                        if (exponent[i] == 0) {
                                                // optimize away
                                        } else if (exponent[i] == 1) {
                                                // optimized
                                                term.push_back(r);
-                                               factor = factor.mul(ex_to<numeric>(c));
+                                               if (c != *_num1_p)
+                                                       factor = factor.mul(c);
                                        } else { // general case exponent[i] > 1
                                                term.push_back((new power(r, exponent[i]))->setflag(status_flags::dynallocated));
-                                               factor = factor.mul(ex_to<numeric>(c).power(exponent[i]));
+                                               if (c != *_num1_p)
+                                                       factor = factor.mul(c.power(exponent[i]));
                                        }
                                }
                                result.push_back(a.combine_ex_with_coeff_to_pair(mul(term).expand(options), factor));