Skip to content
Closed
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/parsetree-mapper-sharing.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Return unchanged parsetree nodes from the built-in PPX and JSX passes instead of copying them, so parsing allocates about 9% less.
88 changes: 44 additions & 44 deletions compiler/frontend/ast_tuple_pattern_flatten.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,44 @@ let flattern_tuple_pattern_vb (self : Ast_mapper.mapper)
Parsetree.value_binding list =
let pvb_pat = self.pat self vb.pvb_pat in
let pvb_constraint =
Option.map
(fun {Parsetree.pvc_newtypes; pvc_type} ->
{
Parsetree.pvc_newtypes =
List.map
(fun (name : string Asttypes.loc) ->
{name with loc = self.location self name.loc})
pvc_newtypes;
pvc_type = self.typ self pvc_type;
})
vb.pvb_constraint
match vb.pvb_constraint with
| None -> None
| Some {pvc_newtypes; pvc_type} as option ->
(* The type before the names, as the record this used to build
evaluated them *)
let pvc_type' = self.typ self pvc_type in
let pvc_newtypes' =
Ext_list.map_sharing pvc_newtypes (fun (name : string Asttypes.loc) ->
let loc = self.location self name.loc in
if loc == name.loc then name else {name with loc})
in
if pvc_type' == pvc_type && pvc_newtypes' == pvc_newtypes then option
else Some {pvc_newtypes = pvc_newtypes'; pvc_type = pvc_type'}
in
let pvb_expr = self.expr self vb.pvb_expr in
let pvb_attributes = self.attributes self vb.pvb_attributes in
let pvb_jsx_component = vb.pvb_jsx_component in
(* The binding with its mapped parts, [vb] itself when none changed *)
let mapped_binding () : Parsetree.value_binding =
if
pvb_pat == vb.pvb_pat
&& pvb_constraint == vb.pvb_constraint
&& pvb_expr == vb.pvb_expr
&& pvb_attributes == vb.pvb_attributes
then vb
else
{
pvb_pat;
pvb_expr;
pvb_constraint;
pvb_jsx_component;
pvb_loc = vb.pvb_loc;
pvb_attributes;
}
in
let pvb_expr_desc = (Ast_payload.unwrap_braces pvb_expr).pexp_desc in
match (pvb_constraint, pvb_pat.ppat_desc, pvb_expr_desc) with
| Some _, _, _ ->
{
pvb_pat;
pvb_expr;
pvb_constraint;
pvb_jsx_component;
pvb_loc = vb.pvb_loc;
pvb_attributes;
}
:: acc
| Some _, _, _ -> mapped_binding () :: acc
| None, Ppat_tuple xs, _ when List.for_all is_simple_pattern xs -> (
match Ast_open_cxt.destruct_open_tuple pvb_expr [] with
| Some (wholes, es, tuple_attributes)
Expand All @@ -92,16 +103,7 @@ let flattern_tuple_pattern_vb (self : Ast_mapper.mapper)
}
:: acc)
xs es acc
| _ ->
{
pvb_pat;
pvb_expr;
pvb_constraint;
pvb_jsx_component;
pvb_loc = vb.pvb_loc;
pvb_attributes;
}
:: acc)
| _ -> mapped_binding () :: acc)
| None, Ppat_record (_, _, Some rest), Pexp_pack {pmod_desc = Pmod_ident _} ->
Loc_report.raise_errorf ~loc:rest.rest_loc
"Record rest patterns are not supported when destructuring modules. Bind \
Expand All @@ -124,18 +126,16 @@ let flattern_tuple_pattern_vb (self : Ast_mapper.mapper)
| _ ->
Loc_report.raise_errorf ~loc:lid.loc
"Not supported pattern match on modules")
| _ ->
{
pvb_pat;
pvb_expr;
pvb_constraint;
pvb_jsx_component;
pvb_loc = vb.pvb_loc;
pvb_attributes;
}
:: acc
| _ -> mapped_binding () :: acc

let value_bindings_mapper (self : Ast_mapper.mapper)
(* The bindings are mapped last to first, as [Ext_list.fold_right] did. A list
whose bindings all come back unchanged is returned as it is. *)
let rec value_bindings_mapper (self : Ast_mapper.mapper)
(vbs : Parsetree.value_binding list) =
(* Ast_mapper.default_mapper.value_bindings self vbs *)
List.fold_right (fun vb acc -> flattern_tuple_pattern_vb self vb acc) vbs []
match vbs with
| [] -> vbs
| vb :: rest -> (
let rest' = value_bindings_mapper self rest in
match flattern_tuple_pattern_vb self vb rest' with
| vb' :: tail when vb' == vb && tail == rest' && rest' == rest -> vbs
| mapped -> mapped)
101 changes: 63 additions & 38 deletions compiler/frontend/bs_builtin_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,18 @@ let default_mapper = Ast_mapper.default_mapper
let default_expr_mapper (self : mapper) (e : Parsetree.expression) =
match e.pexp_desc with
| Pexp_let (Nonrecursive, vbs, body) ->
{
e with
pexp_desc =
Pexp_let
( Nonrecursive,
Ast_tuple_pattern_flatten.value_bindings_mapper self vbs,
self.expr self body );
pexp_attributes = self.attributes self e.pexp_attributes;
}
(* The attributes, the body, then the bindings: the order in which the
record and constructor this used to build evaluated them *)
let attrs = self.attributes self e.pexp_attributes in
let body' = self.expr self body in
let vbs' = Ast_tuple_pattern_flatten.value_bindings_mapper self vbs in
if attrs == e.pexp_attributes && body' == body && vbs' == vbs then e
else
{
e with
pexp_desc = Pexp_let (Nonrecursive, vbs', body');
pexp_attributes = attrs;
}
| _ -> Ast_mapper.default_mapper.expr self e

let rec add_this_to_braced_function attrs (e : Parsetree.expression) =
Expand Down Expand Up @@ -169,24 +172,35 @@ let expr_mapper ~async_context (self : mapper) (e : Parsetree.expression) =
inner function is not async and compiles to a plain arrow. *)
async_context := async;
let attrs = self.attributes self e.pexp_attributes in
let params =
List.map
(fun (p : Parsetree.fun_param) ->
{
p with
p_default = Option.map (self.expr self) p.p_default;
p_pat = self.pat self p.p_pat;
})
params
let params' =
Ext_list.map_sharing params (fun (p : Parsetree.fun_param) ->
(* The pattern before the default, as the record this used to
build evaluated them *)
let p_pat = self.pat self p.p_pat in
let p_default =
match p.p_default with
| None -> None
| Some default as option ->
let default' = self.expr self default in
if default' == default then option else Some default'
in
if p_pat == p.p_pat && p_default == p.p_default then p
else {p with p_default; p_pat})
in
let body = self.expr self body in
let newtypes =
List.map
(fun (name, nt_attrs) -> (name, self.attributes self nt_attrs))
newtypes
let body' = self.expr self body in
let newtypes' =
Ext_list.map_sharing newtypes (fun ((name, nt_attrs) as newtype) ->
let nt_attrs' = self.attributes self nt_attrs in
if nt_attrs' == nt_attrs then newtype else (name, nt_attrs'))
in
let mapped =
Ast_helper.Exp.fun_ ~loc:e.pexp_loc ~attrs ~async ~newtypes params body
if
attrs == e.pexp_attributes && params' == params && body' == body
&& newtypes' == newtypes
then e
else
Ast_helper.Exp.fun_ ~loc:e.pexp_loc ~attrs ~async ~newtypes:newtypes'
params' body'
in
Ast_async.make_function_async ~async mapped
| Meth_callback _, pexp_attributes ->
Expand Down Expand Up @@ -517,15 +531,15 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
[
{
pvb_pat = {ppat_desc = Ppat_var pval_name} as pvb_pat;
pvb_expr;
pvb_expr = expr;
pvb_constraint = None;
pvb_jsx_component;
pvb_attributes;
pvb_attributes = attributes;
pvb_loc;
};
] ) -> (
let pvb_expr = self.expr self pvb_expr in
let pvb_attributes = self.attributes self pvb_attributes in
let pvb_expr = self.expr self expr in
let pvb_attributes = self.attributes self attributes in
let has_inline_property =
Ast_attributes.has_inline_payload pvb_attributes
in
Expand Down Expand Up @@ -563,6 +577,7 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
}
pvb_expr);
}
| _ when pvb_expr == expr && pvb_attributes == attributes -> str
| _ ->
{
str with
Expand All @@ -582,8 +597,9 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
})
| Pstr_attribute ({txt = "config"}, _) -> str
| Pstr_value (Nonrecursive, vbs) ->
Ast_helper.Str.value ~loc:str.pstr_loc Nonrecursive
(Ast_tuple_pattern_flatten.value_bindings_mapper self vbs)
let vbs' = Ast_tuple_pattern_flatten.value_bindings_mapper self vbs in
if vbs' == vbs then str
else Ast_helper.Str.value ~loc:str.pstr_loc Nonrecursive vbs'
| _ -> default_mapper.structure_item self str

type mapper_context = {
Expand Down Expand Up @@ -724,11 +740,10 @@ let rec structure_mapper ~await_context (self : mapper) (stru : Ast_structure.t)
};
}
:: structure_mapper ~await_context self rest
| Some (_, _, Some _) | None ->
self.structure_item self item
:: structure_mapper ~await_context self rest)
| Some (_, _, Some _) | None -> map_item_and_rest ~await_context self stru
)
| Pstr_value (_, vbs) ->
let item = self.structure_item self item in
let item' = self.structure_item self item in
(* [ module __List__ = module type of List ] *)
let rec spelunk_vbs acc vbs =
match vbs with
Expand Down Expand Up @@ -766,11 +781,21 @@ let rec structure_mapper ~await_context (self : mapper) (stru : Ast_structure.t)
aux pvb_expr @ spelunk_vbs acc tl
in
let module_type_decls = spelunk_vbs [] vbs in
let rest' = structure_mapper ~await_context self rest in
if module_type_decls = [] && item' == item && rest' == rest then stru
else module_type_decls @ (item' :: rest')
| _ -> map_item_and_rest ~await_context self stru)

module_type_decls @ (item :: structure_mapper ~await_context self rest)
| _ ->
self.structure_item self item :: structure_mapper ~await_context self rest
)
(* [item :: rest] mapped as [self.structure_item self item :: structure_mapper
self rest] used to: the rest first, then the item. It is returned as it is
when neither changes. *)
and map_item_and_rest ~await_context self stru =
match stru with
| [] -> stru
| item :: rest ->
let rest' = structure_mapper ~await_context self rest in
let item' = self.structure_item self item in
if item' == item && rest' == rest then stru else item' :: rest'

let structure_mapper ~await_context (self : mapper) (stru : Ast_structure.t) =
let await_saved = !await_context in
Expand Down
10 changes: 2 additions & 8 deletions compiler/frontend/jsx_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,12 @@ let get_mapper ~config =
let old_config = save_config () in
config.has_component <- false;
let result =
List.map
(fun item ->
Ext_list.concat_map_sharing items (fun item ->
(match item.psig_desc with
| Psig_attribute attr -> process_config_attribute attr config
| _ -> ());
let item = default_mapper.signature_item mapper item in
if config.version = 4 then transform_signature_item4 item else [item])
items
|> List.flatten
in
restore_config old_config;
result
Expand All @@ -130,15 +127,12 @@ let get_mapper ~config =
let old_config = save_config () in
config.has_component <- false;
let result =
List.map
(fun item ->
Ext_list.concat_map_sharing items (fun item ->
(match item.pstr_desc with
| Pstr_attribute attr -> process_config_attribute attr config
| _ -> ());
let item = default_mapper.structure_item mapper item in
if config.version = 4 then transform_structure_item4 item else [item])
items
|> List.flatten
in
restore_config old_config;
result
Expand Down
44 changes: 44 additions & 0 deletions compiler/typing/ext_list.ml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,50 @@ let rec map_snd_sharing l f =
let xs' = map_snd_sharing xs f in
if x' == x && xs' == xs then l else (k, x') :: xs'

(* [concat_map_sharing] recurses over the first [concat_map_sharing_depth]
elements, which allocates nothing for an unchanged list, and continues with
a loop in constant stack after that, so the long structures the JSX
transform maps don't grow the stack by a frame per element. [map_sharing]
keeps its plain recursion: the Lambda and JS passes call it all the time,
and a depth counter costs measurable instructions there. *)
let concat_map_sharing_depth = 1000

(* The elements of [l] before its suffix [stop], reversed onto [acc] *)
let rec rev_prefix_onto l stop acc =
if l == stop then acc
else
match l with
| [] -> acc
| x :: xs -> rev_prefix_onto xs stop (x :: acc)

(* [concat_map_sharing] in constant stack. [acc] is the result up to the last
changed element, reversed, and [run] the input after that element, whose
elements seen so far map to themselves and are shared at the end. *)
let rec concat_map_sharing_loop f changed acc run l =
match l with
| [] -> if changed then List.rev_append acc run else run
| x :: xs -> (
match f x with
| [x'] when x' == x -> concat_map_sharing_loop f changed acc run xs
| mapped ->
concat_map_sharing_loop f true
(List.rev_append mapped (rev_prefix_onto run l acc))
xs xs)

let rec concat_map_sharing_bounded l f depth =
match l with
| [] -> l
| _ when depth = 0 -> concat_map_sharing_loop f false [] l l
| x :: xs -> (
let mapped = f x in
let xs' = concat_map_sharing_bounded xs f (depth - 1) in
match mapped with
| [x'] when x' == x && xs' == xs -> l
| _ -> mapped @ xs')

let concat_map_sharing l f =
concat_map_sharing_bounded l f concat_map_sharing_depth

let rec map_last l f =
match l with
| [] -> []
Expand Down
5 changes: 5 additions & 0 deletions compiler/typing/ext_list.mli
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ val map_sharing : 'a list -> ('a -> 'a) -> 'a list
val map_snd_sharing : ('a * 'b) list -> ('b -> 'b) -> ('a * 'b) list
(** [map_snd] with the sharing of {!map_sharing}. *)

val concat_map_sharing : 'a list -> ('a -> 'a list) -> 'a list
(** [List.concat_map f l], but [l] itself when [f] maps every element to a list
of just that element. It keeps the unchanged tail of a list, and runs in
constant stack beyond the first thousand elements. *)

val map_last : 'a list -> (bool -> 'a -> 'b) -> 'b list
(** [map_last f xs ]
will pass [true] to [f] for the last element,
Expand Down
Loading
Loading