From c79ab68d378cd2ebde4013e2204e0d365fce0b3e Mon Sep 17 00:00:00 2001 From: ko1 Date: Wed, 21 Nov 2007 09:06:06 +0000 Subject: * vm.c: fix to recycle thread data (VM stack). * thread.c: ditto. * benchmark/bm_vm3_thread_create_join.rb: add loop count. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13994 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 +++++ benchmark/bm_vm3_thread_create_join.rb | 2 +- thread.c | 7 ++++- vm.c | 54 +++++++++++++++++++++++++++++++--- 4 files changed, 65 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 81ffa49765..f76a865519 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Wed Nov 21 18:03:49 2007 Koichi Sasada + + * 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 * benchmark/driver.rb: add path to trunk/lib if driver runner is diff --git a/benchmark/bm_vm3_thread_create_join.rb b/benchmark/bm_vm3_thread_create_join.rb index c459242b0e..325a66d587 100644 --- a/benchmark/bm_vm3_thread_create_join.rb +++ b/benchmark/bm_vm3_thread_create_join.rb @@ -1,5 +1,5 @@ i=0 -while i<1000 # benchmark loop 3 +while i<100_000 # benchmark loop 3 i+=1 Thread.new{ }.join diff --git a/thread.c b/thread.c index 1655ce0548..6955d86829 100644 --- a/thread.c +++ b/thread.c @@ -288,6 +288,7 @@ thread_cleanup_func(void *th_ptr) 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 @@ thread_start_func_2(rb_thread_t *th, VALUE *stack_start, VALUE *register_stack_s 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 @@ thread_join(rb_thread_t *target_th, double delay) 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); diff --git a/vm.c b/vm.c index 4c298c327a..c4923722e3 100644 --- a/vm.c +++ b/vm.c @@ -1521,6 +1521,51 @@ vm_init2(rb_vm_t *vm) /* 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 @@ thread_free(void *ptr) if (ptr) { th = ptr; - + if (!th->root_fiber) { RUBY_FREE_UNLESS_NULL(th->stack); } @@ -1619,6 +1664,8 @@ static VALUE 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 @@ thread_alloc(VALUE klass) 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 @@ Init_BareVM(void) /* 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; -- cgit v1.2.3