ruby-changes:66412
From: S.H <ko1@a...>
Date: Thu, 3 Jun 2021 12:05:19 +0900 (JST)
Subject: [ruby-changes:66412] 28b481938b (master): Implemented some NilClass method in Ruby code is faster [Feature #17054] (#3366)
https://git.ruby-lang.org/ruby.git/commit/?id=28b481938b From 28b481938b5c8211aad53ba82fe4ddd978ffc00f Mon Sep 17 00:00:00 2001 From: "S.H" <gamelinks007@g...> Date: Thu, 3 Jun 2021 12:04:56 +0900 Subject: Implemented some NilClass method in Ruby code is faster [Feature #17054] (#3366) --- benchmark/nilclass.yml | 6 ++++++ common.mk | 4 ++++ inits.c | 1 + nilclass.rb | 25 +++++++++++++++++++++++++ object.c | 34 +--------------------------------- 5 files changed, 37 insertions(+), 33 deletions(-) create mode 100644 benchmark/nilclass.yml create mode 100644 nilclass.rb diff --git a/benchmark/nilclass.yml b/benchmark/nilclass.yml new file mode 100644 index 0000000..fba67a5 --- /dev/null +++ b/benchmark/nilclass.yml @@ -0,0 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/nilclass.yml#L1 +benchmark: + to_i: | + nil.to_i + to_f: | + nil.to_f +loop_count: 100000 diff --git a/common.mk b/common.mk index a46d092..626040a 100644 --- a/common.mk +++ b/common.mk @@ -1031,6 +1031,7 @@ BUILTIN_RB_SRCS = \ https://github.com/ruby/ruby/blob/trunk/common.mk#L1031 $(srcdir)/kernel.rb \ $(srcdir)/ractor.rb \ $(srcdir)/timev.rb \ + $(srcdir)/nilclass.rb \ $(srcdir)/prelude.rb \ $(srcdir)/gem_prelude.rb \ $(empty) @@ -8350,6 +8351,7 @@ miniinit.$(OBJEXT): {$(VPATH)}miniinit.c https://github.com/ruby/ruby/blob/trunk/common.mk#L8351 miniinit.$(OBJEXT): {$(VPATH)}miniprelude.c miniinit.$(OBJEXT): {$(VPATH)}missing.h miniinit.$(OBJEXT): {$(VPATH)}node.h +miniinit.$(OBJEXT): {$(VPATH)}nilclass.rb miniinit.$(OBJEXT): {$(VPATH)}numeric.rb miniinit.$(OBJEXT): {$(VPATH)}onigmo.h miniinit.$(OBJEXT): {$(VPATH)}oniguruma.h @@ -9374,6 +9376,8 @@ object.$(OBJEXT): {$(VPATH)}internal/xmalloc.h https://github.com/ruby/ruby/blob/trunk/common.mk#L9376 object.$(OBJEXT): {$(VPATH)}kernel.rb object.$(OBJEXT): {$(VPATH)}kernel.rbinc object.$(OBJEXT): {$(VPATH)}missing.h +object.$(OBJEXT): {$(VPATH)}nilclass.rb +object.$(OBJEXT): {$(VPATH)}nilclass.rbinc object.$(OBJEXT): {$(VPATH)}object.c object.$(OBJEXT): {$(VPATH)}onigmo.h object.$(OBJEXT): {$(VPATH)}oniguruma.h diff --git a/inits.c b/inits.c index 185f14b..3e04c26 100644 --- a/inits.c +++ b/inits.c @@ -97,6 +97,7 @@ rb_call_builtin_inits(void) https://github.com/ruby/ruby/blob/trunk/inits.c#L97 BUILTIN(array); BUILTIN(kernel); BUILTIN(timev); + BUILTIN(nilclass); Init_builtin_prelude(); } #undef CALL diff --git a/nilclass.rb b/nilclass.rb new file mode 100644 index 0000000..5a2e196 --- /dev/null +++ b/nilclass.rb @@ -0,0 +1,25 @@ https://github.com/ruby/ruby/blob/trunk/nilclass.rb#L1 +class NilClass + # + # call-seq: + # nil.to_i -> 0 + # + # Always returns zero. + # + # nil.to_i #=> 0 + # + def to_i + return 0 + end + + # + # call-seq: + # nil.to_f -> 0.0 + # + # Always returns zero. + # + # nil.to_f #=> 0.0 + # + def to_f + return 0.0 + end +end diff --git a/object.c b/object.c index f0c20ed..887d3b5 100644 --- a/object.c +++ b/object.c @@ -1347,37 +1347,6 @@ rb_obj_frozen_p(VALUE obj) https://github.com/ruby/ruby/blob/trunk/object.c#L1347 /* * call-seq: - * nil.to_i -> 0 - * - * Always returns zero. - * - * nil.to_i #=> 0 - */ - - -static VALUE -nil_to_i(VALUE obj) -{ - return INT2FIX(0); -} - -/* - * call-seq: - * nil.to_f -> 0.0 - * - * Always returns zero. - * - * nil.to_f #=> 0.0 - */ - -static VALUE -nil_to_f(VALUE obj) -{ - return DBL2NUM(0.0); -} - -/* - * call-seq: * nil.to_s -> "" * * Always returns the empty string. @@ -4871,8 +4840,6 @@ InitVM_Object(void) https://github.com/ruby/ruby/blob/trunk/object.c#L4840 rb_cNilClass = rb_define_class("NilClass", rb_cObject); rb_cNilClass_to_s = rb_fstring_enc_lit("", rb_usascii_encoding()); rb_gc_register_mark_object(rb_cNilClass_to_s); - rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0); - rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0); rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0); rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0); rb_define_method(rb_cNilClass, "to_h", nil_to_h, 0); @@ -4975,6 +4942,7 @@ InitVM_Object(void) https://github.com/ruby/ruby/blob/trunk/object.c#L4942 } #include "kernel.rbinc" +#include "nilclass.rbinc" void Init_Object(void) -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/