From 32d6a7b6fed1ec7bca3fadc374031483ec97baed Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Sat, 26 Oct 2024 18:46:12 -0700 Subject: [PATCH] refactor: use inline record for J.Caml_block (#1192) --- jscomp/core/j.ml | 7 +++- jscomp/core/js_analyzer.ml | 7 ++-- jscomp/core/js_dump.ml | 42 +++++++++++++------- jscomp/core/js_exp_make.ml | 19 ++++----- jscomp/core/js_of_lam_variant.ml | 9 ++++- jscomp/core/js_pass_flatten.ml | 6 +-- jscomp/core/js_pass_flatten_and_mark_dead.ml | 23 +++++++++-- 7 files changed, 75 insertions(+), 38 deletions(-) diff --git a/jscomp/core/j.ml b/jscomp/core/j.ml index cd7a75e0f9..848c79c258 100644 --- a/jscomp/core/j.ml +++ b/jscomp/core/j.ml @@ -155,7 +155,12 @@ and expression_desc = | Array of expression list * mutable_flag | Optional_block of expression * bool (* [true] means [identity] *) - | Caml_block of expression list * mutable_flag * expression * tag_info + | Caml_block of { + fields : expression list; + mutable_flag : mutable_flag; + tag : expression; + tag_info : tag_info; + } (* The third argument is [tag] , forth is [tag_info] *) (* | Caml_uninitialized_obj of expression * expression *) (* [tag] and [size] tailed for [Obj.new_block] *) diff --git a/jscomp/core/js_analyzer.ml b/jscomp/core/js_analyzer.ml index 57c5337046..62ea08878b 100644 --- a/jscomp/core/js_analyzer.ml +++ b/jscomp/core/js_analyzer.ml @@ -92,7 +92,8 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) = no_side_effect a && no_side_effect b | Is_null_or_undefined b -> no_side_effect b | Str (b, _) -> b - | Array (xs, _mutable_flag) | Caml_block (xs, _mutable_flag, _, _) -> + | Array (xs, _mutable_flag) + | Caml_block { fields = xs; mutable_flag = _mutable_flag; _ } -> (* create [immutable] block, does not really mean that this opreation itself is [pure]. @@ -206,9 +207,9 @@ let rec eq_expression ({ expression_desc = x0; _ } : J.expression) match y0 with | Optional_block (a1, b1) -> b0 = b1 && eq_expression a0 a1 | _ -> false) - | Caml_block (ls0, flag0, tag0, _) -> ( + | Caml_block { fields = ls0; mutable_flag = flag0; tag = tag0; _ } -> ( match y0 with - | Caml_block (ls1, flag1, tag1, _) -> + | Caml_block { fields = ls1; mutable_flag = flag1; tag = tag1; _ } -> eq_expression_list ls0 ls1 && flag0 = flag1 && eq_expression tag0 tag1 | _ -> false) | Length _ | Char_of_int _ | Char_to_int _ | Is_null_or_undefined _ diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index 6004ab0e8b..55477a3e69 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -202,11 +202,12 @@ let exp_need_paren (e : J.expression) = | Raw_js_code { code_info = Exp _; _ } | Fun _ | Caml_block - ( _, - _, - _, - ( Blk_record _ | Blk_module _ | Blk_poly_var | Blk_extension - | Blk_record_ext _ | Blk_record_inlined _ | Blk_constructor _ ) ) + { + tag_info = + ( Blk_record _ | Blk_module _ | Blk_poly_var | Blk_extension + | Blk_record_ext _ | Blk_record_inlined _ | Blk_constructor _ ); + _; + } | Object _ -> true | Raw_js_code { code_info = Stmt _; _ } @@ -760,18 +761,18 @@ and expression_desc cxt ~(level : int) x : cxt = else E.runtime_call ~module_name:Js_runtime_modules.option ~fn_name:"some" [ e ]) - | Caml_block (el, _, _, Blk_module fields) -> + | Caml_block { fields = el; tag_info = Blk_module fields; _ } -> expression_desc cxt ~level (Object (List.map_combine fields el (fun x -> Js_op.Lit (Ident.convert x)))) (*name convention of Record is slight different from modules*) - | Caml_block (el, mutable_flag, _, Blk_record fields) -> + | Caml_block { fields = el; mutable_flag; tag_info = Blk_record fields; _ } -> if block_has_all_int_fields fields then expression_desc cxt ~level (Array (el, mutable_flag)) else expression_desc cxt ~level (Object (List.map_combine_array fields el (fun i -> Js_op.Lit i))) - | Caml_block (el, _, _, Blk_poly_var) -> ( + | Caml_block { fields = el; tag_info = Blk_poly_var; _ } -> ( match el with | [ { expression_desc = Str (_, name); _ }; value ] -> expression_desc cxt ~level @@ -781,9 +782,11 @@ and expression_desc cxt ~(level : int) x : cxt = (Lit L.polyvar_value, value); ]) | _ -> assert false) - | Caml_block (el, _, _, ((Blk_extension | Blk_record_ext _) as ext)) -> + | Caml_block + { fields = el; tag_info = (Blk_extension | Blk_record_ext _) as ext; _ } + -> expression cxt ~level (exn_block_as_obj ~stack:false el ext) - | Caml_block (el, _, tag, Blk_record_inlined p) -> + | Caml_block { fields = el; tag; tag_info = Blk_record_inlined p; _ } -> let objs = let tails = List.map_combine_array_append p.fields el @@ -804,7 +807,7 @@ and expression_desc cxt ~(level : int) x : cxt = :: tails in expression_desc cxt ~level (Object objs) - | Caml_block (el, _, tag, Blk_constructor p) -> + | Caml_block { fields = el; tag; tag_info = Blk_constructor p; _ } -> let is_cons = Js_op_util.is_cons p.name in let objs = let tails = @@ -833,8 +836,14 @@ and expression_desc cxt ~(level : int) x : cxt = :: tails in expression_desc cxt ~level (Object objs) - | Caml_block (_, _, _, (Blk_module_export | Blk_na _)) -> assert false - | Caml_block (el, mutable_flag, _tag, (Blk_tuple | Blk_class | Blk_array)) -> + | Caml_block { tag_info = Blk_module_export | Blk_na _; _ } -> assert false + | Caml_block + { + fields = el; + mutable_flag; + tag_info = Blk_tuple | Blk_class | Blk_array; + _; + } -> expression_desc cxt ~level (Array (el, mutable_flag)) | Caml_block_tag e -> group cxt 1 (fun _ -> @@ -1214,7 +1223,12 @@ and statement_desc top cxt (s : J.statement_desc) : cxt = | Throw e -> let e = match e.expression_desc with - | Caml_block (el, _, _, ((Blk_extension | Blk_record_ext _) as ext)) -> + | Caml_block + { + fields = el; + tag_info = (Blk_extension | Blk_record_ext _) as ext; + _; + } -> { e with expression_desc = diff --git a/jscomp/core/js_exp_make.ml b/jscomp/core/js_exp_make.ml index e607e2c961..4c5989c4ea 100644 --- a/jscomp/core/js_exp_make.ml +++ b/jscomp/core/js_exp_make.ml @@ -183,15 +183,16 @@ let dot ?loc ?comment (e0 : t) (e1 : string) : t = let module_access (e : t) (name : string) (pos : int32) = let name = Ident.convert name in match e.expression_desc with - | Caml_block (l, _, _, _) when no_side_effect e -> ( + | Caml_block { fields = l; _ } when no_side_effect e -> ( match List.nth_opt l (Int32.to_int pos) with | Some x -> x | None -> make_expression (Static_index (e, name, Some pos))) | _ -> make_expression (Static_index (e, name, Some pos)) -let make_block ?loc ?comment (tag : t) (tag_info : J.tag_info) (es : t list) +let make_block ?loc ?comment (tag : t) (tag_info : J.tag_info) (fields : t list) (mutable_flag : J.mutable_flag) : t = - make_expression ?loc ?comment (Caml_block (es, mutable_flag, tag, tag_info)) + make_expression ?loc ?comment + (Caml_block { fields; mutable_flag; tag; tag_info }) (* ATTENTION: this is relevant to how we encode string, boolean *) let typeof ?loc ?comment (e : t) : t = @@ -327,7 +328,7 @@ let array_index ?loc ?comment (e0 : t) (e1 : t) : t = let array_index_by_int ?loc ?comment (e : t) (pos : int32) : t = match e.expression_desc with | Array (l, _) (* Float i -- should not appear here *) - | Caml_block (l, _, _, _) + | Caml_block { fields = l; _ } when no_side_effect e -> ( match List.nth_opt l (Int32.to_int pos) with | Some x -> x @@ -337,7 +338,7 @@ let array_index_by_int ?loc ?comment (e : t) (pos : int32) : t = let record_access (e : t) (name : string) (pos : int32) = match e.expression_desc with | Array (l, _) (* Float i -- should not appear here *) - | Caml_block (l, _, _, _) + | Caml_block { fields = l; _ } when no_side_effect e -> ( match List.nth_opt l (Int32.to_int pos) with | Some x -> x @@ -364,20 +365,20 @@ let cons_access (e : t) (pos : int32) = let poly_var_tag_access (e : t) = match e.expression_desc with - | Caml_block (l, _, _, _) when no_side_effect e -> ( + | Caml_block { fields = l; _ } when no_side_effect e -> ( match l with x :: _ -> x | [] -> assert false) | _ -> make_expression (Static_index (e, Js_dump_lit.polyvar_hash, Some 0l)) let poly_var_value_access (e : t) = match e.expression_desc with - | Caml_block (l, _, _, _) when no_side_effect e -> ( + | Caml_block { fields = l; _ } when no_side_effect e -> ( match l with _ :: v :: _ -> v | _ -> assert false) | _ -> make_expression (Static_index (e, Js_dump_lit.polyvar_value, Some 1l)) let extension_access (e : t) ?name (pos : int32) : t = match e.expression_desc with | Array (l, _) (* Float i -- should not appear here *) - | Caml_block (l, _, _, _) + | Caml_block { fields = l; _ } when no_side_effect e -> ( match List.nth_opt l (Int32.to_int pos) with | Some x -> x @@ -461,7 +462,7 @@ let extension_assign (e : t) (pos : int32) name (value : t) = let array_length ?loc ?comment (e : t) : t = match e.expression_desc with (* TODO: use array instead? *) - | (Array (l, _) | Caml_block (l, _, _, _)) when no_side_effect e -> + | (Array (l, _) | Caml_block { fields = l; _ }) when no_side_effect e -> int ?comment (Int32.of_int (List.length l)) | _ -> make_expression ?loc ?comment (Length (e, Array)) diff --git a/jscomp/core/js_of_lam_variant.ml b/jscomp/core/js_of_lam_variant.ml index d38c2560ae..cd18079b9d 100644 --- a/jscomp/core/js_of_lam_variant.ml +++ b/jscomp/core/js_of_lam_variant.ml @@ -56,7 +56,12 @@ let eval (arg : J.expression) (dispatches : (string * string) list) : E.t = let eval_as_event (arg : J.expression) (dispatches : (string * string) list option) = match arg.expression_desc with - | Caml_block ([ { expression_desc = Str (_, s); _ }; cb ], _, _, Blk_poly_var) + | Caml_block + { + fields = [ { expression_desc = Str (_, s); _ }; cb ]; + tag_info = Blk_poly_var; + _; + } when Js_analyzer.no_side_effect_expression cb -> let v = match dispatches with @@ -124,5 +129,5 @@ let eval_as_int (arg : J.expression) (dispatches : (string * int) list) : E.t = let eval_as_unwrap (arg : J.expression) : E.t = match arg.expression_desc with - | Caml_block ([ { expression_desc = Number _; _ }; cb ], _, _, _) -> cb + | Caml_block { fields = [ { expression_desc = Number _; _ }; cb ]; _ } -> cb | _ -> E.poly_var_value_access arg diff --git a/jscomp/core/js_pass_flatten.ml b/jscomp/core/js_pass_flatten.ml index 5dae8de920..cd34bbde6c 100644 --- a/jscomp/core/js_pass_flatten.ml +++ b/jscomp/core/js_pass_flatten.ml @@ -47,11 +47,7 @@ let flatten_map = (List.rev_map ~f:(fun x -> self.statement self x) (Js_analyzer.rev_flatten_seq v)) - | Exp - { - expression_desc = Caml_block (args, _mutable_flag, _tag, _tag_info); - _; - } -> + | Exp { expression_desc = Caml_block { fields = args; _ }; _ } -> S.block (List.map ~f:(fun arg -> self.statement self (S.exp arg)) args) | Exp { expression_desc = Cond (a, b, c); comment; _ } -> diff --git a/jscomp/core/js_pass_flatten_and_mark_dead.ml b/jscomp/core/js_pass_flatten_and_mark_dead.ml index 7eaea2adff..d764245481 100644 --- a/jscomp/core/js_pass_flatten_and_mark_dead.ml +++ b/jscomp/core/js_pass_flatten_and_mark_dead.ml @@ -185,7 +185,12 @@ let subst_map (substitution : J.expression Ident.Hash.t) = ({ expression_desc = Caml_block - ((_ :: _ :: _ as ls), Immutable, tag, tag_info); + { + fields = _ :: _ :: _ as ls; + mutable_flag = Immutable; + tag; + tag_info; + }; _; } as block); _; @@ -232,7 +237,13 @@ let subst_map (substitution : J.expression Ident.Hash.t) = { block with expression_desc = - Caml_block (List.rev e, Immutable, tag, tag_info); + Caml_block + { + fields = List.rev e; + mutable_flag = Immutable; + tag; + tag_info; + }; } in let () = add_substitue substitution ident e in @@ -262,8 +273,12 @@ let subst_map (substitution : J.expression Ident.Hash.t) = { expression_desc = Number (Int { i; _ }); _ } ) | Static_index ({ expression_desc = Var (Id id); _ }, _, Some i) -> ( match Ident.Hash.find_opt substitution id with - | Some { expression_desc = Caml_block (ls, Immutable, _, _); _ } - -> ( + | Some + { + expression_desc = + Caml_block { fields = ls; mutable_flag = Immutable; _ }; + _; + } -> ( (* user program can be wrong, we should not turn a runtime crash into compile time crash : ) *)