feat: improve task local storage

This commit is contained in:
Simon Cruanes 2024-02-12 12:04:36 -05:00
parent 41b73462dd
commit e8e61f6b30
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4

View file

@ -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 ->