From 84be1bcfae72af2accdf2d19b5804c2d35b39596 Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Thu, 16 Feb 2017 14:06:42 +0100 Subject: [PATCH] PR#7158: Event.sync, Mutex.create, Condition.create cause too many GCs Before, mutexes and condition variables were allocated with caml_alloc_custom and cost factor 1/N with N of a few hundreds or thousands. Hence GCs were triggered every N allocations approximately, which is bad. The motivation for this allocation cost was to cover the possibility that mutexes and condvars consume rare kernel resources. This appears not to be the case in Linux nor in Windows, and is unlikely to be the case in any robust implementation of POSIX threads. Hence this fix sets the cost factor to 0 in allocations of mutexes and condvars. --- Changes | 5 +++++ otherlibs/systhreads/st_stubs.c | 9 +++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Changes b/Changes index f594b22ba57e..2f3a86bd6116 100644 --- a/Changes +++ b/Changes @@ -150,6 +150,11 @@ Next version (4.05.0): `O_KEEPEXEC` flag for `openfile` by symmetry with `O_CLOEXEC`. (Xavier Leroy) +- PR#7158: Event.sync, Mutex.create, Condition.create cause too many GCs. + The fix is to no longer consider mutexes and condition variables + as rare kernel resources. + (Xavier Leroy) + - PR#7264: document the different behaviors of Unix.lockf under POSIX and under Win32. (Xavier Leroy, report by David Allsopp) diff --git a/otherlibs/systhreads/st_stubs.c b/otherlibs/systhreads/st_stubs.c index e1959febe654..cd7daa7cfd76 100644 --- a/otherlibs/systhreads/st_stubs.c +++ b/otherlibs/systhreads/st_stubs.c @@ -749,7 +749,6 @@ CAMLprim value caml_thread_join(value th) /* ML */ /* Mutex operations */ #define Mutex_val(v) (* ((st_mutex *) Data_custom_val(v))) -#define Max_mutex_number 5000 static void caml_mutex_finalize(value wrapper) { @@ -783,7 +782,7 @@ CAMLprim value caml_mutex_new(value unit) /* ML */ value wrapper; st_check_error(st_mutex_create(&mut), "Mutex.create"); wrapper = caml_alloc_custom(&caml_mutex_ops, sizeof(st_mutex *), - 1, Max_mutex_number); + 0, 1); Mutex_val(wrapper) = mut; return wrapper; } @@ -828,7 +827,6 @@ CAMLprim value caml_mutex_try_lock(value wrapper) /* ML */ /* Conditions operations */ #define Condition_val(v) (* (st_condvar *) Data_custom_val(v)) -#define Max_condition_number 5000 static void caml_condition_finalize(value wrapper) { @@ -863,7 +861,7 @@ CAMLprim value caml_condition_new(value unit) /* ML */ value wrapper; st_check_error(st_condvar_create(&cond), "Condition.create"); wrapper = caml_alloc_custom(&caml_condition_ops, sizeof(st_condvar *), - 1, Max_condition_number); + 0, 1); Condition_val(wrapper) = cond; return wrapper; } @@ -900,7 +898,6 @@ CAMLprim value caml_condition_broadcast(value wrapper) /* ML */ /* Thread status blocks */ #define Threadstatus_val(v) (* ((st_event *) Data_custom_val(v))) -#define Max_threadstatus_number 500 static void caml_threadstatus_finalize(value wrapper) { @@ -930,7 +927,7 @@ static value caml_threadstatus_new (void) value wrapper; st_check_error(st_event_create(&ts), "Thread.create"); wrapper = caml_alloc_custom(&caml_threadstatus_ops, sizeof(st_event *), - 1, Max_threadstatus_number); + 0, 1); Threadstatus_val(wrapper) = ts; return wrapper; }