Skip to content

Commit e4b09fa

Browse files
byrootpeterzhu2118
andcommittedFeb 23, 2022
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@shopify.com>
1 parent 4aa8ffe commit e4b09fa

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed
 

‎load.c

+4
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,10 @@ rb_provide_feature(rb_vm_t *vm, VALUE feature)
625625
rb_str_freeze(feature);
626626

627627
get_loaded_features_index(vm);
628+
// If loaded_features and loaded_features_snapshot share the same backing
629+
// array, pushing into it would cause the whole array to be copied.
630+
// To avoid this we first clear loaded_features_snapshot.
631+
rb_ary_clear(vm->loaded_features_snapshot);
628632
rb_ary_push(features, rb_fstring(feature));
629633
features_index_add(vm, feature, INT2FIX(RARRAY_LEN(features)-1));
630634
reset_loaded_features_snapshot(vm);

0 commit comments

Comments
 (0)
Failed to load comments.