Feature #2561

1.8.7 Patch reduces time cost of Rational operations by 50%.

Added by Kurt Stephens about 2 years ago. Updated 9 months ago.

[ruby-core:27437]
Status:Closed Start date:01/06/2010
Priority:Normal Due date:
Assignee:Akinori MUSHA % Done:

100%

Category:-
Target version:-

Description

This changes adds a specialize Fixnum#gcd and a tuned rational.rb.  
Reduced overall time on Rational operations by > 50%.

<pre>
                                              user     system      total        real
test_it                                  22.380000   2.140000  24.520000 ( 24.559388)
test_it ks_rational                      17.870000   1.830000  19.700000 ( 19.687221)
test_it ks_rational + Fixnum#gcd         10.660000   0.000000  10.660000 ( 10.665765)
</pre>

The patch will perform better with Fixnum#gcd on numeric.c but still is faster with only the rational.rb changes.

I have a version for 1.8.6 if someone wants it.

rational_performance.rb (1.9 kB) Kurt Stephens, 01/06/2010 10:58 am

ruby-1.8.7-rational.patch (9.1 kB) Kurt Stephens, 01/06/2010 10:58 am

rational_performance.rb (2.3 kB) Kurt Stephens, 01/07/2010 03:43 am

ruby-1.8.7-rational.patch (9.4 kB) Kurt Stephens, 01/07/2010 03:43 am

ruby-1.8.7-rational.patch - Fixed Fixnum#gcd for FIXNUM_MIN. (10.7 kB) Kurt Stephens, 01/12/2010 01:18 am

gcd_test.rb (21.6 kB) Kurt Stephens, 01/12/2010 01:18 am

gcd_test.rb - 64-bit test cases. (44.6 kB) Kurt Stephens, 01/22/2010 05:58 am

ruby-1.8.7-fixnum_gcd.patch - numeric.c patch. (1.3 kB) Kurt Stephens, 01/22/2010 05:58 am


Related issues

duplicated by Ruby 1.8 - Backport #838: faster gcd for 187 Closed 12/08/2008

Associated revisions

Revision 26581
Added by knu almost 2 years ago

* ext/rational/rational.c: Added to provide a fast implementation of Fixnum#gcd (and maybe some others in the future) in C. The base code was submitted by Kurt Stephens. [Feature #2561] * ext/rational/lib/rational.rb: Moved from lib/rational.rb. Make overall code optimization; submitted by Kurt Stephens. [Feature #2561] * test/rational/test_rational.rb, test/rational/test_rational2.rb: Add tests for Rational, ported from trunk. * test/rational/test_fixnum_gcd.rb: Add a test for Integer#gcd. Case values are only provided for i386 and amd64 at the moment; submitted by Kurt Stephens. [Feature #2561]

History

Updated by Yui NARUSE about 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to Akinori MUSHA
Discuss in Ruby 1.8 first,

Updated by Kurt Stephens about 2 years ago

New version:

Avoid additional method calls to Float(), call #to_f directly. Avoid Rational() when its clear that denominator will never be 1.

Updated by Kurt Stephens about 2 years ago

Fixed Fixnum#gcd for FIXNUM_MIN.

Ref: http://github.com/kstephens/rubyenterpriseedition187/commit/877d5e4c7892248bb7486f24c076cb23d565a466

gcd_test.rb should pass on 32-bit builds.

Updated by Kurt Stephens about 2 years ago

Fixed Fixnum#gcd to FIXNUM_MIN on 64-bit longs.

Ref: http://github.com/kstephens/rubyenterpriseedition187-248/commit/4863695e774d427d95c0828f8d2d5a92a433a1c4

gcd_test.rb should pass on 64-bit builds.

Updated by Akinori MUSHA about 2 years ago

Patches look fine to me.  I'll handle this as soon as I can take the time, hopefully within a couple weeks.

I think it's better to have the C part as extension rather than putting Fixnum#gcd solely in the core,
so I'll make some layout changes to bring this in in the best shape for future maintainability.

Updated by Akinori MUSHA almost 2 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100
This issue was solved with changeset r26581.
Kurt , thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

Also available in: Atom PDF