Skip to content

Commit 5daa9b1

Browse files
committed
more compact encoding
1 parent 9e50645 commit 5daa9b1

File tree

3 files changed

+63
-75
lines changed

3 files changed

+63
-75
lines changed

jscomp/bsb/bsb_db_encode.ml

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,32 @@ let make_encoding length buf : Ext_buffer.t -> int -> unit =
6363
Strictly speaking, [tmp_buf1] is not needed
6464
*)
6565
let encode_single (db : Bsb_db.map) (buf : Ext_buffer.t) =
66-
nl buf ; (* module name section *)
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-
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
8688
let encode (dbs : Bsb_db.t) buf =
8789
encode_single dbs.lib buf ;
8890
encode_single dbs.dev buf
89-
(* Ext_buffer.add_char_string buf '\n' (string_of_int (Array.length dbs));
90-
Ext_array.iter dbs (fun x -> encode_single x buf) *)
91-
91+
9292

9393
(* shall we avoid writing such file (checking the digest)?
9494
It is expensive to start scanning the whole code base,
@@ -99,9 +99,7 @@ let write_build_cache ~dir (bs_files : Bsb_db.t) : string =
9999
let oc = open_out_bin (Filename.concat dir bsbuild_cache) in
100100
let buf = Ext_buffer.create 100_000 in
101101
encode bs_files buf ;
102-
let digest = Ext_buffer.digest buf in
103-
let hex_digest = Digest.to_hex digest in
104-
output_string oc digest;
105102
Ext_buffer.output_buffer oc buf;
106103
close_out oc;
107-
hex_digest
104+
let digest = Ext_buffer.digest buf in
105+
Digest.to_hex digest

jscomp/bsb_helper/bsb_db_decode.ml

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,49 +25,49 @@
2525
let bsbuild_cache = Literals.bsbuild_cache
2626

2727

28-
type group = {
29-
modules : string array ;
30-
dir_length : int;
31-
dir_info_offset : int ;
32-
module_info_offset : int;
33-
}
28+
type group =
29+
| Dummy
30+
| Group of {
31+
modules : string array ;
32+
dir_length : int;
33+
dir_info_offset : int ;
34+
module_info_offset : int;
35+
}
3436

35-
type t = group array * string (* string is whole content*)
37+
type t = {
38+
lib : group ;
39+
dev : group ;
40+
content : string (* string is whole content*)
41+
}
3642

3743

3844
type cursor = int ref
3945

4046

4147
(*TODO: special case when module_count is zero *)
42-
let rec decode_internal (x : string) (offset : cursor) =
43-
(* let len = Ext_pervasives.parse_nat_of_string x offset in
44-
incr offset; *)
48+
let rec decode_internal (x : string) : t =
49+
let (offset : cursor) = ref 0 in
4550
let lib = decode_single x offset in
4651
let dev = decode_single x offset in
47-
[|lib;dev|]
48-
(* if len = 1 then [|first|]
49-
else
50-
let result = Array.make len first in
51-
for i = 1 to len - 1 do
52-
Array.unsafe_set result i (decode_single x offset)
53-
done ;
54-
result
55-
*)
52+
{lib; dev; content = x}
53+
5654
and decode_single (x : string) (offset : cursor) : group =
5755
let module_number = Ext_pervasives.parse_nat_of_string x offset in
5856
incr offset;
59-
let modules = decode_modules x offset module_number in
60-
let dir_info_offset = !offset in
61-
let module_info_offset =
62-
String.index_from x dir_info_offset '\n' + 1 in
63-
let dir_length = Char.code x.[module_info_offset] - 48 (* Char.code '0'*) in
64-
offset :=
65-
module_info_offset +
66-
1 +
67-
dir_length * module_number +
68-
1
57+
if module_number <> 0 then begin
58+
let modules = decode_modules x offset module_number in
59+
let dir_info_offset = !offset in
60+
let module_info_offset =
61+
String.index_from x dir_info_offset '\n' + 1 in
62+
let dir_length = Char.code x.[module_info_offset] - 48 (* Char.code '0'*) in
63+
offset :=
64+
module_info_offset +
65+
1 +
66+
dir_length * module_number +
67+
1
6968
;
70-
{ modules ; dir_info_offset; module_info_offset ; dir_length}
69+
Group { modules ; dir_info_offset; module_info_offset ; dir_length}
70+
end else Dummy
7171
and decode_modules (x : string) (offset : cursor) module_number : string array =
7272
let result = Array.make module_number "" in
7373
let last = ref !offset in
@@ -93,7 +93,7 @@ and decode_modules (x : string) (offset : cursor) module_number : string array =
9393
let read_build_cache ~dir : t =
9494
let all_content =
9595
Ext_io.load_file (Filename.concat dir bsbuild_cache) in
96-
decode_internal all_content (ref (Ext_digest.length + 1)), all_content
96+
decode_internal all_content
9797

9898

9999

@@ -104,11 +104,13 @@ type module_info = {
104104

105105

106106
let find_opt
107-
((sorteds,whole) : t )
108-
(i : int) (key : string)
107+
({content = whole} as db : t )
108+
lib (key : string)
109109
: module_info option =
110-
let group = sorteds.(i) in
111-
let i = Ext_string_array.find_sorted group.modules key in
110+
match if lib then db.lib else db.dev with
111+
| Dummy -> None
112+
| Group ({modules ;} as group) ->
113+
let i = Ext_string_array.find_sorted modules key in
112114
match i with
113115
| None -> None
114116
| Some count ->
@@ -134,10 +136,10 @@ let find_opt
134136
Some {case ; dir_name = String.sub whole dir_name_start (dir_name_finish - dir_name_start)}
135137

136138
let find db dependent_module is_not_lib_dir =
137-
let opt = find_opt db 0 dependent_module in
139+
let opt = find_opt db true dependent_module in
138140
match opt with
139141
| Some _ -> opt
140142
| None ->
141143
if is_not_lib_dir then
142-
find_opt db 1 dependent_module
144+
find_opt db false dependent_module
143145
else None

jscomp/bsb_helper/bsb_db_decode.mli

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,7 @@
2727

2828
type t
2929

30-
type group = {
31-
modules : string array ;
32-
dir_length : int ;
33-
dir_info_offset : int ;
34-
module_info_offset : int ;
35-
}
36-
37-
(* exposed only for testing *)
38-
val decode_internal :
39-
string ->
40-
int ref ->
41-
group array
42-
30+
type group
4331

4432

4533
val read_build_cache :

0 commit comments

Comments
 (0)