mirror of
https://github.com/c-cube/moonpool.git
synced 2025-12-06 11:15:38 -05:00
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.
This commit is contained in:
parent
e6a2afb2ba
commit
c25975937b
1 changed files with 11 additions and 13 deletions
|
|
@ -141,20 +141,18 @@ let for_ ?chunk_size n (f : int -> int -> unit) : unit =
|
||||||
|
|
||||||
let all_array ?chunk_size (fs : _ array) : _ array =
|
let all_array ?chunk_size (fs : _ array) : _ array =
|
||||||
let len = Array.length fs in
|
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 - 1) (fun low high ->
|
||||||
for_ ?chunk_size len (fun low high ->
|
|
||||||
for i = low to high do
|
for i = low to high do
|
||||||
let x = fs.(i) () in
|
let x = fs.(i + 1) () in
|
||||||
arr.(i) <- Some x
|
arr.(i + 1) <- x
|
||||||
done);
|
done);
|
||||||
|
|
||||||
(* get all results *)
|
|
||||||
Array.map
|
|
||||||
(function
|
|
||||||
| None -> assert false
|
|
||||||
| Some x -> x)
|
|
||||||
arr
|
arr
|
||||||
|
|
||||||
let all_list ?chunk_size fs : _ list =
|
let all_list ?chunk_size fs : _ list =
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue