Skip to content

Commit 8120971

Browse files
skipkayhilbyroot
authored andcommitted
Move more NilClass methods to ruby
``` $ make benchmark ITEM=nilclass COMPARE_RUBY="/opt/rubies/ruby-master/bin/ruby" /opt/rubies/3.4.2/bin/ruby --disable=gems -rrubygems -I../benchmark/lib ../benchmark/benchmark-driver/exe/benchmark-driver \ --executables="compare-ruby::/opt/rubies/ruby-master/bin/ruby -I.ext/common --disable-gem" \ --executables="built-ruby::./miniruby -I../lib -I. -I.ext/common ../tool/runruby.rb --extout=.ext -- --disable-gems --disable-gem" \ --output=markdown --output-compare -v $(find ../benchmark -maxdepth 1 -name 'nilclass' -o -name '*nilclass*.yml' -o -name '*nilclass*.rb' | sort) compare-ruby: ruby 3.5.0dev (2025-06-02T13:52:25Z master cbd49ec) +PRISM [arm64-darwin24] built-ruby: ruby 3.5.0dev (2025-06-02T22:47:21Z hm-ruby-nilclass 3e7f1f0) +PRISM [arm64-darwin24] | |compare-ruby|built-ruby| |:------------|-----------:|---------:| |rationalize | 24.056M| 53.908M| | | -| 2.24x| |to_c | 23.652M| 82.781M| | | -| 3.50x| |to_i | 89.526M| 84.388M| | | 1.06x| -| |to_f | 84.746M| 96.899M| | | -| 1.14x| |to_r | 25.107M| 83.472M| | | -| 3.32x| |splat | 42.772M| 42.717M| | | 1.00x| -| ``` This makes them much faster
1 parent 3abdd42 commit 8120971

File tree

4 files changed

+44
-51
lines changed

4 files changed

+44
-51
lines changed

benchmark/nilclass.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
prelude: |
22
def a = nil
33
benchmark:
4+
rationalize:
5+
nil.rationalize
6+
to_c: |
7+
nil.to_c
48
to_i: |
59
nil.to_i
610
to_f: |
711
nil.to_f
12+
to_r: |
13+
nil.to_r
814
splat: |
915
a(*nil)
1016
loop_count: 100000

complex.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,21 +1925,6 @@ nucomp_to_c(VALUE self)
19251925
return self;
19261926
}
19271927

1928-
/*
1929-
* call-seq:
1930-
* to_c -> (0+0i)
1931-
*
1932-
* Returns zero as a Complex:
1933-
*
1934-
* nil.to_c # => (0+0i)
1935-
*
1936-
*/
1937-
static VALUE
1938-
nilclass_to_c(VALUE self)
1939-
{
1940-
return rb_complex_new1(INT2FIX(0));
1941-
}
1942-
19431928
/*
19441929
* call-seq:
19451930
* to_c -> complex
@@ -2693,7 +2678,6 @@ Init_Complex(void)
26932678
rb_define_method(rb_cComplex, "to_r", nucomp_to_r, 0);
26942679
rb_define_method(rb_cComplex, "rationalize", nucomp_rationalize, -1);
26952680
rb_define_method(rb_cComplex, "to_c", nucomp_to_c, 0);
2696-
rb_define_method(rb_cNilClass, "to_c", nilclass_to_c, 0);
26972681
rb_define_method(rb_cNumeric, "to_c", numeric_to_c, 0);
26982682

26992683
rb_define_method(rb_cString, "to_c", string_to_c, 0);

nilclass.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
11
class NilClass
2+
#
3+
# call-seq:
4+
# rationalize(eps = nil) -> (0/1)
5+
#
6+
# Returns zero as a Rational:
7+
#
8+
# nil.rationalize # => (0/1)
9+
#
10+
# Argument +eps+ is ignored.
11+
#
12+
def rationalize(eps = nil)
13+
0r
14+
end
15+
16+
#
17+
# call-seq:
18+
# to_c -> (0+0i)
19+
#
20+
# Returns zero as a Complex:
21+
#
22+
# nil.to_c # => (0+0i)
23+
#
24+
def to_c
25+
0i
26+
end
27+
228
#
329
# call-seq:
430
# nil.to_i -> 0
@@ -22,4 +48,16 @@ def to_i
2248
def to_f
2349
return 0.0
2450
end
51+
52+
#
53+
# call-seq:
54+
# to_r -> (0/1)
55+
#
56+
# Returns zero as a Rational:
57+
#
58+
# nil.to_r # => (0/1)
59+
#
60+
def to_r
61+
0r
62+
end
2563
end

rational.c

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,39 +2107,6 @@ rb_float_denominator(VALUE self)
21072107
return nurat_denominator(r);
21082108
}
21092109

2110-
/*
2111-
* call-seq:
2112-
* to_r -> (0/1)
2113-
*
2114-
* Returns zero as a Rational:
2115-
*
2116-
* nil.to_r # => (0/1)
2117-
*
2118-
*/
2119-
static VALUE
2120-
nilclass_to_r(VALUE self)
2121-
{
2122-
return rb_rational_new1(INT2FIX(0));
2123-
}
2124-
2125-
/*
2126-
* call-seq:
2127-
* rationalize(eps = nil) -> (0/1)
2128-
*
2129-
* Returns zero as a Rational:
2130-
*
2131-
* nil.rationalize # => (0/1)
2132-
*
2133-
* Argument +eps+ is ignored.
2134-
*
2135-
*/
2136-
static VALUE
2137-
nilclass_rationalize(int argc, VALUE *argv, VALUE self)
2138-
{
2139-
rb_check_arity(argc, 0, 1);
2140-
return nilclass_to_r(self);
2141-
}
2142-
21432110
/*
21442111
* call-seq:
21452112
* int.to_r -> rational
@@ -2823,8 +2790,6 @@ Init_Rational(void)
28232790
rb_define_method(rb_cFloat, "numerator", rb_float_numerator, 0);
28242791
rb_define_method(rb_cFloat, "denominator", rb_float_denominator, 0);
28252792

2826-
rb_define_method(rb_cNilClass, "to_r", nilclass_to_r, 0);
2827-
rb_define_method(rb_cNilClass, "rationalize", nilclass_rationalize, -1);
28282793
rb_define_method(rb_cInteger, "to_r", integer_to_r, 0);
28292794
rb_define_method(rb_cInteger, "rationalize", integer_rationalize, -1);
28302795
rb_define_method(rb_cFloat, "to_r", float_to_r, 0);

0 commit comments

Comments
 (0)