Skip to content

Commit 31b48e7

Browse files
committed
remove some intermediate allocation
1 parent 541963b commit 31b48e7

File tree

7 files changed

+26
-66
lines changed

7 files changed

+26
-66
lines changed

jscomp/syntax/ast_compatible.ml

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ open Parsetree
2828
let default_loc = Location.none
2929

3030

31-
type poly_var_label = Asttypes.label Asttypes.loc
3231

3332

3433

@@ -37,8 +36,9 @@ type poly_var_label = Asttypes.label Asttypes.loc
3736

3837

3938

40-
let arrow ?(loc=default_loc) ?(attrs = []) a b =
41-
Ast_helper.Typ.arrow ~loc ~attrs Nolabel a b
39+
40+
let arrow ?loc ?attrs a b =
41+
Ast_helper.Typ.arrow ?loc ?attrs Nolabel a b
4242

4343
let apply_simple
4444
?(loc = default_loc)
@@ -105,21 +105,6 @@ let fun_
105105
pexp_desc = Pexp_fun(Nolabel,None, pat, exp)
106106
}
107107

108-
(* let opt_label s =
109-
Asttypes.Optional s *)
110-
111-
(* let label_fun
112-
?(loc = default_loc)
113-
?(attrs = [])
114-
~label
115-
pat
116-
exp =
117-
{
118-
pexp_loc = loc;
119-
pexp_attributes = attrs;
120-
pexp_desc = Pexp_fun(label, None, pat, exp)
121-
} *)
122-
type hash_label = string
123108

124109

125110
let const_exp_string
@@ -133,15 +118,7 @@ let const_exp_string
133118
pexp_desc = Pexp_constant(Pconst_string(s,delimiter))
134119
}
135120

136-
let const_hash_label
137-
?(loc = default_loc)
138-
?(attrs = [])
139-
(s : hash_label) : expression =
140-
{
141-
pexp_loc = loc;
142-
pexp_attributes = attrs;
143-
pexp_desc = Pexp_constant(Pconst_string(s,None))
144-
}
121+
145122

146123
let const_exp_int
147124
?(loc = default_loc)
@@ -165,19 +142,6 @@ let apply_labels
165142
fn,
166143
Ext_list.map args (fun (l,a) -> Asttypes.Labelled l, a) ) }
167144

168-
let object_
169-
?(loc= default_loc)
170-
?(attrs = [])
171-
(fields : (Asttypes.label Asttypes.loc * attributes * core_type) list)
172-
flg : core_type =
173-
{
174-
ptyp_desc =
175-
Ptyp_object(
176-
Ext_list.map fields (fun (a,b,c) ->
177-
Parsetree.Otag (a,b,c)),flg);
178-
ptyp_loc = loc;
179-
ptyp_attributes = attrs
180-
}
181145

182146

183147

jscomp/syntax/ast_compatible.mli

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,6 @@ val opt_arrow:
143143
core_type ->
144144
core_type
145145

146-
val object_:
147-
?loc:loc ->
148-
?attrs:attrs ->
149-
(string Asttypes.loc * attributes * core_type) list ->
150-
Asttypes.closed_flag ->
151-
core_type
152146

153147

154148
(* val nonrec_type_str:

jscomp/syntax/ast_core_type.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ let from_labels ~loc arity labels
104104
Typ.var ~loc ("a" ^ string_of_int i)))) in
105105
let result_type =
106106
Ast_comb.to_js_type loc
107-
(Ast_compatible.object_ ~loc
108-
(Ext_list.map2 labels tyvars (fun x y -> x ,[], y)) Closed)
107+
(Typ.object_ ~loc
108+
(Ext_list.map2 labels tyvars
109+
(fun x y -> Parsetree.Otag (x ,[], y))) Closed)
109110
in
110111
Ext_list.fold_right2 labels tyvars result_type
111112
(fun label (* {loc ; txt = label }*)
@@ -115,7 +116,7 @@ let from_labels ~loc arity labels
115116

116117
let make_obj ~loc xs =
117118
Ast_comb.to_js_type loc
118-
(Ast_compatible.object_ ~loc xs Closed)
119+
(Typ.object_ ~loc xs Closed)
119120

120121

121122

jscomp/syntax/ast_core_type.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ val from_labels :
4747

4848
val make_obj :
4949
loc:Location.t ->
50-
(string Asttypes.loc * Parsetree.attributes * t) list ->
50+
Parsetree.object_field list ->
5151
t
5252

5353
val is_user_option : t -> bool

jscomp/syntax/ast_derive_js_mapper.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,10 @@ let init () =
435435

436436
let objType flag =
437437
Ast_comb.to_js_type loc @@
438-
Ast_compatible.object_
438+
Typ.object_
439439
(Ext_list.map label_declarations
440440
(fun {pld_name ; pld_type } ->
441-
pld_name, [], pld_type))
441+
Parsetree.Otag(pld_name, [], pld_type)))
442442
flag in
443443
newTypeStr +?
444444
[

jscomp/syntax/ast_external_process.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ let process_obj
400400
} ->
401401
if String.length prim_name <> 0 then
402402
Location.raise_errorf ~loc "[@@bs.obj] expect external names to be empty string";
403-
let arg_kinds, new_arg_types_ty, result_types =
403+
let arg_kinds, new_arg_types_ty, (result_types : Parsetree.object_field list) =
404404
Ext_list.fold_right arg_types_ty ( [], [], [])
405405
(fun param_type ( arg_labels, (arg_types : Ast_compatible.param_type list), result_types) ->
406406
let arg_label = param_type.label in
@@ -426,22 +426,22 @@ let process_obj
426426
{obj_arg_label = External_arg_spec.obj_label s;
427427
obj_arg_type },
428428
arg_types, (* ignored in [arg_types], reserved in [result_types] *)
429-
(({Asttypes.txt = name; loc} , [], new_ty) :: result_types)
429+
(Parsetree.Otag({Asttypes.txt = name; loc} , [], new_ty) :: result_types)
430430
| Nothing ->
431431
let s = (Lam_methname.translate name) in
432432
{obj_arg_label = External_arg_spec.obj_label s ; obj_arg_type },
433433
{param_type with ty = new_ty}::arg_types,
434-
(({Asttypes.txt = name; loc} , [], new_ty) :: result_types)
434+
(Otag ({Asttypes.txt = name; loc} , [], new_ty) :: result_types)
435435
| Int _ ->
436436
let s = Lam_methname.translate name in
437437
{obj_arg_label = External_arg_spec.obj_label s; obj_arg_type},
438438
{param_type with ty = new_ty}::arg_types,
439-
(({Asttypes.txt = name; loc}, [], Ast_literal.type_int ~loc ()) :: result_types)
439+
(Otag ({Asttypes.txt = name; loc}, [], Ast_literal.type_int ~loc ()) :: result_types)
440440
| Poly_var_string _ ->
441441
let s = Lam_methname.translate name in
442442
{obj_arg_label = External_arg_spec.obj_label s; obj_arg_type},
443443
{param_type with ty = new_ty }::arg_types,
444-
(({Asttypes.txt = name; loc}, [], Ast_literal.type_string ~loc ()) :: result_types)
444+
(Otag({Asttypes.txt = name; loc}, [], Ast_literal.type_string ~loc ()) :: result_types)
445445
| Fn_uncurry_arity _ ->
446446
Location.raise_errorf ~loc
447447
"The combination of [@@bs.obj], [@@bs.uncurry] is not supported yet"
@@ -464,17 +464,17 @@ let process_obj
464464
let s = (Lam_methname.translate name) in
465465
{obj_arg_label = External_arg_spec.optional s; obj_arg_type},
466466
param_type :: arg_types,
467-
( ({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc ty) :: result_types)
467+
( Parsetree.Otag ({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc ty) :: result_types)
468468
| Int _ ->
469469
let s = Lam_methname.translate name in
470470
{obj_arg_label = External_arg_spec.optional s ; obj_arg_type },
471471
param_type :: arg_types,
472-
(({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc @@ Ast_literal.type_int ~loc ()) :: result_types)
472+
(Otag ({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc @@ Ast_literal.type_int ~loc ()) :: result_types)
473473
| Poly_var_string _ ->
474474
let s = Lam_methname.translate name in
475475
{obj_arg_label = External_arg_spec.optional s ; obj_arg_type },
476476
param_type::arg_types,
477-
(({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc @@ Ast_literal.type_string ~loc ()) :: result_types)
477+
(Otag ({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc @@ Ast_literal.type_string ~loc ()) :: result_types)
478478
| Arg_cst _
479479
->
480480
Location.raise_errorf ~loc "bs.as is not supported with optional yet"

jscomp/syntax/ast_util.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ let ocaml_obj_as_js_object
6666

6767
let result = Typ.var ~loc val_name.txt in
6868
result ,
69-
((val_name , [], result ) ::
69+
(Parsetree.Otag (val_name , [], result ) ::
7070
(if is_mutable then
71-
[{val_name with txt = val_name.txt ^ Literals.setter_suffix},[],
72-
Ast_typ_uncurry.to_method_type loc mapper Nolabel result (Ast_literal.type_unit ~loc ()) ]
71+
[ Otag ({val_name with txt = val_name.txt ^ Literals.setter_suffix},[],
72+
Ast_typ_uncurry.to_method_type loc mapper Nolabel result (Ast_literal.type_unit ~loc ())) ]
7373
else
7474
[]) )
7575
in
@@ -84,7 +84,8 @@ let ocaml_obj_as_js_object
8484
for public object type its [@bs.meth] it does not depend on itself
8585
while for label argument it is [@bs.this] which depends internal object
8686
*)
87-
let internal_label_attr_types, public_label_attr_types =
87+
let (internal_label_attr_types : Parsetree.object_field list),
88+
(public_label_attr_types : Parsetree.object_field list) =
8889
Ext_list.fold_right clfs ([], [])
8990
(fun ({pcf_loc = loc} as x : Parsetree.class_field)
9091
(label_attr_types, public_label_attr_types) ->
@@ -102,9 +103,9 @@ let ocaml_obj_as_js_object
102103
->
103104
let method_type =
104105
Ast_typ_uncurry.generate_arg_type x.pcf_loc mapper label.txt lbl pat e in
105-
((label, [], method_type) :: label_attr_types),
106+
(Parsetree.Otag(label, [], method_type) :: label_attr_types),
106107
(if public_flag = Public then
107-
(label, [], method_type) :: public_label_attr_types
108+
Parsetree.Otag (label, [], method_type) :: public_label_attr_types
108109
else
109110
public_label_attr_types)
110111

0 commit comments

Comments
 (0)