Issue #17135 has been updated by S_H_ (Shun Hiraoka).


I try to improve performance of  `Integer#size` in C code with refer to `TrueClass#to_s`


```c
static VALUE rb_cInteger_fix_size;

static VALUE
int_size (VALUE num)
{
    if (FIXNUM_P (num)) {
return rb_cInteger_fix_size;
    }
    else if (RB_TYPE_P (num, T_BIGNUM)) {
return rb_big_size_m (num);
    }
    return Qnil;
}

void
Init_Numeric (void)
{
    rb_cInteger_fix_size = INT2FIX (sizeof (long));
    rb_gc_register_mark_object (rb_cInteger_fix_size);
    rb_define_method (rb_cInteger, "size", int_size, 0);
}
```


benchmark:

```
prelude: |
  n = 42
benchmark: benchmark:
  size: |
    n.size
loop_count: 20000000

```

result:
```bash
sh@MyComputer:~/rubydev/build$ make benchmark/benchmark.yml -e COMPARE_RUBY=~/.rbenv/shims/ruby -e BENCH_RUBY=../install/bin/ruby
# Iteration per second (i/s)

|      |compare-ruby|built-ruby|
|:-----|-----------:|---------:|
|size  |     56.456M|   75.074M|
|      |           -|     1.33x|
```

As a result, seems to be expected  improve performance.

`COMPARE_RUBY` is `ruby 3.0.0dev (2020-09-20T11:39:25Z master 84c4c7bec8) [x86_64-linux]`. `BENCH_RUBY` is patched.

----------------------------------------
Feature #17135: Improve performance of Integer#size method
https://bugs.ruby-lang.org/issues/17135#change-87621

* Author: S_H_ (Shun Hiraoka)
* Status: Assigned
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
----------------------------------------
`Integer#size` seems to show improved performance when written in ruby.

benchmark:

```yml
prelude: |
  n = 42
benchmark:
  size: |
    n.size
loop_count: 20000000
```

result:

```bash
sh@MyComputer:~/rubydev/build$ make benchmark/integer_size.yml -e COMPARE_RUBY=~/.rbenv/shims/ruby -e BENCH_RUBY=../install/bin/ruby
# Iteration per second (i/s)

|      |compare-ruby|built-ruby|
|:-----|-----------:|---------:|
|size  |     65.749M|   87.117M|
|      |           -|     1.33x|
```

`COMPARE_RUBY` is `ruby 2.8.0dev (2020-08-28T10:47:29Z master 7e1fddba4a) [x86_64-linux]`. `BENCH_RUBY` is patched.


pull request:
https://github.com/ruby/ruby/pull/3476



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>