Permalink
Browse files

* load.c (rb_get_expanded_load_path): It is said that require is

  slow.  Yusuke Endoh's investigation pointed out that the
  expand_path operation in this function is called over and over
  and over again, which effectively emits tons of lstat-variant
  system calls. To prevent it, lets cache the expanded path.  TODO
  it lacks cache invalidation for now (I doubt if it really
  matters).
  #3924, #4446.
  • Loading branch information...
1 parent 68aa445 commit c229cb42fe436cfad648cdcd7305c5730f4d1b82 @shyouhei committed May 31, 2011
Showing with 22 additions and 2 deletions.
  1. +11 −0 ChangeLog
  2. +11 −2 load.c
View
@@ -1,3 +1,14 @@
+Tue May 31 13:11:19 2011 URABE Shyouhei <shyouhei@ruby-lang.org>
+
+ * load.c (rb_get_expanded_load_path): It is said that require is
+ slow. Yusuke Endoh's investigation pointed out that the
+ expand_path operation in this function is called over and over
+ and over again, which effectively emits tons of lstat-variant
+ system calls. To prevent it, lets cache the expanded path. TODO
+ it lacks cache invalidation for now (I doubt if it really
+ matters).
+ #3924, #4446.
+
Tue May 31 12:53:10 2011 NAKAMURA Usaku <usa@ruby-lang.org>
* test/io/wait/test_io_wait.rb (TestIOWait#{test_nread,test_ready?,
View
@@ -33,6 +33,8 @@ rb_get_load_path(void)
return load_path;
}
+static VALUE expander = Qundef;
+
VALUE
rb_get_expanded_load_path(void)
{
@@ -42,8 +44,13 @@ rb_get_expanded_load_path(void)
ary = rb_ary_new2(RARRAY_LEN(load_path));
for (i = 0; i < RARRAY_LEN(load_path); ++i) {
- VALUE path = rb_file_expand_path(RARRAY_PTR(load_path)[i], Qnil);
- rb_str_freeze(path);
+ VALUE orig = RARRAY_PTR(load_path)[i];
+ VALUE path = rb_hash_lookup2(expander, orig, Qfalse);
+ if (!RTEST(path)) {
+ path = rb_file_expand_path(orig, Qnil);
+ rb_str_freeze(path);
+ rb_hash_aset(expander, orig, path);
+ }
rb_ary_push(ary, path);
}
rb_obj_freeze(ary);
@@ -769,4 +776,6 @@ Init_load()
ruby_dln_librefs = rb_ary_new();
rb_gc_register_mark_object(ruby_dln_librefs);
+ expander = rb_hash_new();
+ rb_gc_register_mark_object(expander);
}

1 comment on commit c229cb4

@now
now commented on c229cb4 Jun 27, 2011

Awesome! This should make a world of difference on Windows, where lstat is excruciatingly slow.

Please sign in to comment.