Skip to content

Commit

Permalink
Process.warmup: precompute strings coderange
Browse files Browse the repository at this point in the history
This both save time for when it will be eventually needed,
and avoid mutating heap pages after a potential fork.

Instrumenting some large Rails app, I've witnessed up to
58% of String instances having their coderange still unknown.
  • Loading branch information
byroot committed Jul 24, 2023
1 parent 1780ad3 commit 9e51e0b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
25 changes: 24 additions & 1 deletion process.c
Original file line number Diff line number Diff line change
Expand Up @@ -8535,6 +8535,27 @@ static VALUE rb_mProcUID;
static VALUE rb_mProcGID;
static VALUE rb_mProcID_Syscall;


static int
proc_warmup_each_object(void *vstart, void *vend, size_t stride, void *data)
{
VALUE v = (VALUE)vstart;
for (; v != (VALUE)vend; v += stride) {
switch (BUILTIN_TYPE(v)) {
case T_STRING:
// precompute the string coderange. This both save time for when it will be
// eventually needed, and avoid mutating heap pages after a potential fork.
rb_enc_str_coderange(v);
break;
default:
break;
}
}

return 0;
}


/*
* call-seq:
* Process.warmup -> true
Expand Down Expand Up @@ -8563,10 +8584,12 @@ proc_warmup(VALUE _)
RB_VM_LOCK_ENTER();
rb_gc_prepare_heap();
RB_VM_LOCK_LEAVE();

rb_objspace_each_objects(proc_warmup_each_object, NULL);

return Qtrue;
}


/*
* Document-module: Process
*
Expand Down
12 changes: 12 additions & 0 deletions test/ruby/test_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2709,4 +2709,16 @@ def test_warmup_run_major_gc_and_compact
assert_equal compact_count + 1, GC.stat(:compact_count)
end;
end

def test_warmup_precompute_string_coderange
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
require 'objspace'
begin;
obj = "a" * 12
obj.force_encoding(Encoding::BINARY)
assert_includes(ObjectSpace.dump(obj), '"coderange":"unknown"')
Process.warmup
assert_includes(ObjectSpace.dump(obj), '"coderange":"7bit"')
end;
end
end

0 comments on commit 9e51e0b

Please sign in to comment.