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

ruby-changes:66431

From: S.H <ko1@a...>
Date: Sat, 5 Jun 2021 13:57:34 +0900 (JST)
Subject: [ruby-changes:66431] 3208a5df2d (master): Improve perfomance for Integer#size method [Feature #17135] (#3476)

https://git.ruby-lang.org/ruby.git/commit/?id=3208a5df2d

From 3208a5df2dfb429752a130a36274464e9924cf44 Mon Sep 17 00:00:00 2001
From: "S.H" <gamelinks007@g...>
Date: Sat, 5 Jun 2021 13:57:21 +0900
Subject: Improve perfomance for Integer#size method [Feature #17135] (#3476)

* Improve perfomance for Integer#size method [Feature #17135]

* re-run ci

* Let MJIT frame skip work for Integer#size

Co-authored-by: Takashi Kokubun <takashikkbn@g...>
---
 benchmark/mjit_integer.yml |  2 ++
 mjit_compile.c             |  2 +-
 mjit_worker.c              |  2 +-
 numeric.c                  | 21 ++-------------------
 numeric.rb                 | 20 ++++++++++++++++++++
 5 files changed, 26 insertions(+), 21 deletions(-)

diff --git a/benchmark/mjit_integer.yml b/benchmark/mjit_integer.yml
index edc3556..a6b5c9e 100644
--- a/benchmark/mjit_integer.yml
+++ b/benchmark/mjit_integer.yml
@@ -8,6 +8,7 @@ prelude: | https://github.com/ruby/ruby/blob/trunk/benchmark/mjit_integer.yml#L8
   def mjit_magnitude(int)  int.magnitude  end
   def mjit_odd?(int)       int.odd?       end
   def mjit_ord(int)        int.ord        end
+  def mjit_size(int)       int.size       end
   def mjit_to_i(int)       int.to_i       end
   def mjit_to_int(int)     int.to_int     end
   def mjit_uminus(int)     -int           end
@@ -22,6 +23,7 @@ benchmark: https://github.com/ruby/ruby/blob/trunk/benchmark/mjit_integer.yml#L23
   - mjit_magnitude(-1)
   - mjit_odd?(1)
   - mjit_ord(1)
+  - mjit_size(1)
   - mjit_to_i(1)
   - mjit_to_int(1)
   - mjit_uminus(1)
diff --git a/mjit_compile.c b/mjit_compile.c
index afa5e62..d68d440 100644
--- a/mjit_compile.c
+++ b/mjit_compile.c
@@ -515,7 +515,7 @@ precompile_inlinable_iseqs(FILE *f, const rb_iseq_t *iseq, struct compile_status https://github.com/ruby/ruby/blob/trunk/mjit_compile.c#L515
     unsigned int pos = 0;
     while (pos < body->iseq_size) {
         int insn = rb_vm_insn_decode(body->iseq_encoded[pos]);
-        if (insn == BIN(opt_send_without_block)) { // `compile_inlined_cancel_handler` supports only `opt_send_without_block`
+        if (insn == BIN(opt_send_without_block) || insn == BIN(opt_size)) { // `compile_inlined_cancel_handler` supports only `opt_send_without_block`
             CALL_DATA cd = (CALL_DATA)body->iseq_encoded[pos + 1];
             const struct rb_callinfo *ci = cd->ci;
             const struct rb_callcache *cc = captured_cc_entries(status)[call_data_index(cd, body)]; // use copy to avoid race condition
diff --git a/mjit_worker.c b/mjit_worker.c
index 2e87b1c..bfcf8c0 100644
--- a/mjit_worker.c
+++ b/mjit_worker.c
@@ -735,7 +735,7 @@ set_compiling_iseqs(const rb_iseq_t *iseq) https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L735
     unsigned int pos = 0;
     while (pos < iseq->body->iseq_size) {
         int insn = rb_vm_insn_decode(iseq->body->iseq_encoded[pos]);
-        if (insn == BIN(opt_send_without_block)) {
+        if (insn == BIN(opt_send_without_block) || insn == BIN(opt_size)) {
             CALL_DATA cd = (CALL_DATA)iseq->body->iseq_encoded[pos + 1];
             extern const rb_iseq_t *rb_mjit_inlinable_iseq(const struct rb_callinfo *ci, const struct rb_callcache *cc);
             const rb_iseq_t *iseq = rb_mjit_inlinable_iseq(cd->ci, cd->cc);
diff --git a/numeric.c b/numeric.c
index d0538ca..b1425ca 100644
--- a/numeric.c
+++ b/numeric.c
@@ -4715,30 +4715,14 @@ rb_int_abs(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4715
     return Qnil;
 }
 
-/*
- *  Document-method: Integer#size
- *  call-seq:
- *     int.size  ->  int
- *
- *  Returns the number of bytes in the machine representation of +int+
- *  (machine dependent).
- *
- *     1.size               #=> 8
- *     -1.size              #=> 8
- *     2147483647.size      #=> 8
- *     (256**10 - 1).size   #=> 10
- *     (256**20 - 1).size   #=> 20
- *     (256**40 - 1).size   #=> 40
- */
-
 static VALUE
 fix_size(VALUE fix)
 {
     return INT2FIX(sizeof(long));
 }
 
-static VALUE
-int_size(VALUE num)
+MJIT_FUNC_EXPORTED VALUE
+rb_int_size(VALUE num)
 {
     if (FIXNUM_P(num)) {
 	return fix_size(num);
@@ -5458,7 +5442,6 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5442
     rb_define_method(rb_cInteger, "<<", rb_int_lshift, 1);
     rb_define_method(rb_cInteger, ">>", rb_int_rshift, 1);
 
-    rb_define_method(rb_cInteger, "size", int_size, 0);
     rb_define_method(rb_cInteger, "digits", rb_int_digits, -1);
 
     /* An obsolete class, use Integer */
diff --git a/numeric.rb b/numeric.rb
index be33c5b..c892cbe 100644
--- a/numeric.rb
+++ b/numeric.rb
@@ -167,6 +167,26 @@ class Integer https://github.com/ruby/ruby/blob/trunk/numeric.rb#L167
     return self
   end
 
+  #
+  #  Document-method: Integer#size
+  #  call-seq:
+  #     int.size  ->  int
+  #
+  #  Returns the number of bytes in the machine representation of +int+
+  #  (machine dependent).
+  #
+  #     1.size               #=> 8
+  #     -1.size              #=> 8
+  #     2147483647.size      #=> 8
+  #     (256**10 - 1).size   #=> 10
+  #     (256**20 - 1).size   #=> 20
+  #     (256**40 - 1).size   #=> 40
+  #
+  def size
+    Primitive.attr! 'inline'
+    Primitive.cexpr! 'rb_int_size(self)'
+  end
+
   #  call-seq:
   #     int.to_i    ->  integer
   #
-- 
cgit v1.1


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

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