Skip to content

Commit

Permalink
Allow let@ to be inlined
Browse files Browse the repository at this point in the history
This allows a decent optimizing compiler to eliminate the tuple allocation.

Unfortunately, at the time of writing, `ocamlopt` is not such an optimizing
compiler, but Flambda seems to be able to do it.
  • Loading branch information
polytypic committed Jul 21, 2024
1 parent 6adcadc commit c5e4ed8
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/picos_structured/finally.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let[@inline] finally release acquire = (release, acquire)
[release]. Allocations here would mean that e.g. pressing Ctrl-C, i.e.
[SIGINT], at the right moment could mean that [release] would not be called
after [acquire]. *)
let[@inline never] ( let@ ) (release, acquire) body =
let[@inline never] let_at acquire body release =
let x = acquire () in
match body x with
| y ->
Expand All @@ -19,6 +19,8 @@ let[@inline never] ( let@ ) (release, acquire) body =
release x;
raise exn

let[@inline] ( let@ ) (release, acquire) body = let_at acquire body release

type ('a, _) tdt =
| Nothing : ('a, [> `Nothing ]) tdt
| Resource : {
Expand Down Expand Up @@ -55,7 +57,7 @@ let ( let^ ) (release, acquire) body =
end
end
in
( let@ ) (release, acquire) body
let_at acquire body release

let[@inline never] check_no_resource () =
(* In case of cancelation this is not considered an error as the resource was
Expand Down

0 comments on commit c5e4ed8

Please sign in to comment.