From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS197226 185.234.217.0/24 X-Spam-Status: No, score=-0.9 required=3.0 tests=AWL,BAYES_00, RCVD_IN_MSPIKE_BL,RCVD_IN_MSPIKE_ZBI,RCVD_IN_XBL,RDNS_NONE,SPF_FAIL, SPF_HELO_FAIL,TO_EQ_FM_DOM_SPF_FAIL shortcircuit=no autolearn=no autolearn_force=no version=3.4.0 Received: from 80x24.org (unknown [185.234.217.249]) by dcvr.yhbt.net (Postfix) with ESMTP id C0ACB1F42D for ; Wed, 16 May 2018 09:07:22 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] vm_trace: implement postponed_jobs as st_table Date: Wed, 16 May 2018 09:07:20 +0000 Message-Id: <20180516090720.9935-1-e@80x24.org> List-Id: st_table allows the use of st_shift to act as an order-preserving queue while allowing fast lookups to prevent duplicate jobs. In typical Ruby apps, this table will only have one entry for gc_finalize_deferred_register. --- vm_core.h | 3 +-- vm_trace.c | 53 +++++++++++++++-------------------------------------- 2 files changed, 16 insertions(+), 40 deletions(-) diff --git a/vm_core.h b/vm_core.h index 74064f7803..27360a146e 100644 --- a/vm_core.h +++ b/vm_core.h @@ -591,8 +591,7 @@ typedef struct rb_vm_struct { struct st_table *ensure_rollback_table; /* postponed_job */ - struct rb_postponed_job_struct *postponed_job_buffer; - int postponed_job_index; + struct st_table *postponed_jobs; int src_encoding_index; diff --git a/vm_trace.c b/vm_trace.c index 01b99ed6b1..0a671e7721 100644 --- a/vm_trace.c +++ b/vm_trace.c @@ -1517,11 +1517,6 @@ Init_vm_trace(void) Init_postponed_job(); } -typedef struct rb_postponed_job_struct { - rb_postponed_job_func_t func; - void *data; -} rb_postponed_job_t; - #define MAX_POSTPONED_JOB 1000 #define MAX_POSTPONED_JOB_SPECIAL_ADDITION 24 @@ -1529,8 +1524,7 @@ static void Init_postponed_job(void) { rb_vm_t *vm = GET_VM(); - vm->postponed_job_buffer = ALLOC_N(rb_postponed_job_t, MAX_POSTPONED_JOB); - vm->postponed_job_index = 0; + vm->postponed_jobs = st_init_numtable(); } enum postponed_job_register_result { @@ -1541,22 +1535,11 @@ enum postponed_job_register_result { static enum postponed_job_register_result postponed_job_register(rb_execution_context_t *ec, rb_vm_t *vm, - unsigned int flags, rb_postponed_job_func_t func, void *data, int max, int expected_index) + unsigned int flags, rb_postponed_job_func_t func, void *data, size_t max) { - rb_postponed_job_t *pjob; - - if (expected_index >= max) return PJRR_FULL; /* failed */ - - if (ATOMIC_CAS(vm->postponed_job_index, expected_index, expected_index+1) == expected_index) { - pjob = &vm->postponed_job_buffer[expected_index]; - } - else { - return PJRR_INTERRUPTED; - } + if (vm->postponed_jobs->num_entries >= max) return PJRR_FULL; - /* unused: pjob->flags = flags; */ - pjob->func = func; - pjob->data = data; + st_add_direct(vm->postponed_jobs, (st_index_t)func, (st_data_t)data); RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(ec); @@ -1572,7 +1555,7 @@ rb_postponed_job_register(unsigned int flags, rb_postponed_job_func_t func, void rb_vm_t *vm = rb_ec_vm_ptr(ec); begin: - switch (postponed_job_register(ec, vm, flags, func, data, MAX_POSTPONED_JOB, vm->postponed_job_index)) { + switch (postponed_job_register(ec, vm, flags, func, data, MAX_POSTPONED_JOB )) { case PJRR_SUCESS : return 1; case PJRR_FULL : return 0; case PJRR_INTERRUPTED: goto begin; @@ -1586,19 +1569,13 @@ rb_postponed_job_register_one(unsigned int flags, rb_postponed_job_func_t func, { rb_execution_context_t *ec = GET_EC(); rb_vm_t *vm = rb_ec_vm_ptr(ec); - rb_postponed_job_t *pjob; - int i, index; begin: - index = vm->postponed_job_index; - for (i=0; ipostponed_job_buffer[i]; - if (pjob->func == func) { - RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(ec); - return 2; - } + if (st_lookup(vm->postponed_jobs, (st_data_t)func, 0)) { + RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(ec); + return 2; } - switch (postponed_job_register(ec, vm, flags, func, data, MAX_POSTPONED_JOB + MAX_POSTPONED_JOB_SPECIAL_ADDITION, index)) { + switch (postponed_job_register(ec, vm, flags, func, data, MAX_POSTPONED_JOB + MAX_POSTPONED_JOB_SPECIAL_ADDITION)) { case PJRR_SUCESS : return 1; case PJRR_FULL : return 0; case PJRR_INTERRUPTED: goto begin; @@ -1620,12 +1597,12 @@ rb_postponed_job_flush(rb_vm_t *vm) { EC_PUSH_TAG(ec); if (EC_EXEC_TAG() == TAG_NONE) { - int index; - while ((index = vm->postponed_job_index) > 0) { - if (ATOMIC_CAS(vm->postponed_job_index, index, index-1) == index) { - rb_postponed_job_t *pjob = &vm->postponed_job_buffer[index-1]; - (*pjob->func)(pjob->data); - } + st_data_t k, v; + while (st_shift(vm->postponed_jobs, &k, &v)) { + rb_postponed_job_func_t func = (rb_postponed_job_func_t)k; + void *arg = (void *)v; + + func(arg); } } EC_POP_TAG(); -- EW