Skip to content

Commit 6fca4a4

Browse files
authored
Merge pull request #4188 from BuckleScript/optimize_ffi_encoding
a bunch optimizations around ffi encoding
2 parents e4b9322 + e69b18d commit 6fca4a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+33498
-32629
lines changed

jscomp/bsb/bsb_ninja_file_groups.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ let emit_module_build
9696
filename_sans_extension ^ if is_re then Literals.suffix_reiast else Literals.suffix_mliast in
9797
let output_d = filename_sans_extension ^ Literals.suffix_d in
9898
let output_filename_sans_extension =
99-
Ext_namespace.make ?ns:namespace filename_sans_extension
99+
Ext_namespace_encode.make ?ns:namespace filename_sans_extension
100100
in
101101
let output_cmi = output_filename_sans_extension ^ Literals.suffix_cmi in
102102
let output_cmj = output_filename_sans_extension ^ Literals.suffix_cmj in

jscomp/bsb/bsb_world.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let install_targets cwd ({files_to_install; namespace; package_name = _} : Bsb_c
3535
let lib_artifacts_dir = Lazy.force Bsb_global_backend.lib_artifacts_dir in
3636
let install_filename_sans_extension destdir namespace x =
3737
let x =
38-
Ext_namespace.make ?ns:namespace x in
38+
Ext_namespace_encode.make ?ns:namespace x in
3939
install ~destdir (cwd // x ^ Literals.suffix_ml) ;
4040
install ~destdir (cwd // x ^ Literals.suffix_re) ;
4141
install ~destdir (cwd // x ^ Literals.suffix_mli) ;

jscomp/bsb_helper/bsb_helper_depfile_gen.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ type kind = Js | Bytecode | Native
9191

9292
let output_file (buf : Ext_buffer.t) source namespace =
9393
Ext_buffer.add_string buf
94-
(Ext_namespace.make ?ns:namespace source)
94+
(Ext_namespace_encode.make ?ns:namespace source)
9595

9696
(** for bucklescript artifacts
9797
[lhs_suffix] is [.cmj]
@@ -144,11 +144,11 @@ let oc_impl
144144
output_file buf (Ext_filename.chop_extension_maybe mlast) namespace ;
145145
Ext_buffer.add_string buf lhs_suffix;
146146
Ext_buffer.add_string buf dep_lit ) in
147-
Ext_option.iter namespace (fun ns ->
147+
(match namespace with None -> () | Some ns ->
148148
Lazy.force at_most_once;
149149
Ext_buffer.add_string buf ns;
150150
Ext_buffer.add_string buf Literals.suffix_cmi;
151-
) ; (* TODO: moved into static files*)
151+
) ; (* TODO: moved into static files*)
152152
let is_not_lib_dir = not (Bsb_dir_index.is_lib_dir index) in
153153
let s = extract_dep_raw_string mlast in
154154
let offset = ref 1 in
@@ -206,11 +206,11 @@ let oc_intf
206206
output_file buf (Ext_filename.chop_all_extensions_maybe mliast) namespace ;
207207
Ext_buffer.add_string buf Literals.suffix_cmi ;
208208
Ext_buffer.add_string buf dep_lit) in
209-
Ext_option.iter namespace (fun ns ->
209+
(match namespace with None -> () | Some ns ->
210210
Lazy.force at_most_once;
211211
Ext_buffer.add_string buf ns;
212212
Ext_buffer.add_string buf Literals.suffix_cmi;
213-
) ;
213+
) ;
214214
let cur_module_name = Ext_filename.module_name mliast in
215215
let is_not_lib_dir = not (Bsb_dir_index.is_lib_dir index) in
216216
let s = extract_dep_raw_string mliast in

jscomp/core/bs_cmi_load.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ let load_cmi ~unit_name : Env.Persistent_signature.t option =
3838
if Js_config.get_diagnose () then
3939
Format.fprintf Format.err_formatter ">Cmi: %s@." unit_name;
4040
let cmi : Cmi_format.cmi_infos =
41-
Marshal.from_string
42-
Builtin_cmi_datasets.module_data.(cmi) 0 in
41+
Ext_marshal.from_string_uncheck
42+
Builtin_cmi_datasets.module_data.(cmi) in
4343
if Js_config.get_diagnose () then
4444
Format.fprintf Format.err_formatter "<Cmi: %s@." unit_name;
4545
Some {filename = Sys.executable_name ;

jscomp/core/js_cmj_load.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ then
4242
if Js_config.get_diagnose () then
4343
Format.fprintf Format.err_formatter ">Cmj: %s@." unit_name;
4444
let cmj_table : Js_cmj_format.t =
45-
let values, pure = Marshal.from_string Builtin_cmj_datasets.module_data.(i) 0 in
45+
let values, pure = Ext_marshal.from_string_uncheck Builtin_cmj_datasets.module_data.(i) in
4646
{values; pure; package_spec = Js_packages_info.runtime_package_specs;js_file_kind = Little_js}
4747
in
4848
if Js_config.get_diagnose () then

jscomp/core/js_implementation.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ let make_structure_item ~ns cunit : Parsetree.structure_item =
227227
(Mb.mk {txt = cunit; loc }
228228
(Mod.ident
229229
{txt = Lident
230-
( Ext_namespace.make ~ns cunit)
230+
( Ext_namespace_encode.make ~ns cunit)
231231
; loc}))
232232

233233

jscomp/core/lam_compile_external_call.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ let ocaml_to_js_eff
9797
: arg_expression * E.t list =
9898
let arg =
9999
match arg_label with
100-
| Optional ->
100+
| Arg_optional ->
101101
Js_of_lam_option.get_default_undefined_from_optional raw_arg
102-
| Label | Empty -> raw_arg
102+
| Arg_label | Arg_empty -> raw_arg
103103
in
104104
match arg_type with
105105
| Arg_cst _ -> assert false
106106
| Fn_uncurry_arity _ -> assert false
107107
(* has to be preprocessed by {!Lam} module first *)
108108
| Extern_unit ->
109-
(if arg_label = Empty then
109+
(if arg_label = Arg_empty then
110110
Splice0 else Splice1 E.unit),
111111
(if Js_analyzer.no_side_effect_expression arg then
112112
[]
@@ -132,7 +132,7 @@ let ocaml_to_js_eff
132132
| Unwrap ->
133133
let single_arg =
134134
match arg_label with
135-
| Optional ->
135+
| Arg_optional ->
136136
(**
137137
If this is an optional arg (like `?arg`), we have to potentially do
138138
2 levels of unwrapping:
@@ -174,8 +174,8 @@ let assemble_args_no_splice call_loc ffi
174174
match labels, args with
175175
| [], _
176176
-> assert (args = []) ; empty_pair
177-
| { arg_label = Empty | Label ; arg_type = Arg_cst cst } :: labels, args
178-
->
177+
| { arg_type = Arg_cst cst ; _} :: labels, args
178+
-> (* can not be Optional *)
179179
let accs, eff = aux labels args in
180180
Lam_compile_const.translate_arg_cst cst :: accs, eff
181181
| {arg_label ; arg_type } ::labels,
@@ -201,7 +201,7 @@ let assemble_args_has_splice call_loc ffi (arg_types : specs) (args : exprs)
201201
let rec aux (labels : specs) (args : exprs) =
202202
match labels, args with
203203
| [] , _ -> assert (args = []); empty_pair
204-
| { arg_label = Empty | Label; arg_type = Arg_cst cst} :: labels , args
204+
| { arg_type = Arg_cst cst; _} :: labels , args
205205
->
206206
let accs, eff = aux labels args in
207207
Lam_compile_const.translate_arg_cst cst :: accs, eff

jscomp/core/lam_compile_external_obj.ml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,47 +40,47 @@ module S = Js_stmt_make
4040
*)
4141

4242
(* TODO: check stackoverflow *)
43-
let assemble_obj_args (labels : External_arg_spec.t list) (args : J.expression list)
43+
let assemble_obj_args (labels : External_arg_spec.obj_params) (args : J.expression list)
4444
: J.block * J.expression =
45-
let rec aux (labels : External_arg_spec.t list) args
45+
let rec aux (labels : External_arg_spec.obj_params) args
4646
: (Js_op.property_name * E.t ) list * J.expression list * _ =
4747
match labels, args with
4848
| [] , [] -> [], [], []
49-
| {arg_label = Label {name = label; cst = Some cst }} :: labels , args ->
49+
| {obj_arg_label = Obj_label {name = label; }; obj_arg_type = Arg_cst cst } :: labels , args ->
5050
let accs, eff, assign = aux labels args in
5151
(label, Lam_compile_const.translate_arg_cst cst )::accs, eff, assign
52-
| {arg_label = EmptyCst _ } :: rest , args -> assert false
53-
| {arg_label = Empty }::labels, arg::args
52+
(* | {obj_arg_label = EmptyCst _ } :: rest , args -> assert false *)
53+
| {obj_arg_label = Obj_empty }::labels, arg::args
5454
-> (* unit type*)
5555
let (accs, eff, assign) as r = aux labels args in
5656
if Js_analyzer.no_side_effect_expression arg then r
5757
else (accs, arg::eff, assign)
58-
| ({arg_label = Label {name = label; cst = None} } as arg_kind)::labels, arg::args
58+
| ({obj_arg_label = Obj_label {name = label;} } as arg_kind)::labels, arg::args
5959
->
6060
let accs, eff, assign = aux labels args in
61-
let acc, new_eff = Lam_compile_external_call.ocaml_to_js_eff ~arg_label:Label ~arg_type:arg_kind.arg_type arg in
61+
let acc, new_eff = Lam_compile_external_call.ocaml_to_js_eff ~arg_label:Arg_label ~arg_type:arg_kind.obj_arg_type arg in
6262
begin match acc with
6363
| Splice2 _
6464
| Splice0 -> assert false
6565
| Splice1 x ->
6666
(label, x) :: accs , Ext_list.append new_eff eff , assign
6767
end (* evaluation order is undefined *)
6868

69-
| ({arg_label = Optional {name = label}; arg_type } as arg_kind)::labels, arg::args
69+
| ({obj_arg_label = Obj_optional {name = label}; obj_arg_type } as arg_kind)::labels, arg::args
7070
->
7171
let (accs, eff, assign) as r = aux labels args in
7272
Js_of_lam_option.destruct_optional arg
7373
~for_sure_none:r
7474
~for_sure_some:(fun x -> let acc, new_eff = Lam_compile_external_call.ocaml_to_js_eff
75-
~arg_label:Label ~arg_type x in
75+
~arg_label:Arg_label ~arg_type:obj_arg_type x in
7676
begin match acc with
7777
| Splice2 _
7878
| Splice0 -> assert false
7979
| Splice1 x ->
8080
(label, x) :: accs , Ext_list.append new_eff eff , assign
8181
end )
8282
~not_sure:(fun _ -> accs, eff , (arg_kind,arg)::assign )
83-
| {arg_label = Empty | Label {cst = None;_} | Optional _ } :: _ , [] -> assert false
83+
| {obj_arg_label = Obj_empty | Obj_label _ | Obj_optional _ } :: _ , [] -> assert false
8484
| [], _ :: _ -> assert false
8585
in
8686
let map, eff, assignment = aux labels args in
@@ -101,9 +101,9 @@ let assemble_obj_args (labels : External_arg_spec.t list) (args : J.expression
101101
| x::xs -> E.seq (E.fuse_to_seq x xs) (E.obj map)
102102
end) ::
103103
(Ext_list.flat_map assignment (fun
104-
((xlabel : External_arg_spec.t), (arg : J.expression )) ->
104+
((xlabel : External_arg_spec.obj_param), (arg : J.expression )) ->
105105
match xlabel with
106-
| {arg_label = Optional {name = label} } ->
106+
| {obj_arg_label = Obj_optional {name = label} } ->
107107
(* Need make sure whether assignment is effectful or not
108108
to avoid code duplication
109109
*)
@@ -112,7 +112,7 @@ let assemble_obj_args (labels : External_arg_spec.t list) (args : J.expression
112112
let acc,new_eff =
113113
Lam_compile_external_call.ocaml_to_js_eff
114114
~arg_label:
115-
Empty ~arg_type:xlabel.arg_type
115+
Arg_empty ~arg_type:xlabel.obj_arg_type
116116
(Js_of_lam_option.val_from_option arg) in
117117
begin match acc with
118118
| Splice1 v ->
@@ -131,8 +131,8 @@ let assemble_obj_args (labels : External_arg_spec.t list) (args : J.expression
131131
let acc,new_eff =
132132
Lam_compile_external_call.ocaml_to_js_eff
133133
~arg_label:
134-
Empty
135-
~arg_type:xlabel.arg_type
134+
Arg_empty
135+
~arg_type:xlabel.obj_arg_type
136136
(Js_of_lam_option.val_from_option arg) in
137137
begin match acc with
138138
| Splice1 v ->

jscomp/core/lam_compile_external_obj.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*)
3939

4040
val assemble_obj_args :
41-
External_arg_spec.t list ->
41+
External_arg_spec.obj_params ->
4242
J.expression list ->
4343
J.block * J.expression
4444
(* It returns a block in cases we need set the property dynamically: we need

jscomp/core/lam_convert.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
456456
let args = Ext_list.map args convert_aux in
457457
prim ~primitive:(Pjs_object_create labels) ~args loc
458458
| Ffi_bs(arg_types, result_type, ffi) ->
459+
let arg_types =
460+
match arg_types with
461+
| Params ls -> ls
462+
| Param_number i -> Ext_list.init i (fun _ -> External_arg_spec.dummy ) in
459463
let args = Ext_list.map args convert_aux in
460464
Lam.handle_bs_non_obj_ffi arg_types result_type ffi args loc prim_name
461465
| Ffi_inline_const i -> Lam.const i

0 commit comments

Comments
 (0)