From e8e61f6b307fa5f3017f6368f9921bbce26647d1 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 12 Feb 2024 12:04:36 -0500 Subject: [PATCH] feat: improve task local storage --- src/core/task_local_storage.ml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/core/task_local_storage.ml b/src/core/task_local_storage.ml index 63d86f72..3b84a9c8 100644 --- a/src/core/task_local_storage.ml +++ b/src/core/task_local_storage.ml @@ -20,8 +20,11 @@ type ls_value += Dummy (** Resize array of TLS values *) let[@inline never] resize_ (cur : ls_value array ref) n = + if n > Sys.max_array_length then failwith "too many task local storage keys"; let len = Array.length !cur in - let new_ls = Array.make (max n ((len * 2) + 2)) Dummy in + let new_ls = + Array.make (min Sys.max_array_length (max n ((len * 2) + 2))) Dummy + in Array.blit !cur 0 new_ls 0 len; cur := new_ls @@ -32,7 +35,7 @@ let[@inline] get_cur_ () : ls_value array ref = let get (type a) ((module K) : a key) : a = let cur = get_cur_ () in - if K.offset >= Array.length !cur then resize_ cur K.offset; + if K.offset >= Array.length !cur then resize_ cur (K.offset + 1); match !cur.(K.offset) with | K.V x -> (* common case first *) x | Dummy ->