From fa31e1a4189d9d055c7e317da635fd84396d6249 Mon Sep 17 00:00:00 2001 From: normal Date: Wed, 23 May 2018 21:16:56 +0000 Subject: thread_pthread.c: enable thread cache by default Since r62466 ("thread_pthread.c: shorten and fix thread cache implementation"), our thread cache is no longer buggy with programs using fork. This makes significant improvements in vm_thread_alive_check1 and vm_thread_create_join benchmarks and does not introduce regressions. Unlike old thread cache, I've changed the cache to only last 3 seconds since per-thread setup in most programs rarely takes more than a few milliseconds to re-establish things like network connections. This is configurable by changing the THREAD_CACHE_TIME variable. I hope this allows users to simplify their code by removing the need for thread pools in many cases. vm_thread_alive_check1 10.872 0.150 vm_thread_close 1.988 2.027 vm_thread_condvar1 0.751 0.767 vm_thread_condvar2 0.744 0.752 vm_thread_create_join 5.296 2.343 vm_thread_mutex1 1.911 1.892 vm_thread_mutex2 1.902 1.896 vm_thread_mutex3 2.389 2.313 vm_thread_pass 0.271 0.272 vm_thread_pass_flood 0.175 0.179 vm_thread_pipe 0.460 0.436 vm_thread_queue 0.453 0.446 vm_thread_sized_queue 0.547 0.547 vm_thread_sized_queue2 1.417 1.413 vm_thread_sized_queue3 1.410 1.426 vm_thread_sized_queue4 0.787 0.791 Speedup ratio: compare with the result of `trunk' (greater is better) name built vm_thread_alive_check1 72.456 vm_thread_close 0.981 vm_thread_condvar1 0.979 vm_thread_condvar2 0.990 vm_thread_create_join 2.260 vm_thread_mutex1 1.010 vm_thread_mutex2 1.003 vm_thread_mutex3 1.033 vm_thread_pass 0.994 vm_thread_pass_flood 0.980 vm_thread_pipe 1.055 vm_thread_queue 1.016 vm_thread_sized_queue 0.999 vm_thread_sized_queue2 1.003 vm_thread_sized_queue3 0.989 vm_thread_sized_queue4 0.995 [ruby-core:87030] [Feature #14757] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63498 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- thread_pthread.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/thread_pthread.c b/thread_pthread.c index 775c32a6a7..91d7215914 100644 --- a/thread_pthread.c +++ b/thread_pthread.c @@ -432,7 +432,7 @@ native_thread_destroy(rb_thread_t *th) } #ifndef USE_THREAD_CACHE -#define USE_THREAD_CACHE 0 +#define USE_THREAD_CACHE 1 #endif #if USE_THREAD_CACHE @@ -870,10 +870,19 @@ thread_cache_reset(void) } # endif +/* + * number of seconds to cache for, I think 1-5s is sufficient to obviate + * the need for thread pool in many network programs (taking into account + * worst case network latency across the globe) without wasting memory + */ +#ifndef THREAD_CACHE_TIME +# define THREAD_CACHE_TIME 3 +#endif + static rb_thread_t * register_cached_thread_and_wait(rb_nativethread_id_t thread_self_id) { - struct timespec end = { 60, 0 }; + struct timespec end = { THREAD_CACHE_TIME, 0 }; struct cached_thread_entry entry; rb_native_cond_initialize(&entry.cond); -- cgit v1.2.3