Skip to content

Commit 3f9bd64

Browse files
rleshchinskiyRoman Leshchinskiy
and
Roman Leshchinskiy
authored
Don't copy when resolving aliases in try_modtypes (#143)
We were calling `expand_module_alias` to get the unstrengthened, non-lazy type (potential deep copy), then checking whether it's an ident and then strengthening it, thus throwing most of it away. This patch avoids doing that by working directly on the lazy representation. This substantially reduces the peak heap size (and improves compile times, albeit to a lesser degree) in some cases. Note that ocaml-flambda/ocaml-jst#119 changes this area significantly so I didn't want to refactor too much here. Co-authored-by: Roman Leshchinskiy <[email protected]>
1 parent aba6294 commit 3f9bd64

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

typing/includemod.ml

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,12 @@ and try_modtypes ~in_eq ~loc env ~mark subst mty1 mty2 orig_shape =
487487
| exception Env.Error (Env.Missing_module (_, _, path)) ->
488488
Error Error.(Mt_core(Unbound_module_path path))
489489
| p1 ->
490-
begin match expand_module_alias ~strengthen:false env p1 with
491-
| Error e -> Error (Error.Mt_core e)
492-
| Ok mty1 ->
493-
match strengthened_modtypes ~in_eq ~loc ~aliasable:true env ~mark
494-
subst mty1 p1 mty2 orig_shape
490+
begin match (Env.find_module_lazy p1 env).mdl_type with
491+
| exception Not_found ->
492+
Error (Error.Mt_core (Error.Unbound_module_path p1))
493+
| mty1 ->
494+
match strengthened_modtypes_lazy ~in_eq ~loc ~aliasable:true env
495+
~mark subst mty1 p1 mty2 orig_shape
495496
with
496497
| Ok _ as x -> x
497498
| Error reason -> Error (Error.After_alias_expansion reason)
@@ -627,15 +628,22 @@ and functor_param ~in_eq ~loc env ~mark subst param1 param2 =
627628
| _, _ ->
628629
Error (Error.Incompatible_params (param1, param2)), env, subst
629630

630-
and strengthened_modtypes ~in_eq ~loc ~aliasable env ~mark
631+
and strengthened_modtypes_lazy ~in_eq ~loc ~aliasable env ~mark
631632
subst mty1 path1 mty2 shape =
633+
let open Subst.Lazy in
632634
match mty1, mty2 with
633-
| Mty_ident p1, Mty_ident p2 when equal_modtype_paths env p1 subst p2 ->
635+
| MtyL_ident p1, Mty_ident p2 when equal_modtype_paths env p1 subst p2 ->
634636
Ok (Tcoerce_none, shape)
635637
| _, _ ->
636-
let mty1 = Mtype.strengthen ~aliasable env mty1 path1 in
638+
let mty1 = Mtype.strengthen_lazy ~aliasable env mty1 path1 in
639+
let mty1 = Subst.Lazy.force_modtype mty1 in
637640
modtypes ~in_eq ~loc env ~mark subst mty1 mty2 shape
638641

642+
and strengthened_modtypes ~in_eq ~loc ~aliasable env ~mark
643+
subst mty1 path1 mty2 shape =
644+
strengthened_modtypes_lazy ~in_eq ~loc ~aliasable env ~mark
645+
subst (Subst.Lazy.of_modtype mty1) path1 mty2 shape
646+
639647
and strengthened_module_decl ~loc ~aliasable env ~mark
640648
subst md1 path1 md2 shape =
641649
match md1.md_type, md2.md_type with

typing/mtype.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ val scrape_for_type_of:
3333
val freshen: scope:int -> module_type -> module_type
3434
(* Return an alpha-equivalent copy of the given module type
3535
where bound identifiers are fresh. *)
36+
val strengthen_lazy: aliasable:bool -> Env.t -> Subst.Lazy.modtype -> Path.t -> Subst.Lazy.modtype
3637
val strengthen: aliasable:bool -> Env.t -> module_type -> Path.t -> module_type
3738
(* Strengthen abstract type components relative to the
3839
given path. *)

0 commit comments

Comments
 (0)