From 2c3c6c96cfc31eb387c643990375e6e1d67b409d Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sat, 16 May 2020 17:37:28 +0900 Subject: [PATCH] Defer initialization Defer initialization of extension libraries, loading prelude files and requiring files, and skip if dump options are given. --- inits.c | 4 ++++ ruby.c | 41 +++++++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/inits.c b/inits.c index 92cec6f4b4..4e5d65ab85 100644 --- a/inits.c +++ b/inits.c @@ -75,7 +75,11 @@ rb_call_inits(void) // enable builtin loading CALL(builtin); +} +void +rb_call_builtin_inits(void) +{ #define BUILTIN(n) CALL(builtin_##n) BUILTIN(gc); BUILTIN(io); diff --git a/ruby.c b/ruby.c index ba515bf277..922d806aee 100644 --- a/ruby.c +++ b/ruby.c @@ -1481,6 +1481,28 @@ ruby_init_prelude(void) rb_const_remove(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX")); } +void rb_call_builtin_inits(void); + +static void +ruby_opt_init(ruby_cmdline_options_t *opt) +{ + if (opt->dump & dump_exit_bits) return; + + if (opt->features.set & FEATURE_BIT(gems)) { + rb_define_module("Gem"); + if (opt->features.set & FEATURE_BIT(did_you_mean)) { + rb_define_module("DidYouMean"); + } + } + + Init_ext(); /* load statically linked extensions before rubygems */ + rb_call_builtin_inits(); + ruby_init_prelude(); + + ruby_set_script_name(opt->script_name); + require_libraries(&opt->req_list); +} + static int opt_enc_index(VALUE enc_name) { @@ -1851,14 +1873,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) rb_ary_replace(vm->load_path_snapshot, load_path); } } - Init_ext(); /* load statically linked extensions before rubygems */ - if (opt->features.set & FEATURE_BIT(gems)) { - rb_define_module("Gem"); - if (opt->features.set & FEATURE_BIT(did_you_mean)) { - rb_define_module("DidYouMean"); - } - } - ruby_init_prelude(); + if (opt->features.mask & COMPILATION_FEATURES) { VALUE option = rb_hash_new(); #define SET_COMPILE_OPTION(h, o, name) \ @@ -1892,10 +1907,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) } #endif rb_enc_associate(opt->e_script, eenc); - if (!(opt->dump & ~DUMP_BIT(version_v))) { - ruby_set_script_name(opt->script_name); - require_libraries(&opt->req_list); - } + ruby_opt_init(opt); ruby_set_script_name(progname); rb_parser_set_options(parser, opt->do_print, opt->do_loop, opt->do_line, opt->do_split); @@ -2122,10 +2134,7 @@ load_file_internal(VALUE argp_v) if (NIL_P(c)) { argp->f = f = Qnil; } - if (!(opt->dump & ~DUMP_BIT(version_v))) { - ruby_set_script_name(opt->script_name); - require_libraries(&opt->req_list); /* Why here? unnatural */ - } + ruby_opt_init(opt); } if (opt->src.enc.index >= 0) { enc = rb_enc_from_index(opt->src.enc.index);