diff --git a/changes/share-variable-free-types.polish.md b/changes/share-variable-free-types.polish.md new file mode 100644 index 0000000000..96bdcba496 --- /dev/null +++ b/changes/share-variable-free-types.polish.md @@ -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. diff --git a/compiler/ml/ctype.ml b/compiler/ml/ctype.ml index c582d9e8b6..5d3a2d5678 100644 --- a/compiler/ml/ctype.ml +++ b/compiler/ml/ctype.ml @@ -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 = diff --git a/compiler/ml/ctype.mli b/compiler/ml/ctype.mli index 06075c288d..b46113d9df 100644 --- a/compiler/ml/ctype.mli +++ b/compiler/ml/ctype.mli @@ -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 *) diff --git a/compiler/ml/env.ml b/compiler/ml/env.ml index 6278d4c23a..89dc9219f1 100644 --- a/compiler/ml/env.ml +++ b/compiler/ml/env.ml @@ -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] diff --git a/compiler/ml/subst.ml b/compiler/ml/subst.ml index ff62839a2b..e51b303f92 100644 --- a/compiler/ml/subst.ml +++ b/compiler/ml/subst.ml @@ -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 = @@ -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} @@ -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 @@ -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; @@ -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 diff --git a/compiler/ml/subst.mli b/compiler/ml/subst.mli index e04842364a..003740357f 100644 --- a/compiler/ml/subst.mli +++ b/compiler/ml/subst.mli @@ -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 diff --git a/compiler/ml/typecore.ml b/compiler/ml/typecore.ml index 59ce0e57b7..dc2365635c 100644 --- a/compiler/ml/typecore.ml +++ b/compiler/ml/typecore.ml @@ -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 = diff --git a/tests/tests/src/shared_variable_free_types.mjs b/tests/tests/src/shared_variable_free_types.mjs new file mode 100644 index 0000000000..aefd0f431d --- /dev/null +++ b/tests/tests/src/shared_variable_free_types.mjs @@ -0,0 +1,109 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.mjs"; + +let o = { + x: 1, + y: "a" +}; + +function getX(p) { + return p.x; +} + +let a = o.x + o.x | 0; + +let s1 = settableObj; + +let s2 = settableObj; + +function write() { + s1.x = s2.x + 1 | 0; +} + +function toInt(x) { + if (x === "b") { + return 2; + } else if (x === "c") { + return 3; + } else { + return 1; + } +} + +let b = toInt("a") + toInt("a") | 0; + +function even(n) { + if (n === 0) { + return true; + } else { + return odd(n - 1 | 0); + } +} + +function odd(n) { + if (n === 0) { + return false; + } else { + return even(n - 1 | 0); + } +} + +function $$eval(e) { + if (e.TAG === "Int") { + return e._0 + 1 | 0; + } else { + return !e._0; + } +} + +let r = { + contents: undefined +}; + +r.contents = Primitive_option.some(o); + +function local() { + let l = { + z: [ + 1, + 2 + ] + }; + return l.z.length + l.z.length | 0; +} + +let pair = [ + o, + "a" +]; + +let n = o.x + toInt("a") | 0; + +let v = "a"; + +let o2 = o; + +let v2 = "a"; + +export { + o, + getX, + a, + s1, + s2, + write, + v, + toInt, + b, + even, + odd, + $$eval, + r, + local, + pair, + o2, + v2, + n, +} +/* a Not a pure module */ diff --git a/tests/tests/src/shared_variable_free_types.res b/tests/tests/src/shared_variable_free_types.res new file mode 100644 index 0000000000..2b771f9684 --- /dev/null +++ b/tests/tests/src/shared_variable_free_types.res @@ -0,0 +1,55 @@ +/* Uses of let-bound values whose types contain no type variable share those + types instead of copying them (#9265). These uses go through open objects, + settable fields, polymorphic variants, recursion, GADTs and weak types. */ + +let o = {"x": 1, "y": "a"} +let getX = (p: {.."x": int}) => p["x"] +let a = getX(o) + getX(o) + +@val external settable: {@set "x": int} = "settableObj" +let s1 = settable +let s2 = settable +let write = () => s1["x"] = s2["x"] + 1 + +let v: [#a | #b] = #a + +let toInt = (x: [< #a | #b | #c]) => + switch x { + | #a => 1 + | #b => 2 + | #c => 3 + } + +let b = toInt(v) + toInt(v) + +let rec even = n => n == 0 ? true : odd(n - 1) +and odd = n => n == 0 ? false : even(n - 1) + +type rec expr<_> = + | Int(int): expr + | Bool(bool): expr + +let eval = (type a, e: expr): a => + switch e { + | Int(n) => + let k = n + 1 + let k2 = k + k2 + | Bool(b) => + let c = !b + c + } + +let r = ref(None) +let () = r := Some(o) + +let local = () => { + let l = {"z": [1, 2]} + let l1 = l + let l2 = l + Array.length(l1["z"]) + Array.length(l2["z"]) +} + +let pair = (o, v) +let (o2, v2) = pair +let n = o2["x"] + toInt(v2)