diff --git a/changes/jsx-props-skip-label-copy.polish.md b/changes/jsx-props-skip-label-copy.polish.md new file mode 100644 index 0000000000..dfbdd48afd --- /dev/null +++ b/changes/jsx-props-skip-label-copy.polish.md @@ -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. diff --git a/compiler/ml/lambda.ml b/compiler/ml/lambda.ml index fa3c3a9c26..495d193aa6 100644 --- a/compiler/ml/lambda.ml +++ b/compiler/ml/lambda.ml @@ -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. *) diff --git a/compiler/ml/lambda.mli b/compiler/ml/lambda.mli index 3e22e7397a..cabfff2f0d 100644 --- a/compiler/ml/lambda.mli +++ b/compiler/ml/lambda.mli @@ -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. *) diff --git a/compiler/ml/translcore.ml b/compiler/ml/translcore.ml index 694081abae..94ec677966 100644 --- a/compiler/ml/translcore.ml +++ b/compiler/ml/translcore.ml @@ -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] @@ -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 *) @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/compiler/ml/typecore.ml b/compiler/ml/typecore.ml index b2122197d7..2ae56d4446 100644 --- a/compiler/ml/typecore.ml +++ b/compiler/ml/typecore.ml @@ -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 diff --git a/tests/build_tests/super_errors/expected/record_literal_missing_fields_some_optional.res.expected b/tests/build_tests/super_errors/expected/record_literal_missing_fields_some_optional.res.expected new file mode 100644 index 0000000000..5ee571dfcc --- /dev/null +++ b/tests/build_tests/super_errors/expected/record_literal_missing_fields_some_optional.res.expected @@ -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. \ No newline at end of file diff --git a/tests/build_tests/super_errors/fixtures/record_literal_missing_fields_some_optional.res b/tests/build_tests/super_errors/fixtures/record_literal_missing_fields_some_optional.res new file mode 100644 index 0000000000..53d1c62870 --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/record_literal_missing_fields_some_optional.res @@ -0,0 +1,3 @@ +type t = {a?: int, b: string, c?: bool, mutable d: int} + +let r: t = {a: 1} diff --git a/tests/tests/src/large_optional_record_test.mjs b/tests/tests/src/large_optional_record_test.mjs new file mode 100644 index 0000000000..7195872086 --- /dev/null +++ b/tests/tests/src/large_optional_record_test.mjs @@ -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 */ diff --git a/tests/tests/src/large_optional_record_test.res b/tests/tests/src/large_optional_record_test.res new file mode 100644 index 0000000000..f1d4314cb0 --- /dev/null +++ b/tests/tests/src/large_optional_record_test.res @@ -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): big => {count: 1, a3: ?x, name: "n"} +let spread = (b: big) => {...b, a3: 3} +let spreadOpt = (b: big, x: option) => {...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 =
{React.string("x")}
+let hostNoProps =
+let hostOptional = (value: option) => +let hostSpread = (props: JsxDOM.domProps) =>
+ +let hostSpreadProps = (props: JsxDOM.domProps) => +
{React.string("x")}
+ +let nested = +
+ {React.string("a")} +

+ {React.string("b")} +
+

+
+ +let keyed = (items: array) => + items->Array.map(item =>
  • {React.string(item)}
  • )->React.array