Skip to content
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changes/share-variable-free-types.polish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Compile values with large object or polymorphic variant types faster and write much smaller `.cmt` and `.cmi` files for them, by sharing the parts of their types that contain no type variable instead of copying them at every use.
64 changes: 64 additions & 0 deletions compiler/ml/ctype.ml
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,70 @@ let generalize ty =
simple_abbrevs () := Mnil;
generalize ty

(* Generalizes the type of a let-bound value like [generalize], but leaves the
parts that contain no type variable at the current level instead of making
them generic. [copy] returns non-generic nodes as they are, so every use of
the value shares those parts instead of copying them (#9265). They are
ordinary non-generic nodes, like the parts of a weak type, so unification
and later generalizations handle them as usual.

A part is variable-free when no [Tvar] or [Tunivar] is reachable from it and
its rows are static. A generic node doesn't count, so that a part left
non-generic never contains a generic node. Its object rows end in [Tnil],
so the mutability cells
of its fields can't be promoted any more: cells are merged only together
with the tails of their rows, and only an open row is promoted. Cached
expansions are dropped from the parts left non-generic, as they may hold
nodes this pass doesn't visit. *)
let generalize_value ty =
simple_abbrevs () := Mnil;
let level = !(current_level ()) in
let memo = Type_hash.create 16 in
let rec variable_free ty =
let ty = repr ty in
match Type_hash.find_opt memo ty with
| Some free -> free
| None -> (
match ty.desc with
| _ when ty.level = generic_level -> false
| Tvar _ | Tunivar _ | Tsubst _ -> false
| Tvariant row when not (static_row row) -> false
| _ ->
(* Cycles count as not variable-free. *)
Type_hash.replace memo ty false;
let free =
try
iter_type_expr
(fun t -> if not (variable_free t) then raise_notrace Exit)
ty;
true
with Exit -> false
in
Type_hash.replace memo ty free;
free)
in
let rec lower ty =
let ty = repr ty in
if ty.level > level && ty.level <> generic_level then (
set_level ty level;
(match ty.desc with
| Tconstr (_, _, abbrev) -> abbrev := Mnil
| _ -> ());
iter_type_expr lower ty)
in
let rec generalize ty =
let ty = repr ty in
if ty.level > level && ty.level <> generic_level then
if variable_free ty then lower ty
else (
set_level ty generic_level;
(match ty.desc with
| Tconstr (_, _, abbrev) -> iter_abbrev generalize !abbrev
| _ -> ());
iter_type_expr generalize ty)
in
generalize ty

(* Generalize the structure and lower the variables *)

let rec generalize_structure var_level ty =
Expand Down
5 changes: 5 additions & 0 deletions compiler/ml/ctype.mli
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ val filter_row_fields :
val generalize : type_expr -> unit
(* Generalize in-place the given type *)

val generalize_value : type_expr -> unit
(** Generalize the type of a let-bound value in place, leaving the parts that
contain no type variable at the current level, so that the uses of the
value share them instead of copying them. *)

val generalize_expansive : Env.t -> type_expr -> unit
(* Generalize the covariant part of a type, making
contravariant branches non-generalizable *)
Expand Down
2 changes: 1 addition & 1 deletion compiler/ml/env.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1939,7 +1939,7 @@ let save_signature_with_imports ?check_exists ~deprecated sg modname filename
List.iter (fun (name, crc) -> prerr_endline name) imports;*)
Btype.cleanup_abbrev ();
Subst.reset_for_saving ();
let sg = Subst.signature (Subst.for_saving Subst.identity) sg in
let sg = Subst.signature (Subst.for_saving_signature Subst.identity) sg in
let flags =
match deprecated with
| Some s -> [Deprecated s]
Expand Down
231 changes: 152 additions & 79 deletions compiler/ml/subst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type t = {
modules: Path.t Path_map.t;
modtypes: (Ident.t, module_type) Tbl.t;
for_saving: bool;
share_variable_free: bool;
(** Copy each variable-free type once per saved signature. *)
}

let identity =
Expand All @@ -39,6 +41,7 @@ let identity =
modules = Path_map.empty;
modtypes = Tbl.empty;
for_saving = false;
share_variable_free = false;
}

let add_type_path id p s = {s with types = Path_map.add id (Path p) s.types}
Expand All @@ -53,6 +56,8 @@ let add_module id p s = add_module_path (Pident id) p s
let add_modtype id ty s = {s with modtypes = Tbl.add id ty s.modtypes}

let for_saving s = {s with for_saving = true}
let for_saving_signature s =
{s with for_saving = true; share_variable_free = true}

let loc s x =
if s.for_saving && not !((Clflags.current ()).keep_locs) then Location.none
Expand Down Expand Up @@ -110,9 +115,64 @@ let to_subst_by_type_function s p =

(* Special type ids for saved signatures *)

(* While a signature is saved, the saved copy of each variable-free type, so
that items sharing such a type share its copy in the .cmi as well (#9265),
and whether the types met so far are variable-free. Saving extends the
substitution with the renamed identifiers of each nested signature, so a
copy is kept with the path substitution it was made with and reused only
with the same one. *)
type saved_copy = {
copy: type_expr;
copy_types: type_replacement Path_map.t;
copy_modules: Path.t Path_map.t;
}

type saved_types = {copies: saved_copy Type_hash.t; free: bool Type_hash.t}

let saved_types_key =
Request_local.new_key (fun () ->
{copies = Type_hash.create 64; free = Type_hash.create 64})

let reset_for_saving () =
let saved = Domain.DLS.get saved_types_key in
Type_hash.reset saved.copies;
Type_hash.reset saved.free;
(Compiler_request_state.current ()).substitution_saved_id <- -1

(* Whether no type variable is reachable from [ty] and its rows are static.
A node already copied in the current copy session hides its description, so
it counts as not variable-free, and the results that depend on it aren't
remembered. Cycles count as not variable-free. *)
let variable_free ty =
let memo = (Domain.DLS.get saved_types_key).free in
let uncertain = ref false in
let rec check ty =
let ty = repr ty in
match Type_hash.find_opt memo ty with
| Some free -> free
| None -> (
match ty.desc with
| Tvar _ | Tunivar _ -> false
| Tsubst _ ->
uncertain := true;
false
| Tvariant row when not (static_row row) -> false
| _ ->
Type_hash.replace memo ty false;
let free =
try
iter_type_expr
(fun t -> if not (check t) then raise_notrace Exit)
ty;
true
with Exit -> false
in
if !uncertain then Type_hash.remove memo ty
else Type_hash.replace memo ty free;
free)
in
check ty

let newpersty desc =
let state = Compiler_request_state.current () in
state.substitution_saved_id <- state.substitution_saved_id - 1;
Expand Down Expand Up @@ -146,87 +206,100 @@ let rec typexp_rec s ty =
| Tvariant row when not (static_row row) ->
ty
*)
| _ ->
let desc = ty.desc in
save_desc ty desc;
let tm = row_of_type ty in
let has_fixed_row =
(not (is_Tconstr ty)) && is_constr_row ~allow_ident:false tm
in
(* Make a stub *)
let ty' = if s.for_saving then newpersty (Tvar None) else newgenvar () in
ty.desc <- Tsubst ty';
ty'.desc <-
(if has_fixed_row then
match tm.desc with
(* PR#7348 *)
| Tconstr (Pdot (m, i, pos), tl, _abbrev) ->
let i' = String.sub i 0 (String.length i - 4) in
Tconstr (type_path s (Pdot (m, i', pos)), tl, ref Mnil)
| _ -> assert false
else
match desc with
| Tconstr (p, args, _abbrev) -> (
let args = List.map (typexp_rec s) args in
match Path_map.find p s.types with
| exception Not_found -> Tconstr (type_path s p, args, ref Mnil)
| Path _ -> Tconstr (type_path s p, args, ref Mnil)
| Type_function {params; body} ->
(!ctype_apply_env_empty params body args).desc)
| Tpackage (p, n, tl) ->
Tpackage (modtype_path s p, n, List.map (typexp_rec s) tl)
| Tobject t1 -> Tobject (typexp_rec s t1)
| Tvariant row -> (
let row = row_repr row in
let more = repr row.row_more in
(* We must substitute in a subtle way *)
(* Tsubst takes a tuple containing the row var and the variant *)
match more.desc with
| Tsubst {desc = Ttuple [_; ty2]} ->
(* This variant type has been already copied *)
ty.desc <- Tsubst ty2;
(* avoid Tlink in the new type *)
Tlink ty2
| _ -> (
let dup =
s.for_saving || more.level = generic_level || static_row row
||
match more.desc with
| Tconstr _ -> true
| _ -> false
in
(* Various cases for the row variable *)
let more' =
match more.desc with
| Tsubst ty -> ty
| Tconstr _ | Tnil -> typexp_rec s more
| Tunivar _ | Tvar _ ->
save_desc more more.desc;
if s.for_saving then newpersty (norm more.desc)
else if dup && is_Tvar more then newgenty more.desc
else more
| _ -> assert false
in
(* Register new type first for recursion *)
more.desc <- Tsubst (newgenty (Ttuple [more'; ty']));
(* Return a new copy *)
let row = copy_row (typexp_rec s) true row (not dup) more' in
match row.row_name with
| Some (p, tl) ->
Tvariant
{
row with
row_name =
(if to_subst_by_type_function s p then None
else Some (type_path s p, tl));
}
| None -> Tvariant row))
| _ ->
(* A graph prepared for persistence must not retain mutable cells
| _ when s.share_variable_free && variable_free ty -> (
let copies = (Domain.DLS.get saved_types_key).copies in
match Type_hash.find_opt copies ty with
| Some {copy; copy_types; copy_modules}
when copy_types == s.types && copy_modules == s.modules ->
copy
| _ ->
let copy = typexp_fresh s ty in
Type_hash.replace copies ty
{copy; copy_types = s.types; copy_modules = s.modules};
copy)
| _ -> typexp_fresh s ty

and typexp_fresh s ty =
let desc = ty.desc in
save_desc ty desc;
let tm = row_of_type ty in
let has_fixed_row =
(not (is_Tconstr ty)) && is_constr_row ~allow_ident:false tm
in
(* Make a stub *)
let ty' = if s.for_saving then newpersty (Tvar None) else newgenvar () in
ty.desc <- Tsubst ty';
ty'.desc <-
(if has_fixed_row then
match tm.desc with
(* PR#7348 *)
| Tconstr (Pdot (m, i, pos), tl, _abbrev) ->
let i' = String.sub i 0 (String.length i - 4) in
Tconstr (type_path s (Pdot (m, i', pos)), tl, ref Mnil)
| _ -> assert false
else
match desc with
| Tconstr (p, args, _abbrev) -> (
let args = List.map (typexp_rec s) args in
match Path_map.find p s.types with
| exception Not_found -> Tconstr (type_path s p, args, ref Mnil)
| Path _ -> Tconstr (type_path s p, args, ref Mnil)
| Type_function {params; body} ->
(!ctype_apply_env_empty params body args).desc)
| Tpackage (p, n, tl) ->
Tpackage (modtype_path s p, n, List.map (typexp_rec s) tl)
| Tobject t1 -> Tobject (typexp_rec s t1)
| Tvariant row -> (
let row = row_repr row in
let more = repr row.row_more in
(* We must substitute in a subtle way *)
(* Tsubst takes a tuple containing the row var and the variant *)
match more.desc with
| Tsubst {desc = Ttuple [_; ty2]} ->
(* This variant type has been already copied *)
ty.desc <- Tsubst ty2;
(* avoid Tlink in the new type *)
Tlink ty2
| _ -> (
let dup =
s.for_saving || more.level = generic_level || static_row row
||
match more.desc with
| Tconstr _ -> true
| _ -> false
in
(* Various cases for the row variable *)
let more' =
match more.desc with
| Tsubst ty -> ty
| Tconstr _ | Tnil -> typexp_rec s more
| Tunivar _ | Tvar _ ->
save_desc more more.desc;
if s.for_saving then newpersty (norm more.desc)
else if dup && is_Tvar more then newgenty more.desc
else more
| _ -> assert false
in
(* Register new type first for recursion *)
more.desc <- Tsubst (newgenty (Ttuple [more'; ty']));
(* Return a new copy *)
let row = copy_row (typexp_rec s) true row (not dup) more' in
match row.row_name with
| Some (p, tl) ->
Tvariant
{
row with
row_name =
(if to_subst_by_type_function s p then None
else Some (type_path s p, tl));
}
| None -> Tvariant row))
| _ ->
(* A graph prepared for persistence must not retain mutable cells
from the live typing graph. Fresh cells still use the enclosing
copy session, so sharing within the saved graph is preserved. *)
copy_type_desc ~fresh_mutability:s.for_saving (typexp_rec s) desc);
ty'
copy_type_desc ~fresh_mutability:s.for_saving (typexp_rec s) desc);
ty'

(*
Always make a copy of the type. If this is not done, type levels
Expand Down
5 changes: 5 additions & 0 deletions compiler/ml/subst.mli
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ val for_saving : t -> t
object-field mutability classes are fresh, while sharing between fields in
the copied result is preserved. *)

val for_saving_signature : t -> t
(** Like [for_saving], but also copies each type that contains no type
variable once, so that the items of the saved signature share the copy.
Use it for one signature between two calls of [reset_for_saving]. *)

val reset_for_saving : unit -> unit

val module_path : t -> Path.t -> Path.t
Expand Down
2 changes: 1 addition & 1 deletion compiler/ml/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5142,7 +5142,7 @@ and type_let ~context ?(check = fun s -> Warnings.Unused_var s)
iter_pattern (fun pat -> generalize_expansive env pat.pat_type) pat)
pat_list exp_list;
List.iter
(fun pat -> iter_pattern (fun pat -> generalize pat.pat_type) pat)
(fun pat -> iter_pattern (fun pat -> generalize_value pat.pat_type) pat)
pat_list;
let l = List.combine pat_list exp_list in
let l =
Expand Down
Loading
Loading