@@ -40,7 +40,7 @@ let nl buf =
4040 - not readable
4141 *)
4242
43- let make_encoding length buf =
43+ let make_encoding length buf : Ext_buffer.t -> int -> unit =
4444 let max_range = length lsl 1 + 1 in
4545 if max_range < = 0xff then begin
4646 Ext_buffer. add_char buf '1' ;
@@ -62,41 +62,44 @@ let make_encoding length buf =
6262 they are only used to control the order.
6363 Strictly speaking, [tmp_buf1] is not needed
6464*)
65- let encode_single (db : Bsb_db.t ) (buf : Ext_buffer.t ) =
66- nl buf ; (* module name section *)
65+ let encode_single (db : Bsb_db.map ) (buf : Ext_buffer.t ) =
66+ (* module name section *)
6767 let len = Map_string. cardinal db in
6868 Ext_buffer. add_string_char buf (string_of_int len) '\n' ;
69- let mapping = Hash_string. create 50 in
70- Map_string. iter db (fun name {dir} ->
71- Ext_buffer. add_string_char buf name '\n' ;
72- if not (Hash_string. mem mapping dir) then
73- Hash_string. add mapping dir (Hash_string. length mapping)
74- );
75- let length = Hash_string. length mapping in
76- let rev_mapping = Array. make length " " in
77- Hash_string. iter mapping (fun k i -> Array. unsafe_set rev_mapping i k);
78- (* directory name section *)
79- Ext_array. iter rev_mapping (fun s -> Ext_buffer. add_string_char buf s '\t' );
80- nl buf; (* module name info section *)
81- let len_encoding = make_encoding length buf in
82- Map_string. iter db (fun _ module_info ->
83- len_encoding buf
84- (Hash_string. find_exn mapping module_info.dir lsl 1 + Obj. magic module_info.case ))
85-
86- let encode (dbs : Bsb_db.ts ) buf =
87-
88- Ext_buffer. add_char_string buf '\n' (string_of_int (Array. length dbs));
89- Ext_array. iter dbs (fun x -> encode_single x buf)
90-
69+ if len <> 0 then begin
70+ let mapping = Hash_string. create 50 in
71+ Map_string. iter db (fun name {dir} ->
72+ Ext_buffer. add_string_char buf name '\n' ;
73+ if not (Hash_string. mem mapping dir) then
74+ Hash_string. add mapping dir (Hash_string. length mapping)
75+ );
76+ let length = Hash_string. length mapping in
77+ let rev_mapping = Array. make length " " in
78+ Hash_string. iter mapping (fun k i -> Array. unsafe_set rev_mapping i k);
79+ (* directory name section *)
80+ Ext_array. iter rev_mapping (fun s -> Ext_buffer. add_string_char buf s '\t' );
81+ nl buf; (* module name info section *)
82+ let len_encoding = make_encoding length buf in
83+ Map_string. iter db (fun _ module_info ->
84+ len_encoding buf
85+ (Hash_string. find_exn mapping module_info.dir lsl 1 + Obj. magic module_info.case ));
86+ nl buf
87+ end
88+ let encode (dbs : Bsb_db.t ) buf =
89+ encode_single dbs.lib buf ;
90+ encode_single dbs.dev buf
91+
9192
92- (* TODO: shall we avoid writing such file (checking the digest) *)
93- let write_build_cache ~dir (bs_files : Bsb_db.ts ) : string =
93+ (* shall we avoid writing such file (checking the digest)?
94+ It is expensive to start scanning the whole code base,
95+ we should we avoid it in the first place, if we do start scanning,
96+ this operation seems affordable
97+ *)
98+ let write_build_cache ~dir (bs_files : Bsb_db.t ) : string =
9499 let oc = open_out_bin (Filename. concat dir bsbuild_cache) in
95100 let buf = Ext_buffer. create 100_000 in
96101 encode bs_files buf ;
97- let digest = Ext_buffer. digest buf in
98- let hex_digest = Digest. to_hex digest in
99- output_string oc digest;
100102 Ext_buffer. output_buffer oc buf;
101103 close_out oc;
102- hex_digest
104+ let digest = Ext_buffer. digest buf in
105+ Digest. to_hex digest
0 commit comments