Skip to content

Commit 1abe481

Browse files
committed
Tweaks to reduce space usage
1 parent a5e27af commit 1abe481

File tree

2 files changed

+39
-57
lines changed

2 files changed

+39
-57
lines changed

lib/picos_std.structured/bundle.ml

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ let[@inline never] returned value child t canceler =
150150
Computation.return child value;
151151
finish t canceler
152152

153+
let[@inline never] plug t thunk child canceler =
154+
match thunk () with
155+
| value -> returned value child t canceler
156+
| exception exn -> raised exn child t canceler
157+
153158
let fork_as_promise_pass (type a) (Bundle r as t : t) thunk (pass : a pass) =
154159
(* The sequence of operations below ensures that nothing is leaked. *)
155160
incr t Backoff.default;
@@ -160,19 +165,10 @@ let fork_as_promise_pass (type a) (Bundle r as t : t) thunk (pass : a pass) =
160165
let canceler = Computation.attach_canceler ~from:bundle ~into:child in
161166
let main =
162167
match pass with
163-
| FLS -> begin
168+
| FLS ->
164169
Fiber.FLS.set fiber flock_key t;
165-
fun fiber ->
166-
match thunk () with
167-
| value -> returned value child (get_flock fiber) canceler
168-
| exception exn -> raised exn child (get_flock fiber) canceler
169-
end
170-
| Arg -> begin
171-
fun _ ->
172-
match thunk () with
173-
| value -> returned value child t canceler
174-
| exception exn -> raised exn child t canceler
175-
end
170+
fun fiber -> plug (get_flock fiber) thunk child canceler
171+
| Arg -> fun _ -> plug t thunk child canceler
176172
in
177173
Fiber.spawn fiber main;
178174
child
@@ -183,36 +179,24 @@ let fork_as_promise_pass (type a) (Bundle r as t : t) thunk (pass : a pass) =
183179
decr t;
184180
raise canceled_exn
185181

186-
let[@inline never] raised_flock exn fiber =
187-
let t = get_flock fiber in
188-
let bt = Printexc.get_raw_backtrace () in
189-
error t exn bt;
190-
decr t
191-
192-
let[@inline never] raised_bundle exn t =
182+
let[@inline never] raised exn t =
193183
error t exn (Printexc.get_raw_backtrace ());
194184
decr t
195185

186+
let[@inline never] plug t thunk =
187+
match thunk () with () -> decr t | exception exn -> raised exn t
188+
196189
let fork_pass (type a) (Bundle r as t : t) thunk (pass : a pass) =
197190
(* The sequence of operations below ensures that nothing is leaked. *)
198191
incr t Backoff.default;
199192
try
200193
let fiber = Fiber.create_packed ~forbid:false r.bundle in
201194
let main =
202195
match pass with
203-
| FLS -> begin
196+
| FLS ->
204197
Fiber.FLS.set fiber flock_key t;
205-
fun fiber ->
206-
match thunk () with
207-
| () -> decr (get_flock fiber)
208-
| exception exn -> raised_flock exn fiber
209-
end
210-
| Arg -> begin
211-
fun _ ->
212-
match thunk () with
213-
| () -> decr t
214-
| exception exn -> raised_bundle exn t
215-
end
198+
fun fiber -> plug (get_flock fiber) thunk
199+
| Arg -> fun _ -> plug t thunk
216200
in
217201
Fiber.spawn fiber main
218202
with canceled_exn ->

lib/picos_std.structured/run.ml

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
11
open Picos
22

3-
let wrap_all t main _ =
4-
if Bundle.is_running t then begin
5-
try main () with exn -> Bundle.error t exn (Printexc.get_raw_backtrace ())
6-
end;
7-
Bundle.decr t
3+
let[@inline never] wrap_all t main =
4+
match main () with
5+
| () -> Bundle.decr t
6+
| exception exn -> Bundle.raised exn t
87

9-
let wrap_any t main _ =
10-
if Bundle.is_running t then begin
11-
match main () with
12-
| () -> Bundle.terminate t
13-
| exception exn -> Bundle.error t exn (Printexc.get_raw_backtrace ())
14-
end;
15-
Bundle.decr t
8+
let[@inline never] wrap_any t main =
9+
match main () with
10+
| () ->
11+
Bundle.terminate t;
12+
Bundle.decr t
13+
| exception exn -> Bundle.raised exn t
1614

17-
let rec spawn (Bundle r as t : Bundle.t) wrap = function
15+
let rec spawn (Bundle r as t : Bundle.t) ~all = function
1816
| [] -> ()
1917
| [ main ] ->
2018
Bundle.unsafe_incr t;
21-
let unused_fake_fiber = Obj.magic () in
22-
wrap t main unused_fake_fiber
19+
if Bundle.is_running t then
20+
if all then wrap_all t main else wrap_any t main
21+
else Bundle.decr t
2322
| main :: mains ->
2423
Bundle.unsafe_incr t;
2524
let fiber = Fiber.create_packed ~forbid:false r.bundle in
2625
(* Note that [Fiber.spawn] checks the cancelation status of the bundle. *)
27-
Fiber.spawn fiber (wrap t main);
28-
spawn t wrap mains
26+
Fiber.spawn fiber (fun _ ->
27+
if Bundle.is_running t then
28+
if all then wrap_all t main else wrap_any t main
29+
else Bundle.decr t);
30+
spawn t ~all mains
2931

30-
let run actions wrap =
31-
Bundle.join_after @@ fun (Bundle _ as t : Bundle.t) ->
32-
try spawn t wrap actions
33-
with exn ->
34-
let bt = Printexc.get_raw_backtrace () in
35-
Bundle.decr t;
36-
Bundle.error t exn bt
32+
let run actions ~all =
33+
Bundle.join_after @@ fun (t : Bundle.t) ->
34+
try spawn t ~all actions with exn -> Bundle.raised exn t
3735

38-
let all actions = run actions wrap_all
39-
let any actions = run actions wrap_any
36+
let all actions = run actions ~all:true
37+
let any actions = run actions ~all:false

0 commit comments

Comments
 (0)