ruby-changes:2503
From: ko1@a...
Date: 21 Nov 2007 18:06:51 +0900
Subject: [ruby-changes:2503] ko1 - Ruby:r13994 (trunk): * vm.c: fix to recycle thread data (VM stack).
ko1 2007-11-21 18:06:06 +0900 (Wed, 21 Nov 2007) New Revision: 13994 Modified files: trunk/ChangeLog trunk/benchmark/bm_vm3_thread_create_join.rb trunk/thread.c trunk/vm.c Log: * vm.c: fix to recycle thread data (VM stack). * thread.c: ditto. * benchmark/bm_vm3_thread_create_join.rb: add loop count. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=13994&r2=13993 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/thread.c?r1=13994&r2=13993 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/benchmark/bm_vm3_thread_create_join.rb?r1=13994&r2=13993 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/vm.c?r1=13994&r2=13993 Index: ChangeLog =================================================================== --- ChangeLog (revision 13993) +++ ChangeLog (revision 13994) @@ -1,3 +1,11 @@ +Wed Nov 21 18:03:49 2007 Koichi Sasada <ko1@a...> + + * vm.c: fix to recycle thread data (VM stack). + + * thread.c: ditto. + + * benchmark/bm_vm3_thread_create_join.rb: add loop count. + Wed Nov 21 18:02:10 2007 Koichi Sasada <ko1@a...> * benchmark/driver.rb: add path to trunk/lib if driver runner is Index: thread.c =================================================================== --- thread.c (revision 13993) +++ thread.c (revision 13994) @@ -288,6 +288,7 @@ extern void ruby_error_print(void); static VALUE rb_thread_raise(int, VALUE *, rb_thread_t *); +void rb_thread_recycle_stack_release(VALUE *); static int thread_start_func_2(rb_thread_t *th, VALUE *stack_start, VALUE *register_stack_start) @@ -359,6 +360,11 @@ join_th = join_th->join_list_next; } st_delete_wrap(th->vm->living_threads, th->self); + + if (!th->root_fiber) { + rb_thread_recycle_stack_release(th->stack); + th->stack = 0; + } } thread_cleanup_func(th); native_mutex_unlock(&th->vm->global_interpreter_lock); @@ -428,7 +434,6 @@ th->join_list_next = target_th->join_list_head; target_th->join_list_head = th; } - while (target_th->status != THREAD_KILLED) { if (delay == DELAY_INFTY) { sleep_forever(th); Index: vm.c =================================================================== --- vm.c (revision 13993) +++ vm.c (revision 13994) @@ -1521,6 +1521,51 @@ /* Thread */ +#define USE_THREAD_DATA_RECYCLE 1 + +#if USE_THREAD_DATA_RECYCLE +#define RECYCLE_MAX 64 +VALUE *thread_recycle_stack_slot[RECYCLE_MAX]; +int thread_recycle_stack_count = 0; + +static VALUE * +thread_recycle_stack(int size) +{ + if (thread_recycle_stack_count) { + return thread_recycle_stack_slot[--thread_recycle_stack_count]; + } + else { + return ALLOC_N(VALUE, size); + } +} + +#else +#define thread_recycle_stack(size) ALLOC_N(VALUE, (size)) +#endif + +void +rb_thread_recycle_stack_release(VALUE *stack) +{ +#if USE_THREAD_DATA_RECYCLE + if (thread_recycle_stack_count < RECYCLE_MAX) { + thread_recycle_stack_slot[thread_recycle_stack_count++] = stack; + } + else { + ruby_xfree(stack); + } +#else + ruby_xfree(stack); +#endif +} + +static rb_thread_t * +thread_recycle_struct(void) +{ + void *p = ALLOC_N(rb_thread_t, 1); + memset(p, 0, sizeof(rb_thread_t)); + return p; +} + static void thread_free(void *ptr) { @@ -1529,7 +1574,7 @@ if (ptr) { th = ptr; - + if (!th->root_fiber) { RUBY_FREE_UNLESS_NULL(th->stack); } @@ -1619,6 +1664,8 @@ thread_alloc(VALUE klass) { VALUE volatile obj; + //rb_thread_t *th = thread_recycle_struct(); + //obj = Data_Wrap_Struct(klass, rb_thread_mark, thread_free, th); rb_thread_t *th; obj = Data_Make_Struct(klass, rb_thread_t, rb_thread_mark, thread_free, th); @@ -1628,11 +1675,9 @@ static void th_init2(rb_thread_t *th) { - MEMZERO(th, rb_thread_t, 1); - /* allocate thread stack */ th->stack_size = RUBY_VM_THREAD_STACK_SIZE; - th->stack = ALLOC_N(VALUE, th->stack_size); + th->stack = thread_recycle_stack(th->stack_size); th->cfp = (void *)(th->stack + th->stack_size); th->cfp--; @@ -1815,6 +1860,7 @@ /* VM bootstrap: phase 1 */ rb_vm_t *vm = ALLOC(rb_vm_t); rb_thread_t *th = ALLOC(rb_thread_t); + MEMZERO(th, rb_thread_t, 1); vm_init2(vm); ruby_current_vm = vm; Index: benchmark/bm_vm3_thread_create_join.rb =================================================================== --- benchmark/bm_vm3_thread_create_join.rb (revision 13993) +++ benchmark/bm_vm3_thread_create_join.rb (revision 13994) @@ -1,5 +1,5 @@ i=0 -while i<1000 # benchmark loop 3 +while i<100_000 # benchmark loop 3 i+=1 Thread.new{ }.join -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml