From c25975937bcbdc42a9d51ee1e4b8efc0ab5651d8 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 10 Jul 2023 23:33:32 -0400 Subject: [PATCH] wip: remove one array alloc in `Forkjoin.all_array` this can also work for `map_array`. However the risk is that the first element of the array takes a while to process, during which we have no parallelism at all. --- src/fork_join.ml | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/fork_join.ml b/src/fork_join.ml index ccf9a753..cec6919d 100644 --- a/src/fork_join.ml +++ b/src/fork_join.ml @@ -141,20 +141,18 @@ let for_ ?chunk_size n (f : int -> int -> unit) : unit = let all_array ?chunk_size (fs : _ array) : _ array = let len = Array.length fs in - let arr = Array.make len None in + match len with + | 0 -> [||] + | 1 -> [| fs.(0) () |] + | _ -> + let x0 = fs.(0) () in + let arr = Array.make len x0 in - (* parallel for *) - for_ ?chunk_size len (fun low high -> - for i = low to high do - let x = fs.(i) () in - arr.(i) <- Some x - done); - - (* get all results *) - Array.map - (function - | None -> assert false - | Some x -> x) + for_ ?chunk_size (len - 1) (fun low high -> + for i = low to high do + let x = fs.(i + 1) () in + arr.(i + 1) <- x + done); arr let all_list ?chunk_size fs : _ list =