From 9cd8ed658fbc4b8c50b40e1db770462526dfb806 Mon Sep 17 00:00:00 2001 From: Richard Kreckel Date: Sat, 7 Nov 2015 01:50:00 +0100 Subject: [PATCH] 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. --- ginac/add.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ginac/add.cpp b/ginac/add.cpp index fd83de11..a32ae803 100644 --- a/ginac/add.cpp +++ b/ginac/add.cpp @@ -538,6 +538,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); @@ -555,6 +557,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); @@ -562,13 +566,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); -- 2.44.0