Skip to content

Commit

Permalink
Generate deferred poly bodies in one go
Browse files Browse the repository at this point in the history
Instead of recursively deferring each one and generating it right after.
This improves compiler performance a bit
  • Loading branch information
tjammer committed Oct 31, 2023
1 parent 8385dac commit f52c373
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/monomorph_tree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ type morph_param = {
mainmodule : Path.t;
alloc_lvl : int;
recursion_stack : (string * recurs) list;
gen_poly_bodies : bool;
}

let no_var =
Expand Down Expand Up @@ -1563,7 +1564,7 @@ and morph_seq mk p expr cont =
let p, cont, func = morph_expr { p with ret } cont in
(p, mk (Mseq (expr, cont)) ret, func)

and prep_func ?(gen_body = false) p (usrname, uniq, abs) =
and prep_func p (usrname, uniq, abs) =
(* If the function is concretely typed, we add it to the function list and add
the usercode name to the bound variables. In the polymorphic case, we add
the function to the bound variables, but not to the function list. Instead,
Expand All @@ -1581,11 +1582,12 @@ and prep_func ?(gen_body = false) p (usrname, uniq, abs) =
let alloc = Value alloca in

let kind = cln_kind p abs.func.kind in
if (not gen_body) && is_type_polymorphic ftyp then (
if (not p.gen_poly_bodies) && is_type_polymorphic ftyp then (
let fn = Polymorphic call in
let vars = Vars.add username (Normal { no_var with fn; alloc }) p.vars in
let fn () =
let p, _ = prep_func ~gen_body:true p (usrname, uniq, abs) in
let p = { p with gen_poly_bodies = true } in
let p, _ = prep_func p (usrname, uniq, abs) in
p
in
Hashtbl.add deferredfunc_tbl call fn;
Expand Down Expand Up @@ -1678,7 +1680,7 @@ and prep_func ?(gen_body = false) p (usrname, uniq, abs) =
in
(p, (call, func.kind, ftyp, alloca))

and morph_lambda ?(gen_body = false) mk typ p id abs =
and morph_lambda mk typ p id abs =
let ftyp = cln p typ in

(* TODO fix lambdas for nested modules *)
Expand All @@ -1690,11 +1692,12 @@ and morph_lambda ?(gen_body = false) mk typ p id abs =
let ret = p.ret in

let kind = cln_kind p abs.func.kind in
if (not gen_body) && is_type_polymorphic ftyp then (
if (not p.gen_poly_bodies) && is_type_polymorphic ftyp then (
let fn = Polymorphic name in
let vars = Vars.add name (Normal { no_var with fn }) p.vars in
let genfn () =
let p, _, _ = morph_lambda ~gen_body:true mk typ p id abs in
let p = { p with gen_poly_bodies = true } in
let p, _, _ = morph_lambda mk typ p id abs in
p
in
Hashtbl.add deferredfunc_tbl name genfn;
Expand Down Expand Up @@ -2086,6 +2089,7 @@ let monomorphize ~mname { Typed_tree.externals; items; _ } =
mainmodule = mname;
alloc_lvl = 1;
recursion_stack = [];
gen_poly_bodies = false;
}
in
let p, tree, _ = morph_toplvl param items in
Expand Down

0 comments on commit f52c373

Please sign in to comment.