Skip to content

Commit

Permalink
speedup of require
Browse files Browse the repository at this point in the history
backported cached load path + custom loaded features index
  • Loading branch information
funny-falcon committed May 7, 2013
1 parent e559b24 commit cf8959c
Show file tree
Hide file tree
Showing 10 changed files with 670 additions and 40 deletions.
27 changes: 27 additions & 0 deletions array.c
Expand Up @@ -295,6 +295,33 @@ rb_ary_frozen_p(VALUE ary)
return Qfalse;
}

/* This can be used to take a snapshot of an array (with
e.g. rb_ary_replace) and check later whether the array has been
modified from the snapshot. The snapshot is cheap, though if
something does modify the array it will pay the cost of copying
it. */
VALUE
rb_ary_dup_of_p(VALUE ary1, VALUE ary2)
{
VALUE *p1, *p2;
long len = RARRAY_LEN(ary1);

if (len != RARRAY_LEN(ary2)) return Qfalse;

p1 = RARRAY_PTR(ary1);
p2 = RARRAY_PTR(ary2);

if (ARY_EMBED_P(ary1) && ARY_EMBED_P(ary2)) {
for (; len; len--, p1++, p2++) {
if (*p1 != *p2) return Qfalse;
}
return Qtrue;
}

if (p1 == p2) return Qtrue;
return Qfalse;
}

static VALUE
ary_alloc(VALUE klass)
{
Expand Down
43 changes: 31 additions & 12 deletions file.c
Expand Up @@ -149,40 +149,60 @@ file_path_convert(VALUE name)
return name;
}

static VALUE
rb_get_path_check(VALUE obj, int level)
static rb_encoding *
check_path_encoding(VALUE str)
{
rb_encoding *enc = rb_enc_get(str);
if (!rb_enc_asciicompat(enc)) {
rb_raise(rb_eEncCompatError, "path name must be ASCII-compatible (%s): %s",
rb_enc_name(enc), RSTRING_PTR(rb_str_inspect(str)));
}
return enc;
}

VALUE
rb_get_path_check_to_string(VALUE obj, int level)
{
VALUE tmp;
ID to_path;
rb_encoding *enc;

if (insecure_obj_p(obj, level)) {
rb_insecure_operation();
}

if (RB_TYPE_P(obj, T_STRING)) {
return obj;
}
CONST_ID(to_path, "to_path");
tmp = rb_check_funcall(obj, to_path, 0, 0);
if (tmp == Qundef) {
tmp = obj;
}
StringValue(tmp);
return tmp;
}

VALUE
rb_get_path_check_convert(VALUE obj, VALUE tmp, int level)
{
tmp = file_path_convert(tmp);
if (obj != tmp && insecure_obj_p(tmp, level)) {
rb_insecure_operation();
}
enc = rb_enc_get(tmp);
if (!rb_enc_asciicompat(enc)) {
tmp = rb_str_inspect(tmp);
rb_raise(rb_eEncCompatError, "path name must be ASCII-compatible (%s): %s",
rb_enc_name(enc), RSTRING_PTR(tmp));
}

check_path_encoding(tmp);
StringValueCStr(tmp);

return rb_str_new4(tmp);
}

static VALUE
rb_get_path_check(VALUE obj, int level)
{
VALUE tmp = rb_get_path_check_to_string(obj, level);
return rb_get_path_check_convert(obj, tmp, level);
}

VALUE
rb_get_path_no_checksafe(VALUE obj)
{
Expand Down Expand Up @@ -3250,7 +3270,6 @@ rb_file_expand_path(VALUE fname, VALUE dname)
VALUE
rb_file_expand_path_fast(VALUE fname, VALUE dname)
{
check_expand_path_args(fname, dname);
return rb_file_expand_path_internal(fname, dname, 0, 0, EXPAND_PATH_BUFFER());
}

Expand Down Expand Up @@ -5241,7 +5260,7 @@ rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level)
rb_raise(rb_eSecurityError, "loading from non-absolute path %s", f);
}

RB_GC_GUARD(load_path) = rb_get_load_path();
RB_GC_GUARD(load_path) = rb_get_expanded_load_path();
if (!load_path) return 0;

fname = rb_str_dup(*filep);
Expand Down Expand Up @@ -5306,7 +5325,7 @@ rb_find_file_safe(VALUE path, int safe_level)
rb_raise(rb_eSecurityError, "loading from non-absolute path %s", f);
}

RB_GC_GUARD(load_path) = rb_get_load_path();
RB_GC_GUARD(load_path) = rb_get_expanded_load_path();
if (load_path) {
long i;

Expand Down
2 changes: 1 addition & 1 deletion hash.c
Expand Up @@ -1087,7 +1087,7 @@ clear_i(VALUE key, VALUE value, VALUE dummy)
*
*/

static VALUE
VALUE
rb_hash_clear(VALUE hash)
{
rb_hash_modify_check(hash);
Expand Down
2 changes: 2 additions & 0 deletions include/ruby/intern.h
Expand Up @@ -56,6 +56,7 @@ VALUE rb_ary_tmp_new(long);
void rb_ary_free(VALUE);
void rb_ary_modify(VALUE);
VALUE rb_ary_freeze(VALUE);
VALUE rb_ary_dup_of_p(VALUE, VALUE);
VALUE rb_ary_aref(int, VALUE*, VALUE);
VALUE rb_ary_subseq(VALUE, long, long);
void rb_ary_store(VALUE, long, VALUE);
Expand Down Expand Up @@ -441,6 +442,7 @@ VALUE rb_hash_lookup(VALUE, VALUE);
VALUE rb_hash_lookup2(VALUE, VALUE, VALUE);
VALUE rb_hash_fetch(VALUE, VALUE);
VALUE rb_hash_aset(VALUE, VALUE, VALUE);
VALUE rb_hash_clear(VALUE);
VALUE rb_hash_delete_if(VALUE);
VALUE rb_hash_delete(VALUE,VALUE);
typedef VALUE rb_hash_update_func(VALUE newkey, VALUE oldkey, VALUE value);
Expand Down
3 changes: 3 additions & 0 deletions internal.h
Expand Up @@ -94,6 +94,8 @@ VALUE rb_home_dir(const char *user, VALUE result);
VALUE rb_realpath_internal(VALUE basedir, VALUE path, int strict);
VALUE rb_file_expand_path_fast(VALUE, VALUE);
VALUE rb_file_expand_path_internal(VALUE, VALUE, int, int, VALUE);
VALUE rb_get_path_check_to_string(VALUE, int);
VALUE rb_get_path_check_convert(VALUE, VALUE, int);
void Init_File(void);

#ifdef _WIN32
Expand All @@ -119,6 +121,7 @@ VALUE rb_iseq_clone(VALUE iseqval, VALUE newcbase);

/* load.c */
VALUE rb_get_load_path(void);
VALUE rb_get_expanded_load_path(void);

/* math.c */
VALUE rb_math_atan2(VALUE, VALUE);
Expand Down

0 comments on commit cf8959c

Please sign in to comment.