Skip to content
Merged
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
11 changes: 1 addition & 10 deletions analysis/reanalyze/src/arnold.ml
Original file line number Diff line number Diff line change
Expand Up @@ -974,16 +974,7 @@ module Compile = struct
| Texp_field (e, _lid, _desc) -> e |> expression ~ctx
| Texp_record {fields; extended_expression} ->
extended_expression
:: (fields |> Array.to_list
|> List.map
(fun
( _desc,
(record_label_definition : Typedtree.record_label_definition),
_ )
->
match record_label_definition with
| Kept _typeExpr -> None
| Overridden (_loc, e) -> Some e))
:: (fields |> Array.to_list |> List.map (fun (_desc, _lid, e) -> Some e))
|> List.map (expression_opt ~ctx)
|> Command.unordered_sequence
| Texp_setfield (e1, _loc, _desc, e2) ->
Expand Down
6 changes: 3 additions & 3 deletions analysis/reanalyze/src/dead_value.ml
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ let rec collect_expr ~config ~decls ~refs ~file_deps ~cross_file ~direct_callees
~pos_from:loc_from.loc_start
| Texp_record {fields} ->
fields
|> Array.iter (fun (_, record_label_definition, _) ->
match record_label_definition with
| Typedtree.Overridden (_, ({exp_loc} as e)) when exp_loc.loc_ghost ->
|> Array.iter (fun (_, _, (e : Typedtree.expression)) ->
match e with
| {exp_loc} when exp_loc.loc_ghost ->
(* Punned field in OCaml projects has ghost location in expression *)
let e = {e with exp_loc = {exp_loc with loc_ghost = false}} in
collect_expr ~config ~decls ~refs ~file_deps ~cross_file
Expand Down
6 changes: 1 addition & 5 deletions analysis/reanalyze/src/side_effects.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ and expr_opt_no_side_effects eo =
| None -> true
| Some e -> e |> expr_no_side_effects

and field_no_side_effects
((_ld, rld, _) : _ * Typedtree.record_label_definition * _) =
match rld with
| Kept _typeExpr -> true
| Overridden (_lid, e) -> e |> expr_no_side_effects
and field_no_side_effects (_ld, _lid, e) = e |> expr_no_side_effects

and case_no_side_effects : Typedtree.case -> _ =
fun {c_guard; c_rhs} ->
Expand Down
6 changes: 2 additions & 4 deletions analysis/src/process_extra.ml
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,8 @@ let expr ~env ~(extra : extra) (iter : Tast_iterator.iterator)
| Texp_record {fields} ->
add_for_record ~env ~extra ~record_type:expression.exp_type
(fields |> Array.to_list
|> Utils.filter_map (fun (desc, item, opt) ->
match item with
| Typedtree.Overridden (loc, _) -> Some (loc, desc, (), opt)
| _ -> None))
|> List.map (fun (desc, (loc : Longident.t Location.loc), _) ->
(loc, desc, (), false)))
| Texp_constant constant ->
add_loc_item extra expression.exp_loc (Constant constant)
(* Skip unit and list literals *)
Expand Down
1 change: 1 addition & 0 deletions changes/references-omitted-record-fields.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Editor "find references" on a record field no longer lists record literals that leave the optional field out.
1 change: 1 addition & 0 deletions changes/sparse-record-fields.polish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Stop storing a `None` expression for every optional field a record literal leaves out. Modules with many JSX DOM elements now compile up to 10× faster, use far less memory, and get `.cmt` files up to 30× smaller.
8 changes: 3 additions & 5 deletions compiler/ml/printtyped.ml
Original file line number Diff line number Diff line change
Expand Up @@ -738,11 +738,9 @@ and value_binding i ppf x =
pattern (i + 1) ppf x.vb_pat;
expression (i + 1) ppf x.vb_expr

and record_field i ppf = function
| _, Overridden (li, e), opt ->
line i ppf "%a%s\n" fmt_longident li (if opt then "?" else "");
expression (i + 1) ppf e
| _, Kept _, _ -> line i ppf "<kept>"
and record_field i ppf (_, li, e) =
line i ppf "%a\n" fmt_longident li;
expression (i + 1) ppf e

and label_x_expression i ppf (l, e) =
line i ppf "<arg>\n";
Expand Down
11 changes: 4 additions & 7 deletions compiler/ml/rec_check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,7 @@ let rec expression : Env.env -> Typedtree.expression -> Use.t =
| Record_unboxed _ -> fun x -> x
| Record_regular | Record_inlined _ | Record_extension -> Use.guard
in
let field env = function
| _, Kept _, _ -> Use.empty
| _, Overridden (_, e), _ -> expression env e
in
let field env (_, _, e) = expression env e in
Use.join (use (array field env es)) (option expression env eo)
| Texp_ifthenelse (cond, ifso, ifnot) ->
Use.(
Expand Down Expand Up @@ -499,10 +496,10 @@ let check_recursive_bindings valbinds =
in
Ext_list.iter valbinds (fun {vb_expr} ->
match vb_expr.exp_desc with
| Texp_record
{fields = [|(_, Overridden (_, {exp_desc = Texp_function _}), _)|]}
| Texp_function _ ->
| Texp_record {fields = [|(lbl, _, {exp_desc = Texp_function _})|]}
when Array.length lbl.lbl_all = 1 ->
()
| Texp_function _ -> ()
(*TODO: add uncurried function too*)
| _ -> check_recursive_expression ids vb_expr)

Expand Down
6 changes: 1 addition & 5 deletions compiler/ml/tast_iterator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,7 @@ let expr sub {exp_extra; exp_desc; exp_env; _} =
| Texp_construct (_, _, args) -> List.iter (sub.expr sub) args
| Texp_variant (_, expo) -> Option.iter (sub.expr sub) expo
| Texp_record {fields; extended_expression; _} ->
Array.iter
(function
| _, Kept _, _ -> ()
| _, Overridden (_, exp), _ -> sub.expr sub exp)
fields;
Array.iter (fun (_, _, exp) -> sub.expr sub exp) fields;
Option.iter (sub.expr sub) extended_expression
| Texp_field (exp, _, _) -> sub.expr sub exp
| Texp_setfield (exp1, _, _, exp2) ->
Expand Down
5 changes: 1 addition & 4 deletions compiler/ml/tast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,7 @@ let expr sub x =
| Texp_record {fields; representation; extended_expression} ->
let fields =
Array.map
(function
| label, Kept t, o -> (label, Kept t, o)
| label, Overridden (lid, exp), o ->
(label, Overridden (lid, sub.expr sub exp), o))
(fun (label, lid, exp) -> (label, lid, sub.expr sub exp))
fields
in
Texp_record
Expand Down
147 changes: 82 additions & 65 deletions compiler/ml/translcore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ type jsx_props =
| Jsx_props_spread of expression
(** The PPX passes the spread itself when there are no other props *)
| Jsx_props_record of {
fields: (label_description * record_label_definition * bool) array;
fields: (label_description * Longident.t loc * expression) array;
init: expression option;
copy_fields: bool;
(** [transl_record]'s choice: copy the fields of [init] one by one,
Expand Down Expand Up @@ -1017,19 +1017,37 @@ let jsx_children ~multi (lbl : label_description) (child : expression) =
Some children
| _ -> None

(* All labels of the record a [Texp_record] builds. Its [fields] hold only the
written ones. *)
let all_labels
(fields : (label_description * Longident.t loc * expression) array) =
match fields with
| [||] -> [||]
| _ ->
let lbl, _, _ = fields.(0) in
lbl.lbl_all

(* Each label of the record with the expression the literal writes for it, if
any, in position order. *)
let fields_by_position fields =
let written = Array.map (fun _ -> None) (all_labels fields) in
Array.iter (fun (lbl, _, exp) -> written.(lbl.lbl_pos) <- Some exp) fields;
Array.mapi (fun i lbl -> (lbl, written.(i))) (all_labels fields)

(* How [transl_record] builds a record: by copying the fields of [init] one
by one (or with no [init] at all), or by taking a shallow copy of [init]
and setting the other fields. JSX props follow the same rule.
TODO: More strategies
3 + 2 * List.length lbl_expr_list >= size (density) *)
let record_copies_fields
(fields : (label_description * record_label_definition * bool) array)
(fields : (label_description * Longident.t loc * expression) array)
~(init : expression option) =
match init with
| None -> true
| Some _ ->
Array.length fields < 20
&& not (Array.exists (fun (lbl, _, _) -> lbl.lbl_optional) fields)
let lbl_all = all_labels fields in
Array.length lbl_all < 20
&& not (Array.exists (fun lbl -> lbl.lbl_optional) lbl_all)

let jsx_props_of ~multi (props : expression) =
match props.exp_desc with
Expand All @@ -1044,12 +1062,10 @@ let jsx_props_of ~multi (props : expression) =
in
let children =
Array.to_list fields
|> List.find_map (fun ((lbl : label_description), definition, _) ->
match definition with
| Overridden (_, child)
when lbl.lbl_name = "children" && not (is_omitted lbl child) ->
|> List.find_map (fun ((lbl : label_description), _, child) ->
if lbl.lbl_name = "children" && not (is_omitted lbl child) then
Some (lbl, child)
| Overridden _ | Kept _ -> None)
else None)
in
let copy_fields = record_copies_fields fields ~init in
let record children =
Expand All @@ -1058,9 +1074,9 @@ let jsx_props_of ~multi (props : expression) =
let is_array =
Lambda.record_fields_are_array
(Array.map
(fun ((lbl : label_description), _, _) ->
(fun (lbl : label_description) ->
(lbl.lbl_runtime_name, lbl.lbl_optional))
fields)
(all_labels fields))
in
match children with
(* a record with the runtime names "0", "1", ... is an array *)
Expand Down Expand Up @@ -1689,27 +1705,32 @@ and transl_jsx_element loc
(* An optional prop that is statically [undefined] can be left out,
unless it overrides a prop of the spread *)
let has_spread = Option.is_some init && not copy_fields in
let field (lbl : label_description) value =
Some (lbl.lbl_runtime_name, lbl.lbl_optional && not has_spread, value)
in
let written (lbl : label_description) value =
if lbl.lbl_name = "children" && Option.is_some children then None
else field lbl (transl_exp value)
in
let fields =
Array.to_list fields
|> List.mapi (fun i (lbl, definition, _) -> (i, lbl, definition))
|> List.filter_map (fun (i, (lbl : label_description), definition) ->
let field value =
Some
(lbl.lbl_runtime_name, lbl.lbl_optional && not has_spread, value)
in
match definition with
| Overridden _
when lbl.lbl_name = "children" && Option.is_some children ->
None
| Overridden (_, value) -> field (transl_exp value)
| Kept _ when copy_fields ->
field
(prim
~primitive:
(Pfield (i, Lambda.fld_record lbl.lbl_runtime_name))
~args:[var init_id]
loc)
| Kept _ -> None)
match init with
| Some _ when copy_fields ->
(* the props the literal leaves out are copied from [init] *)
fields_by_position fields |> Array.to_list
|> List.filter_map (fun ((lbl : label_description), value) ->
match value with
| Some value -> written lbl value
| None ->
field lbl
(prim
~primitive:
(Pfield
(lbl.lbl_pos, Lambda.fld_record lbl.lbl_runtime_name))
~args:[var init_id]
loc))
| _ ->
Array.to_list fields
|> List.filter_map (fun (lbl, _, value) -> written lbl value)
in
let children = Option.map transl_list children in
let init, spread =
Expand Down Expand Up @@ -1749,15 +1770,15 @@ and transl_jsx_element loc
and transl_record loc env fields repres opt_init_expr =
(* The runtime shape of the record: each field's runtime name, and whether
it is optional. *)
let lbl_all = all_labels fields in
let field_shape () =
fields
|> Array.map (fun ((lbl : Types.label_description), _, _) ->
lbl_all
|> Array.map (fun (lbl : Types.label_description) ->
(lbl.lbl_runtime_name, lbl.lbl_optional))
in
let field_names () =
fields
|> Array.map (fun ((lbl : Types.label_description), _, _) ->
lbl.lbl_runtime_name)
lbl_all
|> Array.map (fun (lbl : Types.label_description) -> lbl.lbl_runtime_name)
in
match (opt_init_expr, repres, fields) with
| _ -> (
Expand All @@ -1767,9 +1788,12 @@ and transl_record loc env fields repres opt_init_expr =
let init_id = Ident.create "init" in
let lv =
Array.mapi
(fun i (lbl, definition, _) ->
match definition with
| Kept _ ->
(fun i (lbl, value) ->
match (value, opt_init_expr) with
| Some expr, _ -> transl_exp expr
(* an optional field the literal leaves out *)
| None, None -> const const_shape_none
| None, Some _ ->
let access =
match repres with
| Record_regular ->
Expand All @@ -1781,14 +1805,12 @@ and transl_record loc env fields repres opt_init_expr =
Pfield
(i + 1, Lambda.fld_record_extension lbl.lbl_runtime_name)
in
prim ~primitive:access ~args:[var init_id] loc
| Overridden (_lid, expr) -> transl_exp expr)
fields
prim ~primitive:access ~args:[var init_id] loc)
(fields_by_position fields)
in
let ll = Array.to_list lv in
let mut =
if Array.exists (fun (lbl, _, _) -> lbl.lbl_mut = Mutable) fields then
Mutable
if Array.exists (fun lbl -> lbl.lbl_mut = Mutable) lbl_all then Mutable
else Immutable
in
let lam =
Expand Down Expand Up @@ -1847,7 +1869,7 @@ and transl_record loc env fields repres opt_init_expr =
| _ -> assert false)
| Record_extension ->
let path =
let label, _, _ = fields.(0) in
let label = lbl_all.(0) in
match label.lbl_res.desc with
| Tconstr (p, _, _) -> p
| _ -> assert false
Expand All @@ -1865,26 +1887,21 @@ and transl_record loc env fields repres opt_init_expr =
(* Take a shallow copy of the init record, then mutate the fields
of the copy *)
let copy_id = Ident.create "newrecord" in
let update_field cont (lbl, definition, _opt) =
match definition with
| Kept _type -> cont
| Overridden (_lid, expr) ->
let upd =
match repres with
| Record_regular ->
Psetfield (lbl.lbl_pos, Lambda.fld_record_set lbl.lbl_runtime_name)
| Record_inlined _ ->
Psetfield
(lbl.lbl_pos, Lambda.fld_record_inline_set lbl.lbl_runtime_name)
| Record_unboxed _ -> assert false
| Record_extension ->
Psetfield
( lbl.lbl_pos + 1,
Lambda.fld_record_extension_set lbl.lbl_runtime_name )
in
seq
(prim ~primitive:upd ~args:[var copy_id; transl_exp expr] loc)
cont
let update_field cont (lbl, _lid, expr) =
let upd =
match repres with
| Record_regular ->
Psetfield (lbl.lbl_pos, Lambda.fld_record_set lbl.lbl_runtime_name)
| Record_inlined _ ->
Psetfield
(lbl.lbl_pos, Lambda.fld_record_inline_set lbl.lbl_runtime_name)
| Record_unboxed _ -> assert false
| Record_extension ->
Psetfield
( lbl.lbl_pos + 1,
Lambda.fld_record_extension_set lbl.lbl_runtime_name )
in
seq (prim ~primitive:upd ~args:[var copy_id; transl_exp expr] loc) cont
in
match opt_init_expr with
| None -> assert false
Expand Down
Loading
Loading