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@gmail.com>
This commit is contained in:
S.H 2021-06-05 13:57:21 +09:00 committed by GitHub
parent 033e76e760
commit 3208a5df2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
Notes: git 2021-06-05 13:57:41 +09:00
Merged-By: k0kubun <takashikkbn@gmail.com>
5 changed files with 26 additions and 21 deletions

View File

@ -8,6 +8,7 @@ prelude: |
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:
- mjit_magnitude(-1)
- mjit_odd?(1)
- mjit_ord(1)
- mjit_size(1)
- mjit_to_i(1)
- mjit_to_int(1)
- mjit_uminus(1)

View File

@ -515,7 +515,7 @@ precompile_inlinable_iseqs(FILE *f, const rb_iseq_t *iseq, struct compile_status
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

View File

@ -735,7 +735,7 @@ set_compiling_iseqs(const rb_iseq_t *iseq)
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);

View File

@ -4715,30 +4715,14 @@ rb_int_abs(VALUE num)
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)
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 */

View File

@ -167,6 +167,26 @@ class Integer
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
#