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
1 change: 1 addition & 0 deletions changes/jsx-props-skip-label-copy.polish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Build: compile JSX elements without copying every label of their props record, which removed most garbage collection pauses in builds of JSX-heavy projects on several domains.
8 changes: 4 additions & 4 deletions compiler/ml/lambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -801,16 +801,16 @@ and eq_option l1 l2 =

and eq_approx_list ls ls1 = Ext_list.for_all2_no_exn ls ls1 eq_approx

let record_fields_are_array fields =
let runtime_names_are_array runtime_name fields =
let len = Array.length fields in
let rec loop i =
if i = len then true
else
let name, _optional = fields.(i) in
string_of_int i = name && loop (i + 1)
else string_of_int i = runtime_name fields.(i) && loop (i + 1)
in
len <> 0 && loop 0

let record_fields_are_array fields = runtime_names_are_array fst fields

(* The JavaScript value a constant is emitted as by [Lam_compile_const] and
[Js_dump], not its source constructor. A constant of another
representation keeps the dispatch at run time. *)
Expand Down
5 changes: 5 additions & 0 deletions compiler/ml/lambda.mli
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,8 @@ val record_fields_are_array : (string * bool) array -> bool
(** Whether a nonempty record has consecutive field names starting at ["0"],
so it is represented as a JavaScript array. Optionality does not affect
the layout. *)

val runtime_names_are_array : ('a -> string) -> 'a array -> bool
(** [record_fields_are_array] over fields of any shape, given their runtime
name, so that a caller holding the labels of a large record (JSX props)
need not build a copy of them. *)
35 changes: 20 additions & 15 deletions compiler/ml/translcore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1028,11 +1028,14 @@ let all_labels
lbl.lbl_all

(* Each label of the record with the expression the literal writes for it, if
any, in position order. *)
any, in position order. A list rather than an array: [Array.map] over more
than 256 labels (JSX props) forces a minor collection, which stops every
domain. *)
let fields_by_position fields =
let written = Array.map (fun _ -> None) (all_labels fields) in
let lbl_all = all_labels fields in
let written = Array.make (Array.length lbl_all) None in
Array.iter (fun (lbl, _, exp) -> written.(lbl.lbl_pos) <- Some exp) fields;
Array.mapi (fun i lbl -> (lbl, written.(i))) (all_labels fields)
List.init (Array.length lbl_all) (fun i -> (lbl_all.(i), written.(i)))

(* 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]
Expand Down Expand Up @@ -1072,11 +1075,9 @@ let jsx_props_of ~multi (props : expression) =
Some (Jsx_props_record {fields; init; copy_fields; children})
in
let is_array =
Lambda.record_fields_are_array
(Array.map
(fun (lbl : label_description) ->
(lbl.lbl_runtime_name, lbl.lbl_optional))
(all_labels fields))
Lambda.runtime_names_are_array
(fun (lbl : label_description) -> lbl.lbl_runtime_name)
(all_labels fields)
in
match children with
(* a record with the runtime names "0", "1", ... is an array *)
Expand Down Expand Up @@ -1716,7 +1717,7 @@ and transl_jsx_element loc
match init with
| Some _ when copy_fields ->
(* the props the literal leaves out are copied from [init] *)
fields_by_position fields |> Array.to_list
fields_by_position fields
|> List.filter_map (fun ((lbl : label_description), value) ->
match value with
| Some value -> written lbl value
Expand Down Expand Up @@ -1772,9 +1773,14 @@ and transl_record loc env fields repres opt_init_expr =
it is optional. *)
let lbl_all = all_labels fields in
let field_shape () =
lbl_all
|> Array.map (fun (lbl : Types.label_description) ->
(lbl.lbl_runtime_name, lbl.lbl_optional))
(* Filled in place: [Array.map] over more than 256 labels would force a
minor collection. *)
let shape = Array.make (Array.length lbl_all) ("", false) in
Array.iteri
(fun i (lbl : Types.label_description) ->
shape.(i) <- (lbl.lbl_runtime_name, lbl.lbl_optional))
lbl_all;
shape
in
let field_names () =
lbl_all
Expand All @@ -1786,8 +1792,8 @@ and transl_record loc env fields repres opt_init_expr =
(* Allocate new record with given fields (and remaining fields
taken from init_expr if any *)
let init_id = Ident.create "init" in
let lv =
Array.mapi
let ll =
List.mapi
(fun i (lbl, value) ->
match (value, opt_init_expr) with
| Some expr, _ -> transl_exp expr
Expand All @@ -1808,7 +1814,6 @@ and transl_record loc env fields repres opt_init_expr =
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) lbl_all then Mutable
else Immutable
Expand Down
16 changes: 11 additions & 5 deletions compiler/ml/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3069,12 +3069,18 @@ and type_expect_ ?deprecated_context ~context ?(recarg = Rejected)
if fields = [] && repr_opt <> None then ([||], Record_regular)
else raise (Error (loc, env, Empty_record_literal))
in
let written = written_labels label_descriptions lbl_exp_list in
let labels_missing =
Array.to_list label_descriptions
|> List.filter_map (fun lbl ->
if written.(lbl.lbl_pos) || label_is_optional lbl then None
else Some lbl.lbl_name)
(* Records whose labels are all optional, like the hundreds of host
element props in JsxDOM.domProps, can't miss a label: check that
first, without allocating for each label. Records with a required
label, like most component props, take the general path. *)
if Array.for_all label_is_optional label_descriptions then []
else
let written = written_labels label_descriptions lbl_exp_list in
Array.to_list label_descriptions
|> List.filter_map (fun lbl ->
if written.(lbl.lbl_pos) || label_is_optional lbl then None
else Some lbl.lbl_name)
in
if labels_missing <> [] then
raise
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

We've found a bug for you!
/.../fixtures/record_literal_missing_fields_some_optional.res:3:12-17

1 │ type t = {a?: int, b: string, c?: bool, mutable d: int}
2 │
3 │ let r: t = {a: 1}
4 │

Some required record fields are missing: b d.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type t = {a?: int, b: string, c?: bool, mutable d: int}

let r: t = {a: 1}
146 changes: 146 additions & 0 deletions tests/tests/src/large_optional_record_test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as JsxRuntime from "react/jsx-runtime";

let omitted = {
count: 1,
name: "n"
};

let written = {
count: 1,
"data-id": "x",
a5: 5,
a23: 23,
name: "n"
};

function writtenOpt(x) {
return {
count: 1,
a3: x,
name: "n"
};
}

function spread(b) {
return {
...b,
a3: 3
};
}

function spreadOpt(b, x) {
return {
...b,
"data-id": "y",
a4: x
};
}

function spreadMutable(b) {
return {
...b,
count: b.count + 1 | 0
};
}

let someFields = {
x: 1,
"y-name": "y"
};

function allOptionalSpread(r) {
return {
...r,
z: true
};
}

function smallSpread(r) {
return {
...r,
s: 2
};
}

function plainSpread(r) {
return {
p: 2,
q: r.q
};
}

let host = JsxRuntime.jsx("div", {
className: "c",
"data-testid": "t",
id: "i",
children: "x"
});

let hostNoProps = JsxRuntime.jsx("div", {});

function hostOptional(value) {
return JsxRuntime.jsx("input", {
type: "text",
value: value
});
}

function hostSpread(props) {
return JsxRuntime.jsx("div", props);
}

function hostSpreadProps(props) {
return JsxRuntime.jsx("div", {
...props,
className: "c",
children: "x"
}, "k");
}

let nested = JsxRuntime.jsxs("div", {
className: "outer",
children: [
JsxRuntime.jsx("span", {
id: "a",
children: "a"
}),
JsxRuntime.jsxs("p", {
children: [
"b",
JsxRuntime.jsx("br", {})
]
})
]
});

function keyed(items) {
return items.map(item => JsxRuntime.jsx("li", {
children: item
}, item));
}

let empty = {};

export {
omitted,
written,
writtenOpt,
spread,
spreadOpt,
spreadMutable,
empty,
someFields,
allOptionalSpread,
smallSpread,
plainSpread,
host,
hostNoProps,
hostOptional,
hostSpread,
hostSpreadProps,
nested,
keyed,
}
/* host Not a pure module */
83 changes: 83 additions & 0 deletions tests/tests/src/large_optional_record_test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
@@config({flags: ["-bs-jsx", "4"]})

// Records with many optional fields, as JSX props records are

type big = {
mutable count: int,
@as("data-id") dataId?: string,
a0?: int,
a1?: int,
a2?: int,
a3?: int,
a4?: int,
a5?: int,
a6?: int,
a7?: int,
a8?: int,
a9?: int,
a10?: int,
a11?: int,
a12?: int,
a13?: int,
a14?: int,
a15?: int,
a16?: int,
a17?: int,
a18?: int,
a19?: int,
a20?: int,
a21?: int,
a22?: int,
a23?: int,
name: string,
}

let omitted: big = {count: 1, name: "n"}
let written: big = {count: 1, dataId: "x", a5: 5, a23: 23, name: "n"}
let writtenOpt = (x: option<int>): big => {count: 1, a3: ?x, name: "n"}
let spread = (b: big) => {...b, a3: 3}
let spreadOpt = (b: big, x: option<int>) => {...b, a4: ?x, dataId: "y"}
let spreadMutable = (b: big) => {...b, count: b.count + 1}

// Every field optional, so no field is required
type allOptional = {
x?: int,
@as("y-name") y?: string,
mutable z?: bool,
}

let empty: allOptional = {}
let someFields: allOptional = {x: 1, y: "y"}
let allOptionalSpread = (r: allOptional) => {...r, z: true}

// A small record with an optional field still uses object spread
type small = {s: int, t?: string}

let smallSpread = (r: small) => {...r, s: 2}

// A small record without optional fields: a spread copies the fields one by
// one
type plain = {p: int, q: string}

let plainSpread = (r: plain) => {...r, p: 2}

// JSX props of host elements are JsxDOM.domProps, all optional
let host = <div className="c" id="i" dataTestId="t">{React.string("x")}</div>
let hostNoProps = <div />
let hostOptional = (value: option<string>) => <input ?value type_="text" />
let hostSpread = (props: JsxDOM.domProps) => <div {...props} />

let hostSpreadProps = (props: JsxDOM.domProps) =>
<div {...props} className="c" key="k">{React.string("x")}</div>

let nested =
<div className="outer">
<span id="a">{React.string("a")}</span>
<p>
{React.string("b")}
<br />
</p>
</div>

let keyed = (items: array<string>) =>
items->Array.map(item => <li key=item>{React.string(item)}</li>)->React.array
Loading