summaryrefslogtreecommitdiff
path: root/ruby.c
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2023-02-24 18:05:36 +0000
committerEric Wong <normal@ruby-lang.org>2023-02-26 20:39:41 +0000
commit35136e1e9c232ad7a03407b992b2e86b6df43f63 (patch)
tree2c60e2d5c9a10089badd41d0f696fe41e70f4a1a /ruby.c
parent6e6992e5db49a238baf290d9b9b521f6b6be5a19 (diff)
downloadruby-35136e1e9c232ad7a03407b992b2e86b6df43f63.tar.gz
reuse open(2) from rb_file_load_ok on POSIX-like system
When loading Ruby source files, we can save the result of successful opens as open(2)/openat(2) are a fairly expensive syscalls. This also avoids a time-of-check-to-time-of-use (TOCTTOU) problem. This reduces open(2) syscalls during `require'; but should be most apparent when users have a small $LOAD_PATH. Users with large $LOAD_PATH will benefit less since there'll be more open(2) failures due to ENOENT. With `strace -c -e openat ruby -e exit' under Linux, this results in a ~14% reduction of openat(2) syscalls (glibc uses openat(2) to implement open(2)). % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 0.00 0.000000 0 296 110 openat 0.00 0.000000 0 254 110 openat Additionally, the introduction of `struct ruby_file_load_state' may make future optimizations more apparent. This change cannot benefit binary (.so) loading since the dlopen(3) API requires a filename and I'm not aware of an alternative that takes a pre-existing FD. In typical situations, Ruby source files outnumber the mount of .so files.
Diffstat (limited to 'ruby.c')
-rw-r--r--ruby.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/ruby.c b/ruby.c
index 8287869d20..f7d0238173 100644
--- a/ruby.c
+++ b/ruby.c
@@ -2539,6 +2539,29 @@ rb_parser_load_file(VALUE parser, VALUE fname_v)
return load_file(parser, fname_v, f, 0, &opt);
}
+void *
+rb_parser_load_state(VALUE parser, VALUE fname_v,
+ struct ruby_file_load_state *fls)
+{
+ if (fls->filev != Qfalse) {
+ ruby_cmdline_options_t opt;
+
+ cmdline_options_init(&opt);
+ /* TODO: xflag for DOSISH || __CYGWIN__ */
+ if (fls->is_nonblock) {
+ struct rb_io_t *fptr;
+
+ RB_IO_POINTER(fls->filev, fptr);
+ disable_nonblock(fptr->fd);
+ }
+ if (fls->is_fifo) {
+ rb_io_wait(fls->filev, RB_INT2NUM(RUBY_IO_READABLE), Qnil);
+ }
+ return load_file(parser, fname_v, fls->filev, 0, &opt);
+ }
+ return rb_parser_load_file(parser, fname_v);
+}
+
/*
* call-seq:
* Process.argv0 -> frozen_string