Skip to content

Commit

Permalink
flambda-backend: Don't allocate a closure unnecessarily (#1836)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldfirere authored Sep 19, 2023
1 parent a8f6aae commit 3782152
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions stdlib/iarray.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ external unsafe_set_local : local_ 'a array -> int -> local_ 'a -> unit =
(* Really trusting the inliner here; to get maximum performance, it has to
inline both [unsafe_init_local] *and* [f]. *)
(** Precondition: [l >= 0]. *)
let[@inline always] unsafe_init_local l (local_ f : int -> local_ 'a) = local_
let[@inline always] unsafe_init_local l (local_ f : int -> local_ 'a) =
if l = 0 then
unsafe_of_local_array [||]
exclave_ unsafe_of_local_array [||]
else
(* The design of this function is exceedingly delicate, and is the only way
we can correctly allocate a local array on the stack via mutation. We
Expand All @@ -120,17 +120,17 @@ let[@inline always] unsafe_init_local l (local_ f : int -> local_ 'a) = local_
function, and why it's not tail-recursive; if it were tail-recursive,
then we wouldn't have anywhere to put the array elements during the whole
process. *)
let rec go i = local_ begin
let rec go ~l ~f i = local_ begin
let x = f i in
if i = l - 1 then
make_mutable_local l x
else begin
let res = go (i+1) in
let res = go ~l ~f (i+1) in
unsafe_set_local res i x;
res
end
end in
unsafe_of_local_array (go 0)
exclave_ unsafe_of_local_array (go ~l ~f 0)

(* The implementation is copied from [Array] so that [f] can be [local_] *)
let init l (local_ f) =
Expand Down

0 comments on commit 3782152

Please sign in to comment.