ruby-changes:74472
From: nagachika <ko1@a...>
Date: Sun, 13 Nov 2022 17:08:48 +0900 (JST)
Subject: [ruby-changes:74472] 012015d762 (ruby_3_1): merge revision(s) b8f0dc59d52266d9fbfc039e2f4b0f727c62baa0: [Backport #18599]
https://git.ruby-lang.org/ruby.git/commit/?id=012015d762 From 012015d762d000966a33fcea5f3069decd9d4007 Mon Sep 17 00:00:00 2001 From: nagachika <nagachika@r...> Date: Sun, 13 Nov 2022 17:07:58 +0900 Subject: merge revision(s) b8f0dc59d52266d9fbfc039e2f4b0f727c62baa0: [Backport #18599] rb_provide_feature: Prevent $LOADED_FEATURES from being copied [Bug #18599] `vm->loaded_features` and `vm->loaded_features_snapshot` both share the same root. When a feature is pushed into `loaded_features`, the sharing is broken and `loaded_features` is copied. So an application requiring 1000 files, will allocate 1000 arrays of increasing size, which is very wasteful. To avoid this, we first clear the snapshot, so that `loaded_features` can directly be pushed into. Co-Authored-By: Peter Zhu <peter.zhu@s...> --- load.c | 4 ++++ 1 file changed, 4 insertions(+) --- load.c | 4 ++++ version.h | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/load.c b/load.c index 812fe2fe93..bc0571124d 100644 --- a/load.c +++ b/load.c @@ -625,6 +625,10 @@ rb_provide_feature(rb_vm_t *vm, VALUE feature) https://github.com/ruby/ruby/blob/trunk/load.c#L625 rb_str_freeze(feature); get_loaded_features_index(vm); + // If loaded_features and loaded_features_snapshot share the same backing + // array, pushing into it would cause the whole array to be copied. + // To avoid this we first clear loaded_features_snapshot. + rb_ary_clear(vm->loaded_features_snapshot); rb_ary_push(features, rb_fstring(feature)); features_index_add(vm, feature, INT2FIX(RARRAY_LEN(features)-1)); reset_loaded_features_snapshot(vm); diff --git a/version.h b/version.h index f226b2832f..231b6d4a09 100644 --- a/version.h +++ b/version.h @@ -11,7 +11,7 @@ https://github.com/ruby/ruby/blob/trunk/version.h#L11 # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR #define RUBY_VERSION_TEENY 3 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR -#define RUBY_PATCHLEVEL 181 +#define RUBY_PATCHLEVEL 182 #define RUBY_RELEASE_YEAR 2022 #define RUBY_RELEASE_MONTH 11 -- cgit v1.2.3 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/