1.9$B$N(Brequire$B$,CY$$(B

前田です。

またYehuda K.æƒ…å ±ãªã®ã§ã™ãŒã€Ruby 1.9で、$:ã®è¦ç´ ãŒå¤šã„ã¨requireが遅いようです。

defiant:build$ cat t.rb
1000.times {
$:.push “/opt/ruby/1.8”
}
1000.times do
require “rational”
end
defiant:build$ time ruby -v t.rb
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]
ruby -v t.rb 0.07s user 0.08s system 90% cpu 0.164 total
defiant:build$ time ruby-trunk -v t.rb
ruby 1.9.2dev (2010-03-10 trunk 26866) [i686-linux]
lib/rational.rb is deprecated
ruby-trunk -v t.rb 3.51s user 1.20s system 99% cpu 4.734 total

この例は極端ですが、Gemパッケージがたくさん入っている状態でRailsを
動かしたりすると、結構影響が大きいそうです。

どうも、rb_get_expanded_load_path()が何度も呼ばれるせいでこんなに
遅いようなのですが、$:の中身がすべて絶対パスの時は、expand_pathを
呼ばずにそのまま$:の値を使うようにしてはどうでしょうか。

添付のパッチでかなり早くなりました。

defiant:build$ time ./ruby-trunk -v t.rb
ruby 1.9.2dev (2010-03-11 trunk 26868) [i686-linux]
lib/rational.rb is deprecated
./ruby-trunk -v t.rb 0.28s user 0.01s system 99% cpu 0.293 total

$:に相対パスが入っているとだめですが、幸い1.9では$:から.が削除されて
いますし、require_relativeも使えるので$:に相対パスを入れたいというニーズ
もあまりないんじゃないかと思っています。

e$B@.@%$G$9!#e(B

e$BFC$K0U8+$O$J$$$s$G$9$,!";2>HDI2C$@$1e(B
[ruby-core:28113]
http://redmine.ruby-lang.org/issues/show/2723

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:40603] 1.9e$B$Ne(Brequiree$B$,CY$$e(B”
on Sat, 13 Mar 2010 02:38:01 +0900, Shugo M.
[email protected] writes:
|
|e$B$^$?e(BYehuda K.e$B>pJs$J$N$G$9$,!"e(BRuby 1.9e$B$G!"e(B$:e$B$NMWAG$,B?$$$He(Brequiree$B$,CY$$$h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