Project

General

Profile

Actions

Backport #8048

closed

require() features_index bloats size of ruby heap

Added by tmm1 (Aman Karmani) about 11 years ago. Updated almost 11 years ago.


Description

The new features_index for require() in 2.0.0 creates a lot of extra ARRAY and STRING objects. In a big rails application, there are ~70k extra strings and ~70k extra arrays on the heap after boot.

$ RAILS_ENV=production ruby -e' puts RUBY_VERSION; require "./config/environment"; p [:NUM_LOADED_FEATURES, $LOADED_FEATURES.size]; GC.start; p ObjectSpace.count_objects.select{ |k,v| [:T_STRING, :T_ARRAY, :T_HASH].include? k } '
1.9.3
[:NUM_LOADED_FEATURES, 2933]
{:T_STRING=>291147, :T_ARRAY=>97402, :T_HASH=>9439}

$ RAILS_ENV=production ruby -e' puts RUBY_VERSION; require "./config/environment"; p [:NUM_LOADED_FEATURES, $LOADED_FEATURES.size]; GC.start; p ObjectSpace.count_objects.select{ |k,v| [:T_STRING, :T_ARRAY, :T_HASH].include? k } '
2.0.0
[:NUM_LOADED_FEATURES, 2945]
{:T_STRING=>363183, :T_ARRAY=>167871, :T_HASH=>9640}

The following patch reduces the number of arrays used by the features_index:

diff --git a/load.c b/load.c
index ea22d5b..1868518 100644
--- a/load.c
+++ b/load.c
@@ -186,11 +186,16 @@ features_index_add_single(VALUE short_feature, VALUE offset)
{
VALUE features_index, this_feature_index;
features_index = get_loaded_features_index_raw();

  • if ((this_feature_index = rb_hash_lookup(features_index, short_feature)) == Qnil) {
  • this_feature_index = rb_ary_new();
  • rb_hash_aset(features_index, short_feature, this_feature_index);
  • this_feature_index = rb_hash_lookup(features_index, short_feature);
  • if (this_feature_index == Qnil) {
  •    rb_hash_aset(features_index, short_feature, offset);
    
  • } else if (RB_TYPE_P(this_feature_index, T_FIXNUM)) {
  •    this_feature_index = rb_ary_new3(2, this_feature_index, offset);
    
  •    rb_hash_aset(features_index, short_feature, this_feature_index);
    
  • } else if (RB_TYPE_P(this_feature_index, T_ARRAY)) {
  •    rb_ary_push(this_feature_index, offset);
    
    }
  • rb_ary_push(this_feature_index, offset);
    }

/* Add to the loaded-features index all the required entries for
@@ -389,8 +394,16 @@ rb_feature_p(const char *feature, const char *ext, int rb, int expanded, const c
or ends in '/'. This includes both match forms above, as well
as any distractors, so we may ignore all other entries in features.
*/

  • for (i = 0; this_feature_index != Qnil && i < RARRAY_LEN(this_feature_index); i++) {
  • long index = FIX2LONG(rb_ary_entry(this_feature_index, i));
  • for (i = 0; this_feature_index != Qnil; i++) {
  • long index;
  • if (RB_TYPE_P(this_feature_index, T_ARRAY)) {
  • if (i >= RARRAY_LEN(this_feature_index)) break;
    
  • index = FIX2LONG(rb_ary_entry(this_feature_index, i));
    
  • } else {
  • if (i > 0) break;
    
  • index = FIX2LONG(this_feature_index);
    
  • }
  • v = RARRAY_PTR(features)[index];
    f = StringValuePtr(v);
    if ((n = RSTRING_LEN(v)) < len) continue;

Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #9201: [patch] remove GC overhead for loaded_features_indexClosednobu (Nobuyoshi Nakada)12/03/2013Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0