Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allocate a closure unnecessarily #1836

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ocaml/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
Loading