Skip to content

Commit 7e6561d

Browse files
author
Simon Cruanes
committed
forkjoin: factor out code
1 parent 8cd8bed commit 7e6561d

1 file changed

Lines changed: 24 additions & 51 deletions

File tree

src/forkjoin/moonpool_forkjoin.ml

Lines changed: 24 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -149,70 +149,43 @@ let for_ ?chunk_size n (f : int -> int -> unit) : unit =
149149
()
150150
)
151151

152-
let all_array ?chunk_size (fs : _ array) : _ array =
153-
let len = Array.length fs in
154-
let arr = Array.make len None in
152+
(** Run [g i] for [i] in [0..n-1], in parallel via {!for_}, filling a length-[n]
153+
array of results. Each slot is [Some _] once [g] returns; callers unwrap via
154+
{!unwrap_}. *)
155+
let fill_ ?chunk_size n (g : int -> 'a) : 'a option array =
156+
let res = Array.make n None in
155157

156-
(* parallel for *)
157-
for_ ?chunk_size len (fun low high ->
158+
for_ ?chunk_size n (fun low high ->
158159
for i = low to high do
159-
let x = fs.(i) () in
160-
arr.(i) <- Some x
160+
res.(i) <- Some (g i)
161161
done);
162162

163-
(* get all results *)
164-
Array.map
165-
(function
166-
| None -> assert false
167-
| Some x -> x)
168-
arr
163+
res
164+
165+
let[@inline] unwrap_ (res : _ option array) i =
166+
match res.(i) with
167+
| None -> assert false
168+
| Some x -> x
169+
170+
let all_array ?chunk_size (fs : _ array) : _ array =
171+
let n = Array.length fs in
172+
let res = fill_ ?chunk_size n (fun i -> fs.(i) ()) in
173+
Array.init n (unwrap_ res)
169174

170175
let all_list ?chunk_size fs : _ list =
171176
Array.to_list @@ all_array ?chunk_size @@ Array.of_list fs
172177

173178
let all_init ?chunk_size n f : _ list =
174-
let arr = Array.make n None in
175-
176-
for_ ?chunk_size n (fun low high ->
177-
for i = low to high do
178-
let x = f i in
179-
arr.(i) <- Some x
180-
done);
181-
182-
(* get all results *)
183-
List.init n (fun i ->
184-
match arr.(i) with
185-
| None -> assert false
186-
| Some x -> x)
179+
let res = fill_ ?chunk_size n f in
180+
List.init n (unwrap_ res)
187181

188182
let map_array ?chunk_size f arr : _ array =
189183
let n = Array.length arr in
190-
let res = Array.make n None in
191-
192-
for_ ?chunk_size n (fun low high ->
193-
for i = low to high do
194-
res.(i) <- Some (f arr.(i))
195-
done);
196-
197-
(* get all results *)
198-
Array.map
199-
(function
200-
| None -> assert false
201-
| Some x -> x)
202-
res
184+
let res = fill_ ?chunk_size n (fun i -> f arr.(i)) in
185+
Array.init n (unwrap_ res)
203186

204187
let map_list ?chunk_size f (l : _ list) : _ list =
205188
let arr = Array.of_list l in
206189
let n = Array.length arr in
207-
let res = Array.make n None in
208-
209-
for_ ?chunk_size n (fun low high ->
210-
for i = low to high do
211-
res.(i) <- Some (f arr.(i))
212-
done);
213-
214-
(* get all results *)
215-
List.init n (fun i ->
216-
match res.(i) with
217-
| None -> assert false
218-
| Some x -> x)
190+
let res = fill_ ?chunk_size n (fun i -> f arr.(i)) in
191+
List.init n (unwrap_ res)

0 commit comments

Comments
 (0)