ruby-changes:50474
From: nobu <ko1@a...>
Date: Wed, 28 Feb 2018 14:17:05 +0900 (JST)
Subject: [ruby-changes:50474] nobu:r62606 (trunk): file.c: get rid of useless conversion
nobu 2018-02-28 14:17:01 +0900 (Wed, 28 Feb 2018) New Revision: 62606 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62606 Log: file.c: get rid of useless conversion * file.c (rb_file_s_stat): File.stat does not accept an IO object as trying conversion to path name string first. skip conversion to IO and try stat(2) only. Modified files: trunk/file.c Index: file.c =================================================================== --- file.c (revision 62605) +++ file.c (revision 62606) @@ -1082,6 +1082,17 @@ no_gvl_fstat(void *data) https://github.com/ruby/ruby/blob/trunk/file.c#L1082 return (VALUE)fstat(arg->file.fd, arg->st); } +static int +fstat_without_gvl(int fd, struct stat *st) +{ + no_gvl_stat_data data; + + data.file.fd = fd; + data.st = st; + + return (int)(VALUE)rb_thread_io_blocking_region(no_gvl_fstat, &data, fd); +} + static void * no_gvl_stat(void * data) { @@ -1090,27 +1101,38 @@ no_gvl_stat(void * data) https://github.com/ruby/ruby/blob/trunk/file.c#L1101 } static int -rb_stat(VALUE file, struct stat *st) +stat_without_gvl(const char *path, struct stat *st) { - VALUE tmp; - VALUE result; no_gvl_stat_data data; + data.file.path = path; data.st = st; + + return (int)(VALUE)rb_thread_call_without_gvl(no_gvl_stat, &data, + RUBY_UBF_IO, NULL); +} + +static int +rb_stat(VALUE file, struct stat *st) +{ + VALUE tmp; + int result; + tmp = rb_check_convert_type_with_id(file, T_FILE, "IO", idTo_io); if (!NIL_P(tmp)) { rb_io_t *fptr; GetOpenFile(tmp, fptr); - data.file.fd = fptr->fd; - result = rb_thread_io_blocking_region(no_gvl_fstat, &data, fptr->fd); - return (int)result; + result = fstat_without_gvl(fptr->fd, st); + file = tmp; + } + else { + FilePathValue(file); + file = rb_str_encode_ospath(file); + result = stat_without_gvl(RSTRING_PTR(file), st); } - FilePathValue(file); - file = rb_str_encode_ospath(file); - data.file.path = StringValueCStr(file); - result = (VALUE)rb_thread_call_without_gvl(no_gvl_stat, &data, RUBY_UBF_IO, NULL); - return (int)result; + RB_GC_GUARD(file); + return result; } /* @@ -1130,7 +1152,8 @@ rb_file_s_stat(VALUE klass, VALUE fname) https://github.com/ruby/ruby/blob/trunk/file.c#L1152 struct stat st; FilePathValue(fname); - if (rb_stat(fname, &st) < 0) { + fname = rb_str_encode_ospath(fname); + if (stat_without_gvl(RSTRING_PTR(fname), &st) < 0) { rb_sys_fail_path(fname); } return rb_stat_new(&st); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/