Issue #11130 has been updated by Masaki Matsushita.
再入チェックを入れてパフォーマンスを比較した結果、改善が見られなかったのでrevertしました。
再入チェック付の最適化:
ruby-dev bm_enum_to_a_sized.rb 19.46s user 1.29s system 99% cpu 20.760 total
ruby-dev bm_enum_to_a_sized.rb 19.46s user 1.29s system 99% cpu 20.754 total
ruby-dev bm_enum_to_a_sized.rb 19.68s user 1.31s system 99% cpu 21.006 total
trunk:
ruby-dev bm_enum_to_a_sized.rb 19.93s user 1.30s system 99% cpu 21.240 total
ruby-dev bm_enum_to_a_sized.rb 19.88s user 1.30s system 99% cpu 21.189 total
ruby-dev bm_enum_to_a_sized.rb 20.81s user 1.48s system 99% cpu 22.322 total
----------------------------------------
Bug #11130: Re: [ruby-changes:38376] glass:r50457 (trunk): * enum.c (enum_to_a): Use size to set array capa when possible.
https://bugs.ruby-lang.org/issues/11130#change-52420
* Author: Kouhei Sutou
* Status: Closed
* Priority: Normal
* Assignee: Masaki Matsushita
* ruby -v: r50457
* Backport: 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: DONTNEED
----------------------------------------
須藤です。
~~~diff
+ if (NIL_P(size) || size == Qundef) {
+ ary = rb_ary_new();
+ }
+ else {
+ ary = rb_ary_new_capa(NUM2LONG(size));
+ }
~~~
を
~~~c
if (FIXNUM_P(size)) {
ary = rb_ary_new_capa(NUM2LONG(size));
}
else {
ary = rb_ary_new();
}
~~~
とかsizeが返す値が数値じゃなかったらこれまでと同じ挙動にする
ようにしてもらえないでしょうか?
これまでは
~~~ruby
class NonIntegerSizeEnum
include Enumerable
def initialize(n)
@n = n
end
def each
@n.times { |i| yield i }
end
def size
:size
end
end
NonIntegerSizeEnum.new(100).to_a
~~~
というコードが動いていたんですが、この変更の後からは
> /tmp/b.rb:17:in `to_a': no implicit conversion of Symbol into Integer (TypeError)
というエラーがでるようになってしまって困っています。
--
https://bugs.ruby-lang.org/