ruby-changes:36514
From: ko1 <ko1@a...>
Date: Thu, 27 Nov 2014 10:57:00 +0900 (JST)
Subject: [ruby-changes:36514] ko1:r48596 (trunk): * vm_core.h: add rb_thread_t::local_storage_recursive_hash
ko1 2014-11-27 10:56:38 +0900 (Thu, 27 Nov 2014) New Revision: 48596 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=48596 Log: * 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. Modified files: trunk/ChangeLog trunk/cont.c trunk/thread.c trunk/vm.c trunk/vm_core.h trunk/vm_trace.c Index: ChangeLog =================================================================== --- ChangeLog (revision 48595) +++ ChangeLog (revision 48596) @@ -1,3 +1,22 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Nov 27 10:51:59 2014 Koichi Sasada <ko1@a...> + + * 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 <e@8...> * compile.c (iseq_calc_param_size): hoist out of iseq_set_arguments Index: vm_core.h =================================================================== --- vm_core.h (revision 48595) +++ vm_core.h (revision 48596) @@ -690,6 +690,7 @@ typedef struct rb_thread_struct { https://github.com/ruby/ruby/blob/trunk/vm_core.h#L690 /* 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_p https://github.com/ruby/ruby/blob/trunk/vm_core.h#L1143 #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); Index: thread.c =================================================================== --- thread.c (revision 48595) +++ thread.c (revision 48596) @@ -2753,15 +2753,25 @@ rb_thread_inspect(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2753 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 (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; + 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) https://github.com/ruby/ruby/blob/trunk/thread.c#L2853 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) https://github.com/ruby/ruby/blob/trunk/thread.c#L4675 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) https://github.com/ruby/ruby/blob/trunk/thread.c#L4683 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) https://github.com/ruby/ruby/blob/trunk/thread.c#L4704 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) https://github.com/ruby/ruby/blob/trunk/thread.c#L4723 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 <obj, paired_obj>) is already * in the recursion list. Index: vm_trace.c =================================================================== --- vm_trace.c (revision 48595) +++ vm_trace.c (revision 48596) @@ -329,8 +329,10 @@ rb_threadptr_exec_event_hooks_orig(rb_tr https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L329 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_tr https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L352 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) { Index: cont.c =================================================================== --- cont.c (revision 48595) +++ cont.c (revision 48596) @@ -459,6 +459,7 @@ cont_init(rb_context_t *cont, rb_thread_ https://github.com/ruby/ruby/blob/trunk/cont.c#L459 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) https://github.com/ruby/ruby/blob/trunk/cont.c#L564 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) https://github.com/ruby/ruby/blob/trunk/cont.c#L1210 th->cfp->me = 0; th->tag = 0; th->local_storage = st_init_numtable(); + th->local_storage_recursive_hash = Qnil; th->first_proc = proc; Index: vm.c =================================================================== --- vm.c (revision 48595) +++ vm.c (revision 48596) @@ -2062,6 +2062,7 @@ rb_thread_mark(void *ptr) https://github.com/ruby/ruby/blob/trunk/vm.c#L2062 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) https://github.com/ruby/ruby/blob/trunk/vm.c#L2196 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/