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 15:29:53 +0000 (16:29 +0100)
ginac/add.cpp
ginac/expairseq.cpp
ginac/mul.cpp
ginac/power.cpp

index 9110491e3246a7fc750f96fce18bdb3895e7fc24..d66133ebdbfc4eacd83ee8365e94cdad9fe1a686 100644 (file)
@@ -344,7 +344,7 @@ ex add::coeff(const ex & s, int n) const
 ex add::eval(int level) const
 {
        std::auto_ptr<epvector> evaled_seqp = evalchildren(level);
-       if (evaled_seqp.get()) {
+       if (unlikely(evaled_seqp.get() != 0)) {
                // do more evaluation later
                return (new add(evaled_seqp, overall_coeff))->
                       setflag(status_flags::dynallocated);
index 9ef8afa9910e081f53bef2f04903c6e12e7235e1..b29f3802c13f2be0f0a55977d6b129c00dd2cdad 100644 (file)
@@ -32,6 +32,7 @@
 #include "utils.h"
 #include "hash_seed.h"
 #include "indexed.h"
+#include "compiler.h"
 
 #include <algorithm>
 #if EXPAIRSEQ_USE_HASHTAB
@@ -1610,7 +1611,7 @@ std::auto_ptr<epvector> expairseq::evalchildren(int level) const
        // returns a pointer to a newly created epvector otherwise
        // (which has to be deleted somewhere else)
 
-       if (level==1)
+       if (likely(level==1))
                return std::auto_ptr<epvector>(0);
        
        if (level == -max_recursion_level)
index 2b404877c9840aa53a071e712aa222e51680533a..636c939fcd08762a126c67543ae06df02071b1de 100644 (file)
@@ -497,7 +497,7 @@ ex mul::coeff(const ex & s, int n) const
 ex mul::eval(int level) const
 {
        std::auto_ptr<epvector> evaled_seqp = evalchildren(level);
-       if (evaled_seqp.get()) {
+       if (unlikely(evaled_seqp.get() != 0)) {
                // do more evaluation later
                return (new mul(evaled_seqp, overall_coeff))->
                           setflag(status_flags::dynallocated);
index bf72d97320b667d1bcee62cf6572adee24845678..28e5bd3a82acbef059c258882d22d44616319ede 100644 (file)
@@ -1215,7 +1215,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) ||
@@ -1223,15 +1222,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));