From daec5f9edcfbf98b10a4bfc1aa501c9ac2c64841 Mon Sep 17 00:00:00 2001 From: "S.H" Date: Sat, 2 Jan 2021 11:39:07 +0900 Subject: [PATCH] Improve performance some Float methods [Feature #17498] (#4018) --- .document | 2 +- benchmark/float_methods.yml | 14 +++++++++ common.mk | 8 +++--- inits.c | 2 +- numeric.c | 54 +---------------------------------- integer.rb => numeric.rb | 57 +++++++++++++++++++++++++++++++++++++ 6 files changed, 78 insertions(+), 59 deletions(-) create mode 100644 benchmark/float_methods.yml rename integer.rb => numeric.rb (78%) diff --git a/.document b/.document index 8616203552..a3f7571ea7 100644 --- a/.document +++ b/.document @@ -14,7 +14,7 @@ array.rb ast.rb dir.rb gc.rb -integer.rb +numeric.rb io.rb kernel.rb pack.rb diff --git a/benchmark/float_methods.yml b/benchmark/float_methods.yml new file mode 100644 index 0000000000..56ea41effc --- /dev/null +++ b/benchmark/float_methods.yml @@ -0,0 +1,14 @@ +prelude: | + flo = 4.2 +benchmark: + to_f: | + flo.to_f + abs: | + flo.abs + magnitude: | + flo.magnitude + -@: | + -flo + zero?: | + flo.zero? +loop_count: 20000000 diff --git a/common.mk b/common.mk index a692577636..af647a68bb 100644 --- a/common.mk +++ b/common.mk @@ -1020,7 +1020,7 @@ BUILTIN_RB_SRCS = \ $(srcdir)/ast.rb \ $(srcdir)/dir.rb \ $(srcdir)/gc.rb \ - $(srcdir)/integer.rb \ + $(srcdir)/numeric.rb \ $(srcdir)/io.rb \ $(srcdir)/pack.rb \ $(srcdir)/trace_point.rb \ @@ -8224,7 +8224,6 @@ miniinit.$(OBJEXT): {$(VPATH)}encoding.h miniinit.$(OBJEXT): {$(VPATH)}gc.rb miniinit.$(OBJEXT): {$(VPATH)}gem_prelude.rb miniinit.$(OBJEXT): {$(VPATH)}id.h -miniinit.$(OBJEXT): {$(VPATH)}integer.rb miniinit.$(OBJEXT): {$(VPATH)}intern.h miniinit.$(OBJEXT): {$(VPATH)}internal.h miniinit.$(OBJEXT): {$(VPATH)}internal/anyargs.h @@ -8376,6 +8375,7 @@ miniinit.$(OBJEXT): {$(VPATH)}miniinit.c miniinit.$(OBJEXT): {$(VPATH)}miniprelude.c miniinit.$(OBJEXT): {$(VPATH)}missing.h miniinit.$(OBJEXT): {$(VPATH)}node.h +miniinit.$(OBJEXT): {$(VPATH)}numeric.rb miniinit.$(OBJEXT): {$(VPATH)}onigmo.h miniinit.$(OBJEXT): {$(VPATH)}oniguruma.h miniinit.$(OBJEXT): {$(VPATH)}pack.rb @@ -9062,8 +9062,6 @@ numeric.$(OBJEXT): {$(VPATH)}defines.h numeric.$(OBJEXT): {$(VPATH)}encoding.h numeric.$(OBJEXT): {$(VPATH)}id.h numeric.$(OBJEXT): {$(VPATH)}id_table.h -numeric.$(OBJEXT): {$(VPATH)}integer.rb -numeric.$(OBJEXT): {$(VPATH)}integer.rbinc numeric.$(OBJEXT): {$(VPATH)}intern.h numeric.$(OBJEXT): {$(VPATH)}internal.h numeric.$(OBJEXT): {$(VPATH)}internal/anyargs.h @@ -9208,6 +9206,8 @@ numeric.$(OBJEXT): {$(VPATH)}internal/warning_push.h numeric.$(OBJEXT): {$(VPATH)}internal/xmalloc.h numeric.$(OBJEXT): {$(VPATH)}missing.h numeric.$(OBJEXT): {$(VPATH)}numeric.c +numeric.$(OBJEXT): {$(VPATH)}numeric.rb +numeric.$(OBJEXT): {$(VPATH)}numeric.rbinc numeric.$(OBJEXT): {$(VPATH)}onigmo.h numeric.$(OBJEXT): {$(VPATH)}oniguruma.h numeric.$(OBJEXT): {$(VPATH)}ruby_assert.h diff --git a/inits.c b/inits.c index 95c6229dcc..d2c99ed495 100644 --- a/inits.c +++ b/inits.c @@ -87,7 +87,7 @@ rb_call_builtin_inits(void) #define BUILTIN(n) CALL(builtin_##n) BUILTIN(gc); BUILTIN(ractor); - BUILTIN(integer); + BUILTIN(numeric); BUILTIN(io); BUILTIN(dir); BUILTIN(ast); diff --git a/numeric.c b/numeric.c index e4bb4817bb..2ce6f8bfa6 100644 --- a/numeric.c +++ b/numeric.c @@ -1030,13 +1030,6 @@ flo_coerce(VALUE x, VALUE y) return rb_assoc_new(rb_Float(y), x); } -/* - * call-seq: - * -float -> float - * - * Returns +float+, negated. - */ - VALUE rb_float_uminus(VALUE flt) { @@ -1701,33 +1694,6 @@ rb_float_eql(VALUE x, VALUE y) #define flo_eql rb_float_eql -/* - * call-seq: - * float.to_f -> self - * - * Since +float+ is already a Float, returns +self+. - */ - -static VALUE -flo_to_f(VALUE num) -{ - return num; -} - -/* - * call-seq: - * float.abs -> float - * float.magnitude -> float - * - * Returns the absolute value of +float+. - * - * (-34.56).abs #=> 34.56 - * -34.56.abs #=> 34.56 - * 34.56.abs #=> 34.56 - * - * Float#magnitude is an alias for Float#abs. - */ - VALUE rb_float_abs(VALUE flt) { @@ -1735,19 +1701,6 @@ rb_float_abs(VALUE flt) return DBL2NUM(val); } -/* - * call-seq: - * float.zero? -> true or false - * - * Returns +true+ if +float+ is 0.0. - */ - -static VALUE -flo_zero_p(VALUE num) -{ - return flo_iszero(num) ? Qtrue : Qfalse; -} - /* * call-seq: * float.nan? -> true or false @@ -5677,7 +5630,6 @@ Init_Numeric(void) rb_define_method(rb_cFloat, "to_s", flo_to_s, 0); rb_define_alias(rb_cFloat, "inspect", "to_s"); rb_define_method(rb_cFloat, "coerce", flo_coerce, 1); - rb_define_method(rb_cFloat, "-@", rb_float_uminus, 0); rb_define_method(rb_cFloat, "+", rb_float_plus, 1); rb_define_method(rb_cFloat, "-", rb_float_minus, 1); rb_define_method(rb_cFloat, "*", rb_float_mul, 1); @@ -5697,10 +5649,6 @@ Init_Numeric(void) rb_define_method(rb_cFloat, "<=", flo_le, 1); rb_define_method(rb_cFloat, "eql?", flo_eql, 1); rb_define_method(rb_cFloat, "hash", flo_hash, 0); - rb_define_method(rb_cFloat, "to_f", flo_to_f, 0); - rb_define_method(rb_cFloat, "abs", rb_float_abs, 0); - rb_define_method(rb_cFloat, "magnitude", rb_float_abs, 0); - rb_define_method(rb_cFloat, "zero?", flo_zero_p, 0); rb_define_method(rb_cFloat, "to_i", flo_to_i, 0); rb_define_method(rb_cFloat, "to_int", flo_to_i, 0); @@ -5732,4 +5680,4 @@ rb_float_new(double d) return rb_float_new_inline(d); } -#include "integer.rbinc" +#include "numeric.rbinc" diff --git a/integer.rb b/numeric.rb similarity index 78% rename from integer.rb rename to numeric.rb index d18494f42f..b8b3a90d75 100644 --- a/integer.rb +++ b/numeric.rb @@ -148,3 +148,60 @@ class Integer Primitive.cexpr! 'rb_int_zero_p(self)' end end + +class Float + # + # call-seq: + # float.to_f -> self + # + # Since +float+ is already a Float, returns +self+. + # + def to_f + return self + end + + # + # call-seq: + # float.abs -> float + # float.magnitude -> float + # + # Returns the absolute value of +float+. + # + # (-34.56).abs #=> 34.56 + # -34.56.abs #=> 34.56 + # 34.56.abs #=> 34.56 + # + # Float#magnitude is an alias for Float#abs. + # + def abs + Primitive.attr! 'inline' + Primitive.cexpr! 'rb_float_abs(self)' + end + + def magnitude + Primitive.attr! 'inline' + Primitive.cexpr! 'rb_float_abs(self)' + end + + # + # call-seq: + # -float -> float + # + # Returns +float+, negated. + # + def -@ + Primitive.attr! 'inline' + Primitive.cexpr! 'rb_float_uminus(self)' + end + + # + # call-seq: + # float.zero? -> true or false + # + # Returns +true+ if +float+ is 0.0. + # + def zero? + Primitive.attr! 'inline' + Primitive.cexpr! 'flo_iszero(self) ? Qtrue : Qfalse' + end +end