From: Richard Kreckel Date: Sat, 7 Nov 2015 00:50:00 +0000 (+0100) Subject: Add trivial shortcuts in expair plumbing of class add. X-Git-Tag: ginac_1-6-6~8 X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=commitdiff_plain;h=bcd95dd63ac2499ee50a416c56ce200cd63521a8 Add trivial shortcuts in expair plumbing of class add. These little shortcuts in add::split_ex_to_pair(ex) and in add::combine_ex_with_coeff_to_pair(ex, ex) avoid the creation of new ex objects. These pieces of code are executed so often that this patch speeds up GiNaC by 5-10%, depending on test. --- diff --git a/ginac/add.cpp b/ginac/add.cpp index d66133eb..ccd49240 100644 --- a/ginac/add.cpp +++ b/ginac/add.cpp @@ -564,6 +564,8 @@ expair add::split_ex_to_pair(const ex & e) const if (is_exactly_a(e)) { const mul &mulref(ex_to(e)); const ex &numfactor = mulref.overall_coeff; + if (numfactor.is_equal(_ex1)) + return expair(e, _ex1); mul *mulcopyp = new mul(mulref); mulcopyp->overall_coeff = _ex1; mulcopyp->clearflag(status_flags::evaluated); @@ -581,6 +583,8 @@ expair add::combine_ex_with_coeff_to_pair(const ex & e, if (is_exactly_a(e)) { const mul &mulref(ex_to(e)); const ex &numfactor = mulref.overall_coeff; + if (numfactor.is_equal(_ex1)) + return expair(e, c); mul *mulcopyp = new mul(mulref); mulcopyp->overall_coeff = _ex1; mulcopyp->clearflag(status_flags::evaluated); @@ -588,13 +592,13 @@ expair add::combine_ex_with_coeff_to_pair(const ex & e, mulcopyp->setflag(status_flags::dynallocated); if (c.is_equal(_ex1)) return expair(*mulcopyp, numfactor); - else if (numfactor.is_equal(_ex1)) - return expair(*mulcopyp, c); else return expair(*mulcopyp, ex_to(numfactor).mul_dyn(ex_to(c))); } else if (is_exactly_a(e)) { if (c.is_equal(_ex1)) return expair(e, _ex1); + if (e.is_equal(_ex1)) + return expair(c, _ex1); return expair(ex_to(e).mul_dyn(ex_to(c)), _ex1); } return expair(e, c);