From 9c6deac2d1ea29002f4af33b9c1c15aa2d4c34b7 Mon Sep 17 00:00:00 2001 From: ko1 Date: Thu, 27 Nov 2014 01:56:38 +0000 Subject: [PATCH] * vm_core.h: add rb_thread_t::local_storage_recursive_hash to speed up Thread#[:__recursive_key__] access. [Bug #10511] * thread.c (threadptr_local_aref): add fast path for :__recursive_data__. * thread.c (threadptr_recursive_hash, threadptr_recursive_hash_set): add special accessor for recursive hash. * cont.c: store/restore local_storage_recursive_hash. * vm.c: init and mark local_storage_recursive_hash. * vm_trace.c (rb_threadptr_exec_event_hooks_orig): clear and restore local_storage_recursive_hash directly. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48596 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 19 +++++++++++++++ cont.c | 3 +++ thread.c | 70 ++++++++++++++++++++++++++++++++---------------------- vm.c | 2 ++ vm_core.h | 4 +--- vm_trace.c | 6 +++-- 6 files changed, 70 insertions(+), 34 deletions(-) diff --git a/ChangeLog b/ChangeLog index b2c2af8f28..ee3ac8deae 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,22 @@ +Thu Nov 27 10:51:59 2014 Koichi Sasada + + * vm_core.h: add rb_thread_t::local_storage_recursive_hash + to speed up Thread#[:__recursive_key__] access. + [Bug #10511] + + * thread.c (threadptr_local_aref): add fast path for + :__recursive_data__. + + * thread.c (threadptr_recursive_hash, threadptr_recursive_hash_set): + add special accessor for recursive hash. + + * cont.c: store/restore local_storage_recursive_hash. + + * vm.c: init and mark local_storage_recursive_hash. + + * vm_trace.c (rb_threadptr_exec_event_hooks_orig): clear and restore + local_storage_recursive_hash directly. + Thu Nov 27 07:11:00 2014 Eric Wong * compile.c (iseq_calc_param_size): hoist out of iseq_set_arguments diff --git a/cont.c b/cont.c index 3910ca26b3..10b6297068 100644 --- a/cont.c +++ b/cont.c @@ -459,6 +459,7 @@ cont_init(rb_context_t *cont, rb_thread_t *th) cont->saved_thread.machine.stack_maxsize = th->machine.stack_maxsize; cont->saved_thread.fiber = th->fiber; cont->saved_thread.local_storage = 0; + cont->saved_thread.local_storage_recursive_hash = Qnil; } static rb_context_t * @@ -563,6 +564,7 @@ cont_restore_thread(rb_context_t *cont) th->stack = sth->stack; th->stack_size = sth->stack_size; th->local_storage = sth->local_storage; + th->local_storage_recursive_hash = sth->local_storage_recursive_hash; th->fiber = (rb_fiber_t*)cont; } @@ -1208,6 +1210,7 @@ fiber_init(VALUE fibval, VALUE proc) th->cfp->me = 0; th->tag = 0; th->local_storage = st_init_numtable(); + th->local_storage_recursive_hash = Qnil; th->first_proc = proc; diff --git a/thread.c b/thread.c index a8cc250239..97c4ab903b 100644 --- a/thread.c +++ b/thread.c @@ -2753,15 +2753,25 @@ rb_thread_inspect(VALUE thread) return rb_thread_inspect_msg(thread, 1, 1, 1); } +/* variables for recursive traversals */ +static ID recursive_key; + static VALUE threadptr_local_aref(rb_thread_t *th, ID id) { - st_data_t val; - - if (th->local_storage && st_lookup(th->local_storage, id, &val)) { - return (VALUE)val; + if (id == recursive_key) { + return th->local_storage_recursive_hash; + } + else { + st_data_t val; + + if (th->local_storage && st_lookup(th->local_storage, id, &val)) { + return (VALUE)val; + } + else { + return Qnil; + } } - return Qnil; } VALUE @@ -2843,16 +2853,22 @@ rb_thread_aref(VALUE thread, VALUE key) static VALUE threadptr_local_aset(rb_thread_t *th, ID id, VALUE val) { - if (NIL_P(val)) { + if (id == recursive_key) { + th->local_storage_recursive_hash = val; + return val; + } + else if (NIL_P(val)) { if (!th->local_storage) return Qnil; st_delete_wrap(th->local_storage, id); return Qnil; } - if (!th->local_storage) { - th->local_storage = st_init_numtable(); + else { + if (!th->local_storage) { + th->local_storage = st_init_numtable(); + } + st_insert(th->local_storage, id, val); + return val; } - st_insert(th->local_storage, id, val); - return val; } VALUE @@ -4659,9 +4675,6 @@ rb_thread_shield_destroy(VALUE self) return rb_thread_shield_waiting(self) > 0 ? Qtrue : Qfalse; } -/* variables for recursive traversals */ -static ID recursive_key; - static VALUE ident_hash_new(void) { @@ -4670,6 +4683,18 @@ ident_hash_new(void) return hash; } +static VALUE +threadptr_recursive_hash(rb_thread_t *th) +{ + return th->local_storage_recursive_hash; +} + +static void +threadptr_recursive_hash_set(rb_thread_t *th, VALUE hash) +{ + th->local_storage_recursive_hash = hash; +} + /* * Returns the current "recursive list" used to detect recursion. * This list is a hash table, unique for the current thread and for @@ -4679,12 +4704,13 @@ ident_hash_new(void) static VALUE recursive_list_access(void) { - volatile VALUE hash = rb_thread_local_aref(rb_thread_current(), recursive_key); + rb_thread_t *th = GET_THREAD(); + VALUE hash = threadptr_recursive_hash(th); VALUE sym = ID2SYM(rb_frame_this_func()); VALUE list; if (NIL_P(hash) || !RB_TYPE_P(hash, T_HASH)) { hash = ident_hash_new(); - rb_thread_local_aset(rb_thread_current(), recursive_key, hash); + threadptr_recursive_hash_set(th, hash); list = Qnil; } else { @@ -4697,20 +4723,6 @@ recursive_list_access(void) return list; } -VALUE -rb_threadptr_reset_recursive_data(rb_thread_t *th) -{ - VALUE old = threadptr_local_aref(th, recursive_key); - threadptr_local_aset(th, recursive_key, Qnil); - return old; -} - -void -rb_threadptr_restore_recursive_data(rb_thread_t *th, VALUE old) -{ - threadptr_local_aset(th, recursive_key, old); -} - /* * Returns Qtrue iff obj_id (or the pair ) is already * in the recursion list. diff --git a/vm.c b/vm.c index ddb93f6034..4800211366 100644 --- a/vm.c +++ b/vm.c @@ -2062,6 +2062,7 @@ rb_thread_mark(void *ptr) RUBY_MARK_UNLESS_NULL(th->locking_mutex); rb_mark_tbl(th->local_storage); + RUBY_MARK_UNLESS_NULL(th->local_storage_recursive_hash); if (GET_THREAD() != th && th->machine.stack_start && th->machine.stack_end) { rb_gc_mark_machine_stack(th); @@ -2195,6 +2196,7 @@ th_init(rb_thread_t *th, VALUE self) th->last_status = Qnil; th->waiting_fd = -1; th->root_svar = Qnil; + th->local_storage_recursive_hash = Qnil; #ifdef NON_SCALAR_THREAD_ID th->thread_id_string[0] = '\0'; #endif diff --git a/vm_core.h b/vm_core.h index 37bfc75bcb..95808809c5 100644 --- a/vm_core.h +++ b/vm_core.h @@ -690,6 +690,7 @@ typedef struct rb_thread_struct { /* storage */ st_table *local_storage; + VALUE local_storage_recursive_hash; rb_thread_list_t *join_list; @@ -1142,9 +1143,6 @@ void rb_threadptr_exec_event_hooks_and_pop_frame(struct rb_trace_arg_struct *tra #define EXEC_EVENT_HOOK_AND_POP_FRAME(th_, flag_, self_, id_, klass_, data_) \ EXEC_EVENT_HOOK_ORIG(th_, flag_, self_, id_, klass_, data_, 1) -VALUE rb_threadptr_reset_recursive_data(rb_thread_t *th); -void rb_threadptr_restore_recursive_data(rb_thread_t *th, VALUE old); - RUBY_SYMBOL_EXPORT_BEGIN int rb_thread_check_trap_pending(void); diff --git a/vm_trace.c b/vm_trace.c index 5ee5bcaf4f..4fce3f37e3 100644 --- a/vm_trace.c +++ b/vm_trace.c @@ -329,8 +329,10 @@ rb_threadptr_exec_event_hooks_orig(rb_trace_arg_t *trace_arg, int pop_p) trace_arg->self != rb_mRubyVMFrozenCore /* skip special methods. TODO: remove it. */) { const VALUE errinfo = th->errinfo; const int outer_state = th->state; - const VALUE old_recursive = rb_threadptr_reset_recursive_data(th); + const VALUE old_recursive = th->local_storage_recursive_hash; int state = 0; + + th->local_storage_recursive_hash = Qnil; th->state = 0; th->errinfo = Qnil; @@ -350,7 +352,7 @@ rb_threadptr_exec_event_hooks_orig(rb_trace_arg_t *trace_arg, int pop_p) terminate: th->trace_arg = 0; th->vm->trace_running--; - rb_threadptr_restore_recursive_data(th, old_recursive); + th->local_storage_recursive_hash = old_recursive; if (state) { if (pop_p) {