1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Implemented some NilClass method in Ruby code is faster [Feature #17054] (#3366)

This commit is contained in:
S.H 2021-06-03 12:04:56 +09:00 committed by GitHub
parent bc65cf1a92
commit 28b481938b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
Notes: git 2021-06-03 12:05:23 +09:00
Merged-By: k0kubun <takashikkbn@gmail.com>
5 changed files with 37 additions and 33 deletions

6
benchmark/nilclass.yml Normal file
View file

@ -0,0 +1,6 @@
benchmark:
to_i: |
nil.to_i
to_f: |
nil.to_f
loop_count: 100000

View file

@ -1031,6 +1031,7 @@ BUILTIN_RB_SRCS = \
$(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
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
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

View file

@ -97,6 +97,7 @@ rb_call_builtin_inits(void)
BUILTIN(array);
BUILTIN(kernel);
BUILTIN(timev);
BUILTIN(nilclass);
Init_builtin_prelude();
}
#undef CALL

25
nilclass.rb Normal file
View file

@ -0,0 +1,25 @@
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

View file

@ -1345,37 +1345,6 @@ rb_obj_frozen_p(VALUE obj)
* The class of the singleton object <code>nil</code>.
*/
/*
* 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 -> ""
@ -4871,8 +4840,6 @@ InitVM_Object(void)
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)
}
#include "kernel.rbinc"
#include "nilclass.rbinc"
void
Init_Object(void)