[前][次][番号順一覧][スレッド一覧]

ruby-changes:61967

From: Takashi <ko1@a...>
Date: Sun, 28 Jun 2020 02:08:03 +0900 (JST)
Subject: [ruby-changes:61967] 0703e01471 (master): Mark some Integer methods as inline (#3264)

https://git.ruby-lang.org/ruby.git/commit/?id=0703e01471

From 0703e014713ae92f4c8a2b31e385718dc2452eac Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@g...>
Date: Sat, 27 Jun 2020 10:07:47 -0700
Subject: Mark some Integer methods as inline (#3264)


diff --git a/benchmark/mjit_int_zero_p.yml b/benchmark/mjit_int_zero_p.yml
deleted file mode 100644
index f8950ba..0000000
--- a/benchmark/mjit_int_zero_p.yml
+++ /dev/null
@@ -1,21 +0,0 @@ https://github.com/ruby/ruby/blob/trunk/#L0
-type: lib/benchmark_driver/runner/mjit
-prelude: |
-  def mjit_zero?(int)
-    int.zero?
-  end
-
-  def mjit_eq_0(int)
-    int == 0
-  end
-
-benchmark:
-  - name: 0.zero?
-    script: mjit_zero?(0)
-  - name: 1.zero?
-    script: mjit_zero?(1)
-  - name: 0 == 0
-    script: mjit_eq_0(0)
-  - name: 1 == 0
-    script: mjit_eq_0(1)
-
-loop_count: 40000000
diff --git a/benchmark/mjit_integer.yml b/benchmark/mjit_integer.yml
new file mode 100644
index 0000000..cd32889
--- /dev/null
+++ b/benchmark/mjit_integer.yml
@@ -0,0 +1,26 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/mjit_integer.yml#L1
+type: lib/benchmark_driver/runner/mjit
+prelude: |
+  def mjit_abs(int)        int.abs        end
+  def mjit_bit_length(int) int.bit_length end
+  def mjit_even?(int)      int.even?      end
+  def mjit_integer?(int)   int.integer?   end
+  def mjit_magnitude(int)  int.magnitude  end
+  def mjit_odd?(int)       int.odd?       end
+  def mjit_ord(int)        int.ord        end
+  def mjit_to_i(int)       int.to_i       end
+  def mjit_to_int(int)     int.to_int     end
+  def mjit_zero?(int)      int.zero?      end
+
+benchmark:
+  - mjit_abs(-1)
+  - mjit_bit_length(100)
+  - mjit_even?(2)
+  - mjit_integer?(0)
+  - mjit_magnitude(-1)
+  - mjit_odd?(1)
+  - mjit_ord(1)
+  - mjit_to_i(1)
+  - mjit_to_int(1)
+  - mjit_zero?(0)
+
+loop_count: 40000000
diff --git a/integer.rb b/integer.rb
index cc2e589..fe1e8a0 100644
--- a/integer.rb
+++ b/integer.rb
@@ -1,4 +1,82 @@ https://github.com/ruby/ruby/blob/trunk/integer.rb#L1
 class Integer
+  def abs
+    Primitive.attr! 'inline'
+    Primitive.cexpr! 'rb_int_abs(self)'
+  end
+
+  def bit_length
+    Primitive.attr! 'inline'
+    Primitive.cexpr! 'rb_int_bit_length(self)'
+  end
+
+  #  call-seq:
+  #     int.even?  ->  true or false
+  #
+  #  Returns +true+ if +int+ is an even number.
+  def even?
+    Primitive.attr! 'inline'
+    Primitive.cexpr! 'int_even_p(self)'
+  end
+
+  #  call-seq:
+  #     int.integer?  ->  true
+  #
+  #  Since +int+ is already an Integer, this always returns +true+.
+  def integer?
+    Primitive.attr! 'inline'
+    Primitive.cexpr! 'Qtrue'
+  end
+
+  def magnitude
+    Primitive.attr! 'inline'
+    Primitive.cexpr! 'rb_int_abs(self)'
+  end
+
+  #  call-seq:
+  #     int.odd?  ->  true or false
+  #
+  #  Returns +true+ if +int+ is an odd number.
+  def odd?
+    Primitive.attr! 'inline'
+    Primitive.cexpr! 'rb_int_odd_p(self)'
+  end
+
+  #  call-seq:
+  #     int.ord  ->  self
+  #
+  #  Returns the +int+ itself.
+  #
+  #     97.ord   #=> 97
+  #
+  #  This method is intended for compatibility to character literals
+  #  in Ruby 1.9.
+  #
+  #  For example, <code>?a.ord</code> returns 97 both in 1.8 and 1.9.
+  def ord
+    Primitive.attr! 'inline'
+    Primitive.cexpr! 'self'
+  end
+
+  #  call-seq:
+  #     int.to_i    ->  integer
+  #
+  #  Since +int+ is already an Integer, returns +self+.
+  #
+  #  #to_int is an alias for #to_i.
+  def to_i
+    Primitive.attr! 'inline'
+    Primitive.cexpr! 'self'
+  end
+
+  #  call-seq:
+  #     int.to_int  ->  integer
+  #
+  #  Since +int+ is already an Integer, returns +self+.
+  def to_int
+    Primitive.attr! 'inline'
+    Primitive.cexpr! 'self'
+  end
+
   # call-seq:
   #    int.zero? -> true or false
   #
diff --git a/numeric.c b/numeric.c
index 76567f8..4fee3d9 100644
--- a/numeric.c
+++ b/numeric.c
@@ -3217,42 +3217,6 @@ rb_num2ull(VALUE val) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3217
  *
  */
 
-/*
- *  call-seq:
- *     int.to_i    ->  integer
- *     int.to_int  ->  integer
- *
- *  Since +int+ is already an Integer, returns +self+.
- *
- *  #to_int is an alias for #to_i.
- */
-
-static VALUE
-int_to_i(VALUE num)
-{
-    return num;
-}
-
-/*
- *  call-seq:
- *     int.integer?  ->  true
- *
- *  Since +int+ is already an Integer, this always returns +true+.
- */
-
-static VALUE
-int_int_p(VALUE num)
-{
-    return Qtrue;
-}
-
-/*
- *  call-seq:
- *     int.odd?  ->  true or false
- *
- *  Returns +true+ if +int+ is an odd number.
- */
-
 VALUE
 rb_int_odd_p(VALUE num)
 {
@@ -3268,13 +3232,6 @@ rb_int_odd_p(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3232
     }
 }
 
-/*
- *  call-seq:
- *     int.even?  ->  true or false
- *
- *  Returns +true+ if +int+ is an even number.
- */
-
 static VALUE
 int_even_p(VALUE num)
 {
@@ -3468,26 +3425,6 @@ int_chr(int argc, VALUE *argv, VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3425
 }
 
 /*
- *  call-seq:
- *     int.ord  ->  self
- *
- *  Returns the +int+ itself.
- *
- *     97.ord   #=> 97
- *
- *  This method is intended for compatibility to character literals
- *  in Ruby 1.9.
- *
- *  For example, <code>?a.ord</code> returns 97 both in 1.8 and 1.9.
- */
-
-static VALUE
-int_ord(VALUE num)
-{
-    return num;
-}
-
-/*
  * Fixnum
  */
 
@@ -5644,9 +5581,6 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5581
 
     rb_define_method(rb_cInteger, "to_s", int_to_s, -1);
     rb_define_alias(rb_cInteger, "inspect", "to_s");
-    rb_define_method(rb_cInteger, "integer?", int_int_p, 0);
-    rb_define_method(rb_cInteger, "odd?", rb_int_odd_p, 0);
-    rb_define_method(rb_cInteger, "even?", int_even_p, 0);
     rb_define_method(rb_cInteger, "allbits?", int_allbits_p, 1);
     rb_define_method(rb_cInteger, "anybits?", int_anybits_p, 1);
     rb_define_method(rb_cInteger, "nobits?", int_nobits_p, 1);
@@ -5657,9 +5591,6 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5591
     rb_define_method(rb_cInteger, "next", int_succ, 0);
     rb_define_method(rb_cInteger, "pred", int_pred, 0);
     rb_define_method(rb_cInteger, "chr", int_chr, -1);
-    rb_define_method(rb_cInteger, "ord", int_ord, 0);
-    rb_define_method(rb_cInteger, "to_i", int_to_i, 0);
-    rb_define_method(rb_cInteger, "to_int", int_to_i, 0);
     rb_define_method(rb_cInteger, "to_f", int_to_f, 0);
     rb_define_method(rb_cInteger, "floor", int_floor, -1);
     rb_define_method(rb_cInteger, "ceil", int_ceil, -1);
@@ -5682,9 +5613,6 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5613
 
     rb_define_method(rb_cInteger, "pow", rb_int_powm, -1); /* in bignum.c */
 
-    rb_define_method(rb_cInteger, "abs", rb_int_abs, 0);
-    rb_define_method(rb_cInteger, "magnitude", rb_int_abs, 0);
-
     rb_define_method(rb_cInteger, "===", rb_int_equal, 1);
     rb_define_method(rb_cInteger, "==", rb_int_equal, 1);
     rb_define_method(rb_cInteger, ">", rb_int_gt, 1);
@@ -5702,7 +5630,6 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5630
     rb_define_method(rb_cInteger, ">>", rb_int_rshift, 1);
 
     rb_define_method(rb_cInteger, "size", int_size, 0);
-    rb_define_method(rb_cInteger, "bit_length", rb_int_bit_length, 0);
     rb_define_method(rb_cInteger, "digits", rb_int_digits, -1);
 
     /* An obsolete class, use Integer */
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]