Skip to content

Commit be85cb9

Browse files
committed
new encoding for mlast, more efficient, readable
1 parent 2bc9217 commit be85cb9

File tree

7 files changed

+51
-51
lines changed

7 files changed

+51
-51
lines changed

jscomp/bsb_helper/bsb_helper_depfile_gen.ml

+17-15
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,28 @@ let write_file name (buf : Ext_buffer.t) =
5151
(* Make sure it is the same as {!Binary_ast.magic_sep_char}*)
5252
let magic_sep_char = '\n'
5353

54-
let deps_of_channel (ic : in_channel) : string array =
54+
let deps_of_channel (ic : in_channel) : string list =
5555
let size = input_binary_int ic in
56-
let s = really_input_string ic size in
57-
let first_tab = String.index s magic_sep_char in
58-
let return_arr = Array.make (int_of_string (String.sub s 0 first_tab)) "" in
59-
let rec aux s ith (offset : int) : unit =
56+
let s = really_input_string ic size in
57+
let rec aux (s : string) acc (offset : int) size : string list =
6058
if offset < size then
61-
let next_tab = String.index_from s offset magic_sep_char in
62-
return_arr.(ith) <- String.sub s offset (next_tab - offset) ;
63-
aux s (ith + 1) (next_tab + 1)
59+
let next_tab = String.index_from s offset magic_sep_char in
60+
aux s
61+
(String.sub s offset (next_tab - offset)::acc) (next_tab + 1)
62+
size
63+
else acc
6464
in
65-
aux s 0 (first_tab + 1) ;
65+
aux s [] 1 size
66+
67+
68+
6669

67-
return_arr
6870

6971
(** Please refer to {!Binary_ast} for encoding format, we move it here
7072
mostly for cutting the dependency so that [bsb_helper.exe] does
7173
not depend on compler-libs
7274
*)
73-
let read_deps (fn : string) : string array =
75+
let read_deps (fn : string) : string list =
7476
let ic = open_in_bin fn in
7577
let v = deps_of_channel ic in
7678
close_in ic;
@@ -123,7 +125,7 @@ let find_module db dependent_module is_not_lib_dir (index : Bsb_dir_index.t) =
123125
Bsb_db_decode.find_opt db (index :> int) dependent_module
124126
else None
125127
let oc_impl
126-
(dependent_module_set : string array)
128+
(dependent_module_set : string list)
127129
(input_file : string)
128130
(index : Bsb_dir_index.t)
129131
(db : Bsb_db_decode.t)
@@ -145,7 +147,7 @@ let oc_impl
145147
Ext_buffer.add_string buf Literals.suffix_cmi;
146148
) ; (* TODO: moved into static files*)
147149
let is_not_lib_dir = not (Bsb_dir_index.is_lib_dir index) in
148-
Ext_array.iter dependent_module_set (fun dependent_module ->
150+
Ext_list.iter dependent_module_set (fun dependent_module ->
149151
match
150152
find_module db dependent_module is_not_lib_dir index
151153
with
@@ -165,7 +167,7 @@ let oc_impl
165167
[.cmi] file
166168
*)
167169
let oc_intf
168-
(dependent_module_set : string array)
170+
(dependent_module_set : string list)
169171
input_file
170172
(index : Bsb_dir_index.t)
171173
(db : Bsb_db_decode.t)
@@ -183,7 +185,7 @@ let oc_intf
183185
Ext_buffer.add_string buf Literals.suffix_cmi;
184186
) ;
185187
let is_not_lib_dir = not (Bsb_dir_index.is_lib_dir index) in
186-
Ext_array.iter dependent_module_set begin fun dependent_module ->
188+
Ext_list.iter dependent_module_set begin fun dependent_module ->
187189
match find_module db dependent_module is_not_lib_dir index
188190
with
189191
| None -> ()

jscomp/bsb_helper/bsb_helper_depfile_gen.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type kind = Js | Bytecode | Native
2727
(** [deps_of_channel ic]
2828
given an input_channel dumps all modules it depend on, only used for debugging
2929
*)
30-
val deps_of_channel : in_channel -> string array
30+
val deps_of_channel : in_channel -> string list
3131

3232
(**
3333
[make compilation_kind filename index namespace]

jscomp/depends/binary_ast.ml

+7-9
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@
2323
* along with this program; if not, write to the Free Software
2424
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2525

26+
(** Synced up with module {!Bsb_helper_depfile_gen} *)
2627
module String_set = Ast_extract.String_set
2728

2829

2930

30-
31-
32-
3331
let read_ast (type t ) (kind : t Ml_binary.kind) fn : t =
3432
let ic = open_in_bin fn in
3533
try
@@ -50,14 +48,14 @@ let magic_sep_char = '\n'
5048
*)
5149
let write_ast (type t) ~(fname : string) ~output (kind : t Ml_binary.kind) ( pt : t) : unit =
5250
let oc = open_out_bin output in
53-
5451
let output_set = Ast_extract.read_parse_and_extract kind pt in
55-
let buf = Ext_buffer.create 64 in
56-
let number = String_set.cardinal output_set in
57-
Ext_buffer.add_string buf (string_of_int number) ;
52+
let buf = Ext_buffer.create 1000 in
53+
5854
Ext_buffer.add_char buf magic_sep_char;
59-
String_set.iter (fun s -> Ext_buffer.add_string buf s ; Ext_buffer.add_char buf magic_sep_char) output_set ;
60-
output_binary_int oc (Ext_buffer.length buf);
55+
String_set.iter (fun s -> Ext_buffer.add_string buf s ;
56+
Ext_buffer.add_char buf magic_sep_char) output_set ;
57+
58+
output_binary_int oc (Ext_buffer.length buf);
6159
Ext_buffer.output_buffer oc buf;
6260
Ml_binary.write_ast kind fname pt oc;
6361
close_out oc

jscomp/main/astdump_main.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ let () =
3737
(Format.pp_print_list ~pp_sep:(fun fmt () ->
3838
p fmt "@ ;"
3939
) Format.pp_print_string)
40-
(Array.to_list arrs);
40+
arrs;
4141
p Format.std_formatter "@.==============================@.";
4242
if Ext_path.check_suffix_case file ".mlast" then
4343
begin

lib/4.02.3/bsb_helper.ml

+18-16
Original file line numberDiff line numberDiff line change
@@ -4561,7 +4561,7 @@ type kind = Js | Bytecode | Native
45614561
(** [deps_of_channel ic]
45624562
given an input_channel dumps all modules it depend on, only used for debugging
45634563
*)
4564-
val deps_of_channel : in_channel -> string array
4564+
val deps_of_channel : in_channel -> string list
45654565

45664566
(**
45674567
[make compilation_kind filename index namespace]
@@ -4636,26 +4636,28 @@ let write_file name (buf : Ext_buffer.t) =
46364636
(* Make sure it is the same as {!Binary_ast.magic_sep_char}*)
46374637
let magic_sep_char = '\n'
46384638

4639-
let deps_of_channel (ic : in_channel) : string array =
4639+
let deps_of_channel (ic : in_channel) : string list =
46404640
let size = input_binary_int ic in
4641-
let s = really_input_string ic size in
4642-
let first_tab = String.index s magic_sep_char in
4643-
let return_arr = Array.make (int_of_string (String.sub s 0 first_tab)) "" in
4644-
let rec aux s ith (offset : int) : unit =
4641+
let s = really_input_string ic size in
4642+
let rec aux (s : string) acc (offset : int) size : string list =
46454643
if offset < size then
4646-
let next_tab = String.index_from s offset magic_sep_char in
4647-
return_arr.(ith) <- String.sub s offset (next_tab - offset) ;
4648-
aux s (ith + 1) (next_tab + 1)
4644+
let next_tab = String.index_from s offset magic_sep_char in
4645+
aux s
4646+
(String.sub s offset (next_tab - offset)::acc) (next_tab + 1)
4647+
size
4648+
else acc
46494649
in
4650-
aux s 0 (first_tab + 1) ;
4650+
aux s [] 1 size
4651+
4652+
4653+
46514654

4652-
return_arr
46534655

46544656
(** Please refer to {!Binary_ast} for encoding format, we move it here
46554657
mostly for cutting the dependency so that [bsb_helper.exe] does
46564658
not depend on compler-libs
46574659
*)
4658-
let read_deps (fn : string) : string array =
4660+
let read_deps (fn : string) : string list =
46594661
let ic = open_in_bin fn in
46604662
let v = deps_of_channel ic in
46614663
close_in ic;
@@ -4708,7 +4710,7 @@ let find_module db dependent_module is_not_lib_dir (index : Bsb_dir_index.t) =
47084710
Bsb_db_decode.find_opt db (index :> int) dependent_module
47094711
else None
47104712
let oc_impl
4711-
(dependent_module_set : string array)
4713+
(dependent_module_set : string list)
47124714
(input_file : string)
47134715
(index : Bsb_dir_index.t)
47144716
(db : Bsb_db_decode.t)
@@ -4730,7 +4732,7 @@ let oc_impl
47304732
Ext_buffer.add_string buf Literals.suffix_cmi;
47314733
) ; (* TODO: moved into static files*)
47324734
let is_not_lib_dir = not (Bsb_dir_index.is_lib_dir index) in
4733-
Ext_array.iter dependent_module_set (fun dependent_module ->
4735+
Ext_list.iter dependent_module_set (fun dependent_module ->
47344736
match
47354737
find_module db dependent_module is_not_lib_dir index
47364738
with
@@ -4750,7 +4752,7 @@ let oc_impl
47504752
[.cmi] file
47514753
*)
47524754
let oc_intf
4753-
(dependent_module_set : string array)
4755+
(dependent_module_set : string list)
47544756
input_file
47554757
(index : Bsb_dir_index.t)
47564758
(db : Bsb_db_decode.t)
@@ -4768,7 +4770,7 @@ let oc_intf
47684770
Ext_buffer.add_string buf Literals.suffix_cmi;
47694771
) ;
47704772
let is_not_lib_dir = not (Bsb_dir_index.is_lib_dir index) in
4771-
Ext_array.iter dependent_module_set begin fun dependent_module ->
4773+
Ext_list.iter dependent_module_set begin fun dependent_module ->
47724774
match find_module db dependent_module is_not_lib_dir index
47734775
with
47744776
| None -> ()

lib/4.02.3/whole_compiler.ml

+7-9
Original file line numberDiff line numberDiff line change
@@ -28738,13 +28738,11 @@ end = struct
2873828738
* along with this program; if not, write to the Free Software
2873928739
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2874028740

28741+
(** Synced up with module {!Bsb_helper_depfile_gen} *)
2874128742
module String_set = Ast_extract.String_set
2874228743

2874328744

2874428745

28745-
28746-
28747-
2874828746
let read_ast (type t ) (kind : t Ml_binary.kind) fn : t =
2874928747
let ic = open_in_bin fn in
2875028748
try
@@ -28765,14 +28763,14 @@ let magic_sep_char = '\n'
2876528763
*)
2876628764
let write_ast (type t) ~(fname : string) ~output (kind : t Ml_binary.kind) ( pt : t) : unit =
2876728765
let oc = open_out_bin output in
28768-
2876928766
let output_set = Ast_extract.read_parse_and_extract kind pt in
28770-
let buf = Ext_buffer.create 64 in
28771-
let number = String_set.cardinal output_set in
28772-
Ext_buffer.add_string buf (string_of_int number) ;
28767+
let buf = Ext_buffer.create 1000 in
28768+
2877328769
Ext_buffer.add_char buf magic_sep_char;
28774-
String_set.iter (fun s -> Ext_buffer.add_string buf s ; Ext_buffer.add_char buf magic_sep_char) output_set ;
28775-
output_binary_int oc (Ext_buffer.length buf);
28770+
String_set.iter (fun s -> Ext_buffer.add_string buf s ;
28771+
Ext_buffer.add_char buf magic_sep_char) output_set ;
28772+
28773+
output_binary_int oc (Ext_buffer.length buf);
2877628774
Ext_buffer.output_buffer oc buf;
2877728775
Ml_binary.write_ast kind fname pt oc;
2877828776
close_out oc

lib/bstracing

100644100755
File mode changed.

0 commit comments

Comments
 (0)