Project

General

Profile

Actions

Bug #3443

closed

requireが遅くなる

Added by mame (Yusuke Endoh) almost 14 years ago. Updated almost 13 years ago.

Status:
Closed
Target version:
ruby -v:
ruby 1.9.3dev (2010-05-30 trunk 28086) [i386-cygwin]
Backport:
[ruby-dev:41610]

Description

=begin
[ruby-dev:41502] をチケット化します。


三浦と申します。

1年ぶりくらいに最新バージョンのRubyでyarv2llvmを動かしてみたら、
すごく時間がかかるようになっていたので調べてみました。
環境は以下の通りです。
  CPU AMD Athlon(tm)X2 Dual-Core QL-60 1.90 GHz

 RAM 4GBytes

 OS    Windows Vista

CYGWIN_NT-6.0 miura-PC 1.7.5(0.225/5/3) 2010-04-12 19:07 i686 Cygwin

Ruby ruby 1.9.3dev (2010-05-30 trunk 28086) [i386-cygwin]

その結果、require時にファイル入力の後に大量のRead/Writeではない
File Operationが発生していることが分かりました。
さらに、strace(Cygwin版)を見たところ、次のようなシステムコールが
大量に発生していました。

857 67985110 [main] ruby 4896 open: open
(/home/miura/demo/yarv2llvm64/yarv2llvm/lib/vmtraverse.rb, 0x0)
101 67985211 [main] ruby 4896 normalize_posix_path: src
/home/miura/demo/yarv2llvm64/yarv2llvm/lib/vmtraverse.rb
96 67985307 [main] ruby 4896 normalize_posix_path:
/home/miura/demo/yarv2llvm64/yarv2llvm/lib/vmtraverse.rb =
normalize_posix_path
(/home/miura/demo/yarv2llvm64/yarv2llvm/lib/vmtraverse.rb)
96 67985403 [main] ruby 4896 mount_info::conv_to_win32_path:
conv_to_win32_path
(/home/miura/demo/yarv2llvm64/yarv2llvm/lib/vmtraverse.rb)
96 67985499 [main] ruby 4896 set_flags: flags: binary (0x2)
90 67985589 [main] ruby 4896 mount_info::conv_to_win32_path: src_path
/home/miura/demo/yarv2llvm64/yarv2llvm/lib/vmtraverse.rb, dst
C:\cygwin\home\miura\demo\yarv2llvm64\yarv2llvm\lib\vmtraverse.rb, flags
0x3000A, rc 0
954 67986543 [main] ruby 4896 symlink_info::check: 0x0 = NtCreateFile
(??\C:\cygwin\home\miura\demo\yarv2llvm64\yarv2llvm\lib\vmtraverse.rb)
352 67986895 [main] ruby 4896 symlink_info::check: not a symlink
575 67987470 [main] ruby 4896 symlink_info::check: 0 = symlink.check
(C:\cygwin\home\miura\demo\yarv2llvm64\yarv2llvm\lib\vmtraverse.rb,
0x22A320) (0x3000A)
156 67987626 [main] ruby 4896 path_conv::check:
this->path(C:\cygwin\home\miura\demo\yarv2llvm64\yarv2llvm\lib\vmtraverse.rb),
has_acls(1)
144 67987770 [main] ruby 4896 build_fh_pc: fh 0x612269D4
103 67987873 [main] ruby 4896 fhandler_base::open:
(??\C:\cygwin\home\miura\demo\yarv2llvm64\yarv2llvm\lib\vmtraverse.rb,
0x100000)
711 67988584 [main] ruby 4896 fhandler_base::set_flags: flags 0x100000,
supplied_bin 0x10000

これを見る限り、Cygwinではファイルのパスを得るために
ファイルシステムのアクセスを含む結構重い処理を行っているようです。

そこで、ファイルパスを繰り返し得る処理をしているところを
探したところ、iseq.cのprepare_iseq_build中の

iseq->filepath = filepath == Qnil ? Qnil : rb_realpath_internal(Qnil,
filepath, 1);

が見つかりました。試しにiseq->filepathを常にQnilにするように変更
して、実行速度の差を見てみました。

結果です。
time ruby -I . yarv2llvm.rb fib.rb

オリジナル
real 0m35.954s
user 0m4.163s
sys 0m14.351s

常にQnil       
real 0m10.323s
user 0m2.525s
sys 0m3.197s

また、rb_realpath_internal(Qnil, filepath, 1)の結果をキャッシュする
ようにしてみました。その結果です。

キャッシュ
real 0m26.757s
user 0m3.291s
sys 0m10.623s

この実験で使った変更点を以下に示します。
なお、iseq_s_compileの

  • rb_scan_args(argc, argv, "13", &src, &file, &path, &line, &opt);
  • rb_scan_args(argc, argv, "14", &src, &file, &path, &line, &opt);
    は直接関係ないのですが、ついでに乗せておきます。
    こちらも御検討してもらえると幸いです。

Index: iseq.c

--- iseq.c (revision 28086)
+++ iseq.c (working copy)
@@ -217,7 +217,22 @@

VALUE rb_realpath_internal(VALUE basedir, VALUE path, int strict);

+static VALUE path_cache;
+static VALUE realpath_cache;
static VALUE
+realpath_iseq(VALUE path)
+{

  • if (path_cache == path) {
  •    return realpath_cache;
    
  • }
  • path_cache = path;
  • realpath_cache = rb_realpath_internal(Qnil, path, 1);
  • return realpath_cache;
    +}

+static VALUE
prepare_iseq_build(rb_iseq_t *iseq,
VALUE name, VALUE filename, VALUE filepath, VALUE line_no,
VALUE parent, VALUE type, VALUE block_opt,
@@ -228,7 +243,9 @@

  iseq->name = name;
  iseq->filename = filename;
  • iseq->filepath = filepath == Qnil ? Qnil : rb_realpath_internal(Qnil,
    filepath, 1);
  • //iseq->filepath = filepath == Qnil ? Qnil : rb_realpath_internal(Qnil,
    filepath, 1);

  • iseq->filepath = filepath == Qnil ? Qnil : realpath_iseq(filepath);

  • // iseq->filepath = Qnil;
    iseq->line_no = (unsigned short)line_no; /* TODO: really enough? */
    iseq->defined_method_id = 0;
    iseq->mark_ary = rb_ary_tmp_new(3);
    @@ -579,7 +596,7 @@

    rb_secure(1);

  • rb_scan_args(argc, argv, "13", &src, &file, &path, &line, &opt);
  • rb_scan_args(argc, argv, "14", &src, &file, &path, &line, &opt);
    if (NIL_P(file)) file = rb_str_new2("");
    if (NIL_P(line)) line = INT2FIX(1);

@@ -1516,5 +1533,7 @@
rb_define_singleton_method(rb_cISeq, "compile_option=",
iseq_s_compile_option_set, 1);
rb_define_singleton_method(rb_cISeq, "disasm", iseq_s_disasm, 1);
rb_define_singleton_method(rb_cISeq, "disassemble", iseq_s_disasm, 1);
+

  • rb_global_variable(&path_cache);
    }
    =end
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0