Skip to content

Commit

Permalink
thread_pthread.c: enable thread cache by default
Browse files Browse the repository at this point in the history
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
  • Loading branch information
normal committed May 23, 2018
1 parent 8a2aa4e commit fa31e1a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions thread_pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit fa31e1a

Please sign in to comment.