Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
file_load_ok refactoring
diff --git a/Makefile.in b/Makefile.in
index a3040b6..e3c5d05 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -12,6 +12,7 @@ NULL = /dev/null
srcdir = @srcdir@
top_srcdir = $(srcdir)
hdrdir = $(srcdir)/include
+PLATFORM_DIR = win32
CC = @CC@
CPP = @CPP@
@@ -346,6 +347,9 @@ clean-capi distclean-capi realclean-capi:
@echo $(@:-capi=ing) capi
$(Q)$(RMALL) $(CAPIOUT)
+clean-platform:
+ @$(RM) $(PLATFORM_DIR)/.time
+ -$(Q) $(RMDIR) $(PLATFORM_DIR) 2> /dev/null || true
ext/extinit.$(OBJEXT): ext/extinit.c $(SETUP)
$(ECHO) compiling $@
diff --git a/common.mk b/common.mk
index a25c550..733676c 100644
--- a/common.mk
+++ b/common.mk
@@ -24,6 +24,7 @@ EXTCONF = extconf.rb
RBCONFIG = ./.rbconfig.time
LIBRUBY_EXTS = ./.libruby-with-ext.time
REVISION_H = ./.revision.time
+PLATFORM_D = ./$(PLATFORM_DIR)/.time
RDOCOUT = $(EXTOUT)/rdoc
CAPIOUT = doc/capi
ID_H_TARGET = -id.h-
@@ -424,7 +425,7 @@ install-prereq: $(CLEAR_INSTALLED_LIST) PHONY
clear-installed-list: PHONY
@> $(INSTALLED_LIST) set MAKE="$(MAKE)"
-clean: clean-ext clean-local clean-enc clean-golf clean-rdoc clean-capi clean-extout
+clean: clean-ext clean-local clean-enc clean-golf clean-rdoc clean-capi clean-extout clean-platform
clean-local:: PHONY
@$(RM) $(OBJS) $(MINIOBJS) $(MAINOBJ) $(LIBRUBY_A) $(LIBRUBY_SO) $(LIBRUBY) $(LIBRUBY_ALIASES)
@$(RM) $(PROGRAM) $(WPROGRAM) miniruby$(EXEEXT) dmyext.$(OBJEXT) $(ARCHFILE) .*.time
@@ -434,10 +435,11 @@ clean-golf: PHONY
@$(RM) $(GORUBY)$(EXEEXT) $(GOLFOBJS)
clean-rdoc: PHONY
clean-capi: PHONY
+clean-platform: PHONY
clean-extout: PHONY
clean-docs: clean-rdoc clean-capi
-distclean: distclean-ext distclean-local distclean-enc distclean-golf distclean-extout
+distclean: distclean-ext distclean-local distclean-enc distclean-golf distclean-extout distclean-platform
distclean-local:: clean-local
@$(RM) $(MKFILES) yasmdata.rb *.inc
@$(RM) config.cache config.status config.status.lineno $(PRELUDES)
@@ -448,6 +450,7 @@ distclean-golf: clean-golf
distclean-rdoc: PHONY
distclean-capi: PHONY
distclean-extout: clean-extout
+distclean-platform: clean-platform
realclean:: realclean-ext realclean-local realclean-enc realclean-golf realclean-extout
realclean-local:: distclean-local
@@ -572,7 +575,12 @@ dl_os2.$(OBJEXT): {$(VPATH)}dl_os2.c
ia64.$(OBJEXT): {$(VPATH)}ia64.s
$(CC) $(CFLAGS) -c $<
-win32.$(OBJEXT): {$(VPATH)}win32.c $(RUBY_H_INCLUDES)
+$(PLATFORM_D):
+ $(Q) $(MAKEDIRS) $(PLATFORM_DIR)
+ @exit > $@
+
+win32/win32.$(OBJEXT): {$(VPATH)}win32/win32.c $(RUBY_H_INCLUDES) $(PLATFORM_D)
+win32/file.$(OBJEXT): {$(VPATH)}win32/file.c $(RUBY_H_INCLUDES) $(PLATFORM_D)
###
diff --git a/configure.in b/configure.in
index 0407bfe..8555395 100644
--- a/configure.in
+++ b/configure.in
@@ -2620,7 +2620,8 @@ AS_CASE(["$target_os"],
fi
EXPORT_PREFIX=' '
DLDFLAGS="${DLDFLAGS}"' $(DEFFILE)'
- AC_LIBOBJ([win32])
+ AC_LIBOBJ([win32/win32])
+ AC_LIBOBJ([win32/file])
COMMON_LIBS=m
# COMMON_MACROS="WIN32_LEAN_AND_MEAN="
COMMON_HEADERS="winsock2.h windows.h"
diff --git a/file.c b/file.c
index 5417917..38d2ee8 100644
--- a/file.c
+++ b/file.c
@@ -5144,8 +5144,9 @@ rb_path_check(const char *path)
return 1;
}
-static int
-file_load_ok(const char *path)
+#ifndef _WIN32
+int
+rb_file_load_ok(const char *path)
{
int ret = 1;
int fd = rb_cloexec_open(path, O_RDONLY, 0);
@@ -5162,12 +5163,7 @@ file_load_ok(const char *path)
(void)close(fd);
return ret;
}
-
-int
-rb_file_load_ok(const char *path)
-{
- return file_load_ok(path);
-}
+#endif
static int
is_explicit_relative(const char *path)
@@ -5219,7 +5215,7 @@ rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level)
fnlen = RSTRING_LEN(fname);
for (i=0; ext[i]; i++) {
rb_str_cat2(fname, ext[i]);
- if (file_load_ok(RSTRING_PTR(fname))) {
+ if (rb_file_load_ok(RSTRING_PTR(fname))) {
*filep = copy_path_class(fname, *filep);
return (int)(i+1);
}
@@ -5247,7 +5243,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);
- if (file_load_ok(RSTRING_PTR(tmp))) {
+ if (rb_file_load_ok(RSTRING_PTR(tmp))) {
*filep = copy_path_class(tmp, *filep);
return (int)(j+1);
}
@@ -5286,7 +5282,7 @@ rb_find_file_safe(VALUE path, int safe_level)
if (safe_level >= 1 && !fpath_check(path)) {
rb_raise(rb_eSecurityError, "loading from unsafe path %s", f);
}
- if (!file_load_ok(f)) return 0;
+ if (!rb_file_load_ok(f)) return 0;
if (!expanded)
path = copy_path_class(file_expand_path_1(path), path);
return path;
@@ -5307,7 +5303,7 @@ rb_find_file_safe(VALUE path, int safe_level)
if (RSTRING_LEN(str) > 0) {
file_expand_path(path, str, 0, tmp);
f = RSTRING_PTR(tmp);
- if (file_load_ok(f)) goto found;
+ if (rb_file_load_ok(f)) goto found;
}
}
return 0;
diff --git a/win32/Makefile.sub b/win32/Makefile.sub
index 774011c..86d588f 100644
--- a/win32/Makefile.sub
+++ b/win32/Makefile.sub
@@ -122,6 +122,9 @@ PLATFORM = mswin32
!ifdef NTVER
ARCHDEFS = -D_WIN32_WINNT=$(NTVER) $(ARCHDEFS)
!endif
+!if !defined(PLATFORM_DIR)
+PLATFORM_DIR = win32
+!endif
arch = $(ARCH)-$(PLATFORM)
sitearch = $(ARCH)-$(RT)
@@ -225,7 +228,7 @@ LIBS = oldnames.lib user32.lib advapi32.lib shell32.lib ws2_32.lib imagehlp.lib
LIBS = unicows.lib $(LIBS)
!endif
!if !defined(MISSING)
-MISSING = acosh.obj cbrt.obj crypt.obj erf.obj ffs.obj langinfo.obj lgamma_r.obj strlcat.obj strlcpy.obj tgamma.obj win32.obj setproctitle.obj
+MISSING = acosh.obj cbrt.obj crypt.obj erf.obj ffs.obj langinfo.obj lgamma_r.obj strlcat.obj strlcpy.obj tgamma.obj win32/win32.obj win32/file.obj setproctitle.obj
!endif
ARFLAGS = -machine:$(MACHINE) -out:
@@ -286,7 +289,7 @@ DEFAULT_PRELUDES = $(NO_GEM_PRELUDE)
DEFAULT_PRELUDES = $(YES_GEM_PRELUDE)
!endif
-MAKEDIRS = $(MINIRUBY) -run -e mkdir -- -p
+MAKEDIRS = $(BASERUBY) -run -e mkdir -- -p
!if !defined(STACK)
!if "$(ARCH)" == "x64" || "$(ARCH)" == "ia64"
@@ -919,6 +922,10 @@ clean-rdoc distclean-rdoc realclean-rdoc:
clean-capi distclean-capi realclean-capi:
-$(Q)$(RMALL) $(CAPIOUT:/=\)
+clean-platform:
+ $(Q)$(RM) $(PLATFORM_DIR)\.time
+ -$(Q)$(RMDIR) $(PLATFORM_DIR:/=\)
+
clean-ext distclean-ext realclean-ext::
!if "$(EXTS)" != ""
@for %I in ($(EXTS)) \
@@ -992,7 +999,7 @@ $(ruby_pc): $(RBCONFIG)
{$(win_srcdir)/sys}.c.obj:
$(ECHO) compiling $(<:\=/)
$(Q) $(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(COUTFLAG)$@ -c -Tc$(<:\=/)
-{$(srcdir)}.c.obj:
+{$(srcdir)}.c{}.obj:
$(ECHO) compiling $(<:\=/)
$(Q) $(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(COUTFLAG)$@ -c -Tc$(<:\=/)
.c.obj:
@@ -1039,7 +1046,7 @@ enc/unicode/name2ctype.h: {$(srcdir)}enc/unicode/name2ctype.h.blt
$(OBJS): {$(hdrdir)/ruby}win32.h
-dir.$(OBJEXT) win32.$(OBJEXT): {$(srcdir)}win32/dir.h
+dir.$(OBJEXT) win32/win32.$(OBJEXT): {$(srcdir)}win32/dir.h
ext/extinit.obj: ext/extinit.c $(SETUP)
$(ECHO) compiling ext/extinit.c
diff --git a/win32/file.c b/win32/file.c
new file mode 100644
index 0000000..cd89da2
--- /dev/null
+++ b/win32/file.c
@@ -0,0 +1,25 @@
+#include "ruby/ruby.h"
+#include <winbase.h>
+
+int
+rb_file_load_ok(const char *path)
+{
+ int ret = 1;
+ DWORD attr = GetFileAttributes(path);
+ if (attr == INVALID_FILE_ATTRIBUTES ||
+ attr & FILE_ATTRIBUTE_DIRECTORY) {
+ ret = 0;
+ }
+ else {
+ HANDLE h = CreateFile(path, GENERIC_READ,
+ FILE_SHARE_READ | FILE_SHARE_WRITE,
+ NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+ if (h != INVALID_HANDLE_VALUE) {
+ CloseHandle(h);
+ }
+ else {
+ ret = 0;
+ }
+ }
+ return ret;
+}
@shirosaki

This comment has been minimized.

Copy link
Owner Author

shirosaki commented Jan 13, 2012

ruby 2.0.0dev (2012-01-12 trunk 34278) [i386-mingw32]
Windows XP SP3 with mingw32 gcc 4.5.2 (tdm-1)
On Imdisk 1000M

Rails 3.2.0.rc2
Empty rails app start up time.
Did benchmark just after create ramdisk and xcopy ruby and empty rails app for avoiding disk fragment.

Best rails start up time results (seconds)

NOD32 OFF
trunk     9.234
patched   8.437

NOD32 ON
trunk    10.328
patched   8.546

Raw numbers

# trunk with NOD32 OFF
V:\empty>timer ruby script\rails r 'p $".size, $:.size'
741
59
real    9.234
system  3.796
user    5.437

V:\empty>timer ruby script\rails r 'p $".size, $:.size'
741
59
real    9.406
system  3.843
user    5.562

V:\empty>timer ruby script\rails r 'p $".size, $:.size'
741
59
real    9.609
system  4.343
user    5.250


# patched with NOD32 OFF
V:\empty>timer ruby script\rails r 'p $".size, $:.size'
741
59
real    8.437
system  4.062
user    4.359

V:\empty>timer ruby script\rails r 'p $".size, $:.size'
741
59
real    8.468
system  3.875
user    4.593

V:\empty>timer ruby script\rails r 'p $".size, $:.size'
741
59
real    8.453
system  4.171
user    4.281


# trunk with NOD32 ON
V:\empty>timer ruby script\rails r 'p $".size, $:.size'
741
59
real    10.328
system  4.281
user    5.906

V:\empty>timer ruby script\rails r 'p $".size, $:.size'
741
59
real    10.375
system  4.140
user    6.046

V:\empty>timer ruby script\rails r 'p $".size, $:.size'
741
59
real    10.515
system  4.140
user    6.250


# patched with NOD32 ON
V:\empty>timer ruby script\rails r 'p $".size, $:.size'
741
59
real    8.671
system  4.156
user    4.296

V:\empty>timer ruby script\rails r 'p $".size, $:.size'
741
59
real    8.546
system  4.140
user    4.265

V:\empty>timer ruby script\rails r 'p $".size, $:.size'
741
59
real    8.937
system  3.859
user    4.843
@shirosaki

This comment has been minimized.

Copy link
Owner Author

shirosaki commented Jan 13, 2012

file_access.cpp results

https://github.com/jonforums/tma/blob/master/samples/file_access.cpp

# NOD32 OFF
V:\samples>file_access.exe

file exist
access() time:                12.586
open() time:                  34.6613
stat() time:                  56.7891
OpenFile() time:              71.1018
CreateFile() time:            23.055
GetFileAttributesEx() time:   11.4712
GetFileAttributes() time:     8.48702

file not exist
access() time:                14.8119
open() time:                  18.3254
stat() time:                  30.292
OpenFile() time:              294.79
CreateFile() time:            17.1617
GetFileAttributesEx() time:   10.6754
GetFileAttributes() time:     10.3601


# NOD32 ON
V:\samples>file_access.exe

file exist
access() time:                12.8751
open() time:                  59.9309
stat() time:                  84.2359
OpenFile() time:              91.9834
CreateFile() time:            43.595
GetFileAttributesEx() time:   11.6026
GetFileAttributes() time:     8.66345

file not exist
access() time:                15.1348
open() time:                  29.8641
stat() time:                  30.1119
OpenFile() time:              303.787
CreateFile() time:            28.1449
GetFileAttributesEx() time:   10.8105
GetFileAttributes() time:     10.499
@shirosaki

This comment has been minimized.

Copy link
Owner Author

shirosaki commented Feb 9, 2012

On Windows 7 64bit with Microsoft Security Essentials ON

trunk

ruby 2.0.0dev (2012-02-09 trunk 34511) [i386-mingw32]

V:\empty>timer ruby script\rails r 'p $:.size, $".size' -e production
59
765
real    7.831
system  5.257
user    2.511

V:\empty>timer ruby script\rails r 'p $:.size, $".size' -e production
59
765
real    7.815
system  5.210
user    2.589

V:\empty>timer ruby script\rails r 'p $:.size, $".size' -e production
59
765
real    7.831
system  5.116
user    2.667

patched

ruby 2.0.0dev (2012-02-09 trunk 34511) [i386-mingw32]

V:\empty>timer ruby script\rails r 'p $:.size, $".size' -e production
59
766
real    7.612
system  4.882
user    2.714

V:\empty>timer ruby script\rails r 'p $:.size, $".size' -e production
59
766
real    7.644
system  4.898
user    2.714

V:\empty>timer ruby script\rails r 'p $:.size, $".size' -e production
59
766
real    7.644
system  5.148
user    2.464
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.