Skip to content

Instantly share code, notes, and snippets.

@shirosaki
Created August 8, 2012 08:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirosaki/3293339 to your computer and use it in GitHub Desktop.
Save shirosaki/3293339 to your computer and use it in GitHub Desktop.
Remove short name expansion only on require and load
diff --git a/file.c b/file.c
index 4050067..4c86c27 100644
--- a/file.c
+++ b/file.c
@@ -2883,7 +2883,7 @@ append_fspath(VALUE result, VALUE fname, char *dir, rb_encoding **enc, rb_encodi
}
static VALUE
-file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result)
+file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, VALUE result, int long_name)
{
const char *s, *b, *fend;
char *buf, *p, *pend, *root;
@@ -2945,7 +2945,7 @@ file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result)
/* specified drive, but not full path */
int same = 0;
if (!NIL_P(dname) && !not_same_drive(dname, s[0])) {
- file_expand_path(dname, Qnil, abs_mode, result);
+ file_expand_path_internal(dname, Qnil, abs_mode, result, long_name);
BUFINIT();
if (has_drive_letter(p) && TOLOWER(p[0]) == TOLOWER(s[0])) {
/* ok, same drive */
@@ -2969,7 +2969,7 @@ file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result)
#endif
else if (!rb_is_absolute_path(s)) {
if (!NIL_P(dname)) {
- file_expand_path(dname, Qnil, abs_mode, result);
+ file_expand_path_internal(dname, Qnil, abs_mode, result, long_name);
rb_enc_associate(result, rb_enc_check(result, fname));
BUFINIT();
p = pend;
@@ -3179,7 +3179,12 @@ file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result)
wstr = ALLOCV_N(WCHAR, v, len);
MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, wstr, len);
if (tmp != result) rb_str_resize(tmp, 0);
- h = FindFirstFileW(wstr, &wfd);
+ if (long_name) {
+ h = FindFirstFileW(wstr, &wfd);
+ }
+ else {
+ h = INVALID_HANDLE_VALUE;
+ }
ALLOCV_END(v);
if (h != INVALID_HANDLE_VALUE) {
size_t wlen;
@@ -3223,6 +3228,18 @@ file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result)
return result;
}
+static VALUE
+file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result)
+{
+ return file_expand_path_internal(fname, dname, abs_mode, result, 1);
+}
+
+static VALUE
+file_expand_path_fast(VALUE fname, VALUE dname, int abs_mode, VALUE result)
+{
+ return file_expand_path_internal(fname, dname, abs_mode, result, 0);
+}
+
#define EXPAND_PATH_BUFFER() rb_usascii_str_new(0, MAXPATHLEN + 2)
#define check_expand_path_args(fname, dname) \
@@ -3232,7 +3249,7 @@ file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result)
static VALUE
file_expand_path_1(VALUE fname)
{
- return file_expand_path(fname, Qnil, 0, EXPAND_PATH_BUFFER());
+ return file_expand_path_fast(fname, Qnil, 0, EXPAND_PATH_BUFFER());
}
VALUE
@@ -3242,6 +3259,13 @@ rb_file_expand_path(VALUE fname, VALUE dname)
return file_expand_path(fname, dname, 0, EXPAND_PATH_BUFFER());
}
+VALUE
+rb_file_expand_path_fast(VALUE fname, VALUE dname)
+{
+ check_expand_path_args(fname, dname);
+ return file_expand_path_fast(fname, dname, 0, EXPAND_PATH_BUFFER());
+}
+
/*
* call-seq:
* File.expand_path(file_name [, dir_string] ) -> abs_file_name
@@ -5250,7 +5274,7 @@ rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level)
RB_GC_GUARD(str) = rb_get_path_check(str, safe_level);
if (RSTRING_LEN(str) == 0) continue;
- file_expand_path(fname, str, 0, tmp);
+ file_expand_path_fast(fname, str, 0, tmp);
if (rb_file_load_ok(RSTRING_PTR(tmp))) {
*filep = copy_path_class(tmp, *filep);
return (int)(j+1);
@@ -5309,7 +5333,7 @@ rb_find_file_safe(VALUE path, int safe_level)
VALUE str = RARRAY_PTR(load_path)[i];
RB_GC_GUARD(str) = rb_get_path_check(str, safe_level);
if (RSTRING_LEN(str) > 0) {
- file_expand_path(path, str, 0, tmp);
+ file_expand_path_fast(path, str, 0, tmp);
f = RSTRING_PTR(tmp);
if (rb_file_load_ok(f)) goto found;
}
diff --git a/internal.h b/internal.h
index 57b485d..fc6e00d 100644
--- a/internal.h
+++ b/internal.h
@@ -105,6 +105,7 @@ VALUE rb_home_dir(const char *user, VALUE result);
VALUE rb_realpath_internal(VALUE basedir, VALUE path, int strict);
void rb_file_const(const char*, VALUE);
int rb_file_load_ok(const char *);
+VALUE rb_file_expand_path_fast(VALUE fname, VALUE dname);
void Init_File(void);
/* gc.c */
diff --git a/load.c b/load.c
index fd5f862..b21d17c 100644
--- a/load.c
+++ b/load.c
@@ -43,7 +43,7 @@ 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);
+ VALUE path = rb_file_expand_path_fast(RARRAY_PTR(load_path)[i], Qnil);
rb_str_freeze(path);
rb_ary_push(ary, path);
}
@@ -233,7 +233,7 @@ rb_feature_provided(const char *feature, const char **loading)
if (*feature == '.' &&
(feature[1] == '/' || strncmp(feature+1, "./", 2) == 0)) {
- fullpath = rb_file_expand_path(rb_str_new2(feature), Qnil);
+ fullpath = rb_file_expand_path_fast(rb_str_new2(feature), Qnil);
feature = RSTRING_PTR(fullpath);
}
if (ext && !strchr(ext, '/')) {

Benchmark

Windows XP SP3 Core 2 Duo 2.4GHz Ramdisk 500M

Bench script: https://github.com/jonforums/measurements/blob/master/workloads/core_require_empty.rb

trunk

V:\measurements>rci bench core_require_empty
ruby 2.0.0dev (2012-08-08 trunk 36657) [i386-mswin32_100]
Rehearsal ------------------------------------------------------
core_require_empty   1.031000  11.969000  13.000000 ( 13.078125)
-------------------------------------------- total: 13.000000sec

                         user     system      total        real
core_require_empty   0.938000  12.203000  13.141000 ( 13.234375)

patched

V:\measurements>rci bench core_require_empty
ruby 2.0.0dev (2012-08-08 trunk 36657) [i386-mswin32_100]
Rehearsal ------------------------------------------------------
core_require_empty   0.688000   2.953000   3.641000 (  3.734375)
--------------------------------------------- total: 3.641000sec

                         user     system      total        real
core_require_empty   0.766000   2.875000   3.641000 (  3.703125)

bench expand_path

@luislavena
Copy link

@shirosaki, this patch applies on top of the one based on fenix or just plain trunk version?

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment