ruby-changes:74274
From: S.H <ko1@a...>
Date: Fri, 28 Oct 2022 01:13:42 +0900 (JST)
Subject: [ruby-changes:74274] c6f439a6a8 (master): Improve performance some `Integer` and `Float` methods [Feature #19085] (#6638)
https://git.ruby-lang.org/ruby.git/commit/?id=c6f439a6a8 From c6f439a6a8df582416e756d7511aa4d9c72071a9 Mon Sep 17 00:00:00 2001 From: "S.H" <gamelinks007@g...> Date: Fri, 28 Oct 2022 01:13:16 +0900 Subject: Improve performance some `Integer` and `Float` methods [Feature #19085] (#6638) * Improve some Integer and Float methods * Using alias and Remove unnecessary code * Remove commentout code --- benchmark/numeric_methods.yml | 16 ++++++++++ complex.c | 43 ------------------------- numeric.rb | 74 +++++++++++++++++++++++++++++++++++++------ rational.c | 27 ---------------- 4 files changed, 81 insertions(+), 79 deletions(-) diff --git a/benchmark/numeric_methods.yml b/benchmark/numeric_methods.yml index 433c2268a3..1384902935 100644 --- a/benchmark/numeric_methods.yml +++ b/benchmark/numeric_methods.yml @@ -10,4 +10,20 @@ benchmark: https://github.com/ruby/ruby/blob/trunk/benchmark/numeric_methods.yml#L10 int.finite? infinite?: | int.infinite? + integer_real: | + int.real + float_real: | + flo.real + integr_imag: | + int.imag + float_imag: | + flo.imag + integer_conj: | + int.conj + float_conj: | + flo.conj + integer_numerator: | + int.numerator + integer_denominator: | + int.denominator loop_count: 20000000 diff --git a/complex.c b/complex.c index db114cd914..d927f62d55 100644 --- a/complex.c +++ b/complex.c @@ -2159,31 +2159,6 @@ nucomp_s_convert(int argc, VALUE *argv, VALUE klass) https://github.com/ruby/ruby/blob/trunk/complex.c#L2159 return nucomp_convert(klass, a1, a2, TRUE); } -/* - * call-seq: - * num.real -> self - * - * Returns self. - */ -static VALUE -numeric_real(VALUE self) -{ - return self; -} - -/* - * call-seq: - * num.imag -> 0 - * num.imaginary -> 0 - * - * Returns zero. - */ -static VALUE -numeric_imag(VALUE self) -{ - return INT2FIX(0); -} - /* * call-seq: * num.abs2 -> real @@ -2255,19 +2230,6 @@ numeric_polar(VALUE self) https://github.com/ruby/ruby/blob/trunk/complex.c#L2230 return rb_assoc_new(abs, arg); } -/* - * call-seq: - * num.conj -> self - * num.conjugate -> self - * - * Returns self. - */ -static VALUE -numeric_conj(VALUE self) -{ - return self; -} - /* * call-seq: * flo.arg -> 0 or float @@ -2433,9 +2395,6 @@ Init_Complex(void) https://github.com/ruby/ruby/blob/trunk/complex.c#L2395 rb_define_private_method(CLASS_OF(rb_cComplex), "convert", nucomp_s_convert, -1); - rb_define_method(rb_cNumeric, "real", numeric_real, 0); - rb_define_method(rb_cNumeric, "imaginary", numeric_imag, 0); - rb_define_method(rb_cNumeric, "imag", numeric_imag, 0); rb_define_method(rb_cNumeric, "abs2", numeric_abs2, 0); rb_define_method(rb_cNumeric, "arg", numeric_arg, 0); rb_define_method(rb_cNumeric, "angle", numeric_arg, 0); @@ -2443,8 +2402,6 @@ Init_Complex(void) https://github.com/ruby/ruby/blob/trunk/complex.c#L2402 rb_define_method(rb_cNumeric, "rectangular", numeric_rect, 0); rb_define_method(rb_cNumeric, "rect", numeric_rect, 0); rb_define_method(rb_cNumeric, "polar", numeric_polar, 0); - rb_define_method(rb_cNumeric, "conjugate", numeric_conj, 0); - rb_define_method(rb_cNumeric, "conj", numeric_conj, 0); rb_define_method(rb_cFloat, "arg", float_arg, 0); rb_define_method(rb_cFloat, "angle", float_arg, 0); diff --git a/numeric.rb b/numeric.rb index c2091465f8..f026679210 100644 --- a/numeric.rb +++ b/numeric.rb @@ -6,7 +6,17 @@ class Numeric https://github.com/ruby/ruby/blob/trunk/numeric.rb#L6 # Returns +true+ if +num+ is a real number (i.e. not Complex). # def real? - return true + true + end + + # + # call-seq: + # num.real -> self + # + # Returns self. + # + def real + self end # @@ -19,7 +29,7 @@ class Numeric https://github.com/ruby/ruby/blob/trunk/numeric.rb#L29 # 1.integer? #=> true # def integer? - return false + false end # @@ -29,7 +39,7 @@ class Numeric https://github.com/ruby/ruby/blob/trunk/numeric.rb#L39 # Returns +true+ if +num+ is a finite number, otherwise returns +false+. # def finite? - return true + true end # @@ -40,8 +50,34 @@ class Numeric https://github.com/ruby/ruby/blob/trunk/numeric.rb#L50 # finite, <code>-Infinity</code>, or <code>+Infinity</code>. # def infinite? - return nil + nil + end + + # + # call-seq: + # num.imag -> 0 + # num.imaginary -> 0 + # + # Returns zero. + # + def imaginary + 0 + end + + alias imag imaginary + + # + # call-seq: + # num.conj -> self + # num.conjugate -> self + # + # Returns self. + # + def conjugate + self end + + alias conj conjugate end class Integer @@ -146,7 +182,7 @@ class Integer https://github.com/ruby/ruby/blob/trunk/numeric.rb#L182 # # Since +int+ is already an Integer, this always returns +true+. def integer? - return true + true end alias magnitude abs @@ -178,7 +214,7 @@ class Integer https://github.com/ruby/ruby/blob/trunk/numeric.rb#L214 # # For example, <code>?a.ord</code> returns 97 both in 1.8 and 1.9. def ord - return self + self end # @@ -208,7 +244,7 @@ class Integer https://github.com/ruby/ruby/blob/trunk/numeric.rb#L244 # # #to_int is an alias for #to_i. def to_i - return self + self end # call-seq: @@ -216,7 +252,7 @@ class Integer https://github.com/ruby/ruby/blob/trunk/numeric.rb#L252 # # Since +int+ is already an Integer, returns +self+. def to_int - return self + self end # call-seq: @@ -244,6 +280,26 @@ class Integer https://github.com/ruby/ruby/blob/trunk/numeric.rb#L280 def ceildiv(other) -div(-other) end + + # + # call-seq: + # int.numerator -> self + # + # Returns self. + # + def numerator + self + end + + # + # call-seq: + # int.denominator -> 1 + # + # Returns 1. + # + def denominator + 1 + end end # call-seq: @@ -276,7 +332,7 @@ class Float https://github.com/ruby/ruby/blob/trunk/numeric.rb#L332 # Since +float+ is already a Float, returns +self+. # def to_f - return self + self end # diff --git a/rational.c b/rational.c index e537bd498b..48a9ab2ed2 100644 --- a/rational.c +++ b/rational.c @@ -2059,30 +2059,6 @@ rb_rational_canonicalize(VALUE x) https://github.com/ruby/ruby/blob/trunk/rational.c#L2059 return x; } -/* - * call-seq: - * int.numerator -> self - * - * Returns self. - */ -static VALUE -integer_numerator(VALUE self) -{ - return self; -} - -/* - * call-seq: - * int.denominator -> 1 - * - * Returns 1. - */ -static VALUE -integer_denominator(VALUE self) -{ - return INT2FIX(1); -} - /* * call-seq: * flo.numerator -> integer @@ -2832,9 +2808,6 @@ Init_Rational(void) https://github.com/ruby/ruby/blob/trunk/rational.c#L2808 rb_define_method(rb_cNumeric, "denominator", numeric_denominator, 0); rb_define_method(rb_cNumeric, "quo", rb_numeric_quo, 1); - rb_define_method(rb_cInteger, "numerator", integer_numerator, 0); - rb_define_method(rb_cInteger, "denominator", integer_denominator, 0); - rb_define_method(rb_cFloat, "numerator", rb_float_numerator, 0); rb_define_method(rb_cFloat, "denominator", rb_float_denominator, 0); -- cgit v1.2.3 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/