Skip to content

Commit 634c6cb

Browse files
committed
Tweaks to reduce space usage
Turns out OCaml has space leaks related to use of closures.
1 parent 849dea9 commit 634c6cb

File tree

1 file changed

+24
-26
lines changed
  • lib/picos_std.structured

1 file changed

+24
-26
lines changed

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)