ruby-changes:68590
From: Alan <ko1@a...>
Date: Thu, 21 Oct 2021 08:10:17 +0900 (JST)
Subject: [ruby-changes:68590] 265c5ca8b1 (master): Avoid triggering GC while translating threaded code
https://git.ruby-lang.org/ruby.git/commit/?id=265c5ca8b1 From 265c5ca8b1ec599f609ea690bfd41f93f4d38cb1 Mon Sep 17 00:00:00 2001 From: Alan Wu <XrXr@u...> Date: Tue, 22 Sep 2020 18:24:55 -0400 Subject: Avoid triggering GC while translating threaded code --- compile.c | 27 ++++++++++++++++++++------- iseq.c | 1 + 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/compile.c b/compile.c index bb87fa6715..cbcda72949 100644 --- a/compile.c +++ b/compile.c @@ -863,16 +863,19 @@ rb_iseq_translate_threaded_code(rb_iseq_t *iseq) https://github.com/ruby/ruby/blob/trunk/compile.c#L863 #if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE const void * const *table = rb_vm_get_insns_address_table(); VALUE *encoded = (VALUE *)iseq->body->iseq_encoded; + VALUE translated_insns_buf; - unsigned int insn_idx; + unsigned int insn_idx, translated_idx; unsigned int next_ujit_idx = 0; + unsigned int translated_len = 0; bool ujit_disabled = false /*get_cmdline_flag()*/; - for (insn_idx = 0; insn_idx < iseq->body->iseq_size; /* */) - { - int insn = (int)iseq->body->iseq_encoded[insn_idx]; - int len = insn_len(insn); + VALUE *translated_insns = ALLOCV_N(VALUE, translated_insns_buf, iseq->body->iseq_size); + for (insn_idx = 0; insn_idx < iseq->body->iseq_size; /* */) { + int insn = (int)encoded[insn_idx]; + int len = insn_len(insn); + VALUE translated; uint8_t* native_code_ptr = NULL; @@ -881,14 +884,24 @@ rb_iseq_translate_threaded_code(rb_iseq_t *iseq) https://github.com/ruby/ruby/blob/trunk/compile.c#L884 native_code_ptr = ujit_compile_insn(iseq, insn_idx, &next_ujit_idx); if (native_code_ptr) - encoded[insn_idx] = (VALUE)native_code_ptr; + translated = (VALUE)native_code_ptr; else - encoded[insn_idx] = (VALUE)table[insn]; + translated = (VALUE)table[insn]; + translated_insns[translated_len++] = translated; insn_idx += len; } + insn_idx = 0; + for (translated_idx = 0; translated_idx < translated_len; translated_idx++) { + int insn = (int)encoded[insn_idx]; + int len = insn_len(insn); + encoded[insn_idx] = translated_insns[translated_idx]; + insn_idx += len; + } + FL_SET((VALUE)iseq, ISEQ_TRANSLATED); + ALLOCV_END(translated_insns_buf); #endif return COMPILE_OK; } diff --git a/iseq.c b/iseq.c index ca3c551beb..19fd8fcb18 100644 --- a/iseq.c +++ b/iseq.c @@ -163,6 +163,7 @@ static int https://github.com/ruby/ruby/blob/trunk/iseq.c#L163 iseq_extract_values(VALUE *code, size_t pos, iseq_value_itr_t * func, void *data, rb_vm_insns_translator_t * translator) { VALUE insn = translator((void *)code[pos]); + if (insn >= VM_INSTRUCTION_SIZE) rb_bug("invalid insn. translator=%p addr2insn=%p", (void *)translator, (void*)rb_vm_insn_addr2insn2); int len = insn_len(insn); int op_no; const char *types = insn_op_types(insn); -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/