ruby-changes:30760
From: akr <ko1@a...>
Date: Thu, 5 Sep 2013 06:23:27 +0900 (JST)
Subject: [ruby-changes:30760] akr:r42839 (trunk): * bignum.c (bary_divmod_normal): Reduce temporary array allocations.
akr 2013-09-05 06:23:21 +0900 (Thu, 05 Sep 2013) New Revision: 42839 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42839 Log: * bignum.c (bary_divmod_normal): Reduce temporary array allocations. Modified files: trunk/ChangeLog trunk/bignum.c Index: ChangeLog =================================================================== --- ChangeLog (revision 42838) +++ ChangeLog (revision 42839) @@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Sep 5 06:22:42 2013 Tanaka Akira <akr@f...> + + * bignum.c (bary_divmod_normal): Reduce temporary array allocations. + Thu Sep 5 02:17:06 2013 Tanaka Akira <akr@f...> * bignum.c (rb_big_divrem_normal): Add GC guards. Index: bignum.c =================================================================== --- bignum.c (revision 42838) +++ bignum.c (revision 42839) @@ -2641,8 +2641,7 @@ bary_divmod_normal(BDIGIT *qds, size_t q https://github.com/ruby/ruby/blob/trunk/bignum.c#L2641 int shift; BDIGIT *zds, *yyds; size_t zn; - VALUE tmpz = 0; - VALUE tmpyy = 0; + VALUE tmpyz = 0; assert(yn < xn || (xn == yn && yds[yn - 1] <= xds[xn - 1])); assert(qds ? (xn - yn + 1) <= qn : 1); @@ -2652,14 +2651,16 @@ bary_divmod_normal(BDIGIT *qds, size_t q https://github.com/ruby/ruby/blob/trunk/bignum.c#L2651 shift = nlz(yds[yn-1]); if (shift) { - if (qds && zn <= qn) - zds = qds; - else - zds = ALLOCV_N(BDIGIT, tmpz, zn); - if (rds) - yyds = rds; - else - yyds = ALLOCV_N(BDIGIT, tmpyy, yn); + int alloc_y = !rds; + int alloc_z = !qds || qn < zn; + if (alloc_y && alloc_z) { + yyds = ALLOCV_N(BDIGIT, tmpyz, yn+zn); + zds = yyds + yn; + } + else { + yyds = alloc_y ? ALLOCV_N(BDIGIT, tmpyz, yn) : rds; + zds = alloc_z ? ALLOCV_N(BDIGIT, tmpyz, zn) : qds; + } zds[xn] = bary_small_lshift(zds, xds, xn, shift); bary_small_lshift(yyds, yds, yn, shift); } @@ -2667,7 +2668,7 @@ bary_divmod_normal(BDIGIT *qds, size_t q https://github.com/ruby/ruby/blob/trunk/bignum.c#L2668 if (qds && zn <= qn) zds = qds; else - zds = ALLOCV_N(BDIGIT, tmpz, zn); + zds = ALLOCV_N(BDIGIT, tmpyz, zn); MEMCPY(zds, xds, BDIGIT, xn); zds[xn] = 0; /* bigdivrem_restoring will not modify y. @@ -2691,10 +2692,8 @@ bary_divmod_normal(BDIGIT *qds, size_t q https://github.com/ruby/ruby/blob/trunk/bignum.c#L2692 BDIGITS_ZERO(qds+j, qn-j); } - if (tmpyy) - ALLOCV_END(tmpyy); - if (tmpz) - ALLOCV_END(tmpz); + if (tmpyz) + ALLOCV_END(tmpyz); } VALUE -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/