Skip to content

Commit

Permalink
Process.warnump: 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.
  • Loading branch information
byroot committed Jul 24, 2023
1 parent 1780ad3 commit 1a8b3db
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
24 changes: 24 additions & 0 deletions 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,6 +8584,9 @@ 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;
}

Expand Down
8 changes: 8 additions & 0 deletions test/ruby/test_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2709,4 +2709,12 @@ 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
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

0 comments on commit 1a8b3db

Please sign in to comment.