Skip to content

Commit 77c9f7f

Browse files
committed
cmj cache
1 parent 57c44b2 commit 77c9f7f

File tree

14 files changed

+64
-356
lines changed

14 files changed

+64
-356
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ jobs:
123123
if: runner.os == 'Linux'
124124
uses: actions/upload-artifact@v3
125125
with:
126-
name: lib-cmi-cache
127-
path: lib/cmi_cache.bin
126+
name: cmij-cache
127+
path: |
128+
lib/cmi_cache.bin
129+
lib/cmj_cache.bin
128130
129131
- name: Check for changes in lib folder
130132
run: git diff --exit-code lib/js lib/es6

jscomp/cmij/cmij.ml

Lines changed: 0 additions & 1 deletion
This file was deleted.

jscomp/cmij/cmij.mli

Lines changed: 0 additions & 1 deletion
This file was deleted.

jscomp/cmij/cmij_cache.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type t = { module_names : string array; module_data : bytes array }
2+
type cmi_data = Cmi_format.cmi_infos
3+
type cmj_data = { values : Js_cmj_format.keyed_cmj_value array; pure : bool }
4+
5+
let marshal_cmi_data (cmi_data : cmi_data) = Marshal.to_bytes cmi_data []
6+
let marshal_cmj_data (cmj_data : cmj_data) = Marshal.to_bytes cmj_data []
7+
let unmarshal_cmi_data bytes : cmi_data = Marshal.from_bytes bytes 0
8+
let unmarshal_cmj_data bytes : cmj_data = Marshal.from_bytes bytes 0

jscomp/cmij/cmij_cache.mli

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type t = { module_names : string array; module_data : bytes array }
2+
type cmi_data = Cmi_format.cmi_infos
3+
type cmj_data = { values : Js_cmj_format.keyed_cmj_value array; pure : bool }
4+
5+
val marshal_cmi_data : cmi_data -> bytes
6+
val marshal_cmj_data : cmj_data -> bytes
7+
val unmarshal_cmi_data : bytes -> cmi_data
8+
val unmarshal_cmj_data : bytes -> cmj_data

jscomp/cmij/cmij_main.ml

Lines changed: 19 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,17 @@ let get_files ext dir =
3030
if Ext_string.ends_with x ext then Some (Filename.concat dir x) else None)
3131
|> Array.to_list
3232

33-
(** the cache should be readable and also update *)
34-
let check_digest output_file digest : bool =
35-
if Sys.file_exists output_file then
36-
match
37-
Ext_list.filter
38-
(String.split_on_char ' ' (Ext_io.load_file output_file))
39-
(fun x -> x <> "")
40-
with
41-
| _head :: old_digest :: _tail -> Digest.equal digest old_digest
42-
| _ -> false
43-
else false
44-
45-
let ( +> ) = Ext_buffer.add_string
46-
4733
let from_cmj ~mode (files : string list) (output_file : string) : unit =
4834
let files =
4935
let cmp = Ext_filename.module_name in
50-
List.sort
51-
(fun filea fileb -> Ext_string_array.cmp (cmp filea) (cmp fileb))
52-
files
36+
files
37+
|> List.sort (fun filea fileb ->
38+
Ext_string_array.cmp (cmp filea) (cmp fileb))
39+
|> Array.of_list
5340
in
54-
let buf = Ext_buffer.create 10000 in
55-
56-
let abs =
57-
Ext_list.map files (fun file ->
58-
let module_name = Ext_filename.module_name file in
41+
let module_names = Ext_array.map files Ext_filename.module_name in
42+
let module_data =
43+
Ext_array.map files (fun file ->
5944
let content : Js_cmj_format.t = Js_cmj_format.from_file file in
6045
let () =
6146
match mode with
@@ -72,29 +57,13 @@ let from_cmj ~mode (files : string list) (output_file : string) : unit =
7257
| Playground _ -> ()
7358
in
7459
(* prerr_endline (Ext_obj.dump content.package_spec); *)
75-
let c = Marshal.to_string (content.values, content.pure) [] in
76-
( Printf.sprintf {|%S (* %d *)|} module_name (String.length c),
77-
Printf.sprintf {|(* %s *)%S|} module_name c ))
60+
let { Js_cmj_format.values; pure } = content in
61+
Cmij_cache.marshal_cmj_data { values; pure })
7862
in
79-
buf
80-
+> Printf.sprintf
81-
{|let module_names : string array = Obj.magic (
82-
%s
83-
)
84-
let module_data : string array = Obj.magic (
85-
%s
86-
)
87-
|}
88-
(String.concat ",\n" (Ext_list.map abs fst))
89-
(String.concat ",\n" (Ext_list.map abs snd));
90-
buf +> "\n";
91-
let digest = Digest.to_hex (Ext_buffer.digest buf) in
92-
let same = check_digest output_file digest in
93-
if not same then
94-
let v = open_out_bin output_file in
95-
Ext_pervasives.finally v ~clean:close_out (fun f ->
96-
output_string f ("(* " ^ digest ^ " *) \n");
97-
Ext_buffer.output_buffer f buf)
63+
let cmj_cache = { Cmij_cache.module_names; module_data } in
64+
let v = open_out_bin output_file in
65+
Ext_pervasives.finally v ~clean:close_out (fun f ->
66+
Marshal.to_channel f cmj_cache [])
9867

9968
let from_cmi files output_file =
10069
let files =
@@ -110,21 +79,19 @@ let from_cmi files output_file =
11079
let module_name = Ext_filename.module_name file in
11180
let cmi = Cmi_format.read_cmi file in
11281
assert (cmi.cmi_name = module_name);
113-
let content = Marshal.to_bytes cmi [] in
114-
content)
82+
Cmij_cache.marshal_cmi_data cmi)
11583
in
116-
let cmi_cache = { Cmij.module_names; module_data } in
84+
let cmi_cache = { Cmij_cache.module_names; module_data } in
11785
let v = open_out_bin output_file in
11886
Ext_pervasives.finally v ~clean:close_out (fun f ->
11987
Marshal.to_channel f cmi_cache [])
12088

12189
let stdlib = "stdlib-406"
12290
let ( // ) = Filename.concat
12391
let ( |~ ) = Ext_string.contain_substring
124-
125-
let cmi_target_file =
126-
Filename.dirname Sys.argv.(0) // ".." // ".." // "lib" // "cmi_cache.bin"
127-
92+
let lib_dir = Filename.dirname Sys.argv.(0) // ".." // ".." // "lib"
93+
let cmi_target_file = lib_dir // "cmi_cache.bin"
94+
let cmj_target_file = lib_dir // "cmj_cache.bin"
12895
let release_cmi = Array.exists (( = ) "-release") Sys.argv
12996

13097
let () =
@@ -158,9 +125,7 @@ let () =
158125
(Filename.dirname Sys.argv.(0) // ".." // "others")
159126
@ third_party_cmj_files
160127
in
161-
from_cmj ~mode cmj_files
162-
(Filename.dirname Sys.argv.(0)
163-
// ".." // "core" // "builtin_cmj_datasets.ml");
128+
from_cmj ~mode cmj_files cmj_target_file;
164129
let third_party_cmi_files =
165130
match mode with
166131
| Native -> []

jscomp/core/bs_cmi_load.ml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@
2525
#ifdef RELEASE
2626
(*true *)
2727

28-
type cmi_cache = {module_names : string array; module_data : bytes array}
29-
3028
let cmi_cache_path =
3129
let ( // ) = Filename.concat in
3230
Config.standard_library // Filename.parent_dir_name // "cmi_cache.bin"
3331

3432
let load_cmi_cache () =
3533
let channel = open_in_bin cmi_cache_path in
36-
let cache: cmi_cache = Marshal.from_channel channel in
34+
let cache: Cmij_cache.t = Marshal.from_channel channel in
3735
close_in channel;
3836
cache
3937

@@ -45,13 +43,12 @@ let load_cmi ~unit_name : Env.Persistent_signature.t option =
4543
| None ->
4644
if !Js_config.no_stdlib then None
4745
else
48-
let {module_names; module_data} = Lazy.force cmi_cache in
46+
let {Cmij_cache.module_names; module_data} = Lazy.force cmi_cache in
4947
match Ext_string_array.find_sorted module_names unit_name with
50-
| Some cmi ->
48+
| Some index ->
5149
if Js_config.get_diagnose () then
5250
Format.fprintf Format.err_formatter ">Cmi: %s@." unit_name;
53-
let cmi : Cmi_format.cmi_infos =
54-
Marshal.from_bytes module_data.(cmi) 0 in
51+
let cmi = Cmij_cache.unmarshal_cmi_data module_data.(index) in
5552
if Js_config.get_diagnose () then
5653
Format.fprintf Format.err_formatter "<Cmi: %s@." unit_name;
5754
Some {filename = Sys.executable_name ;

jscomp/core/builtin_cmj_datasets.ml

Lines changed: 0 additions & 276 deletions
This file was deleted.

jscomp/core/builtin_cmj_datasets.mli

Lines changed: 0 additions & 9 deletions
This file was deleted.

jscomp/core/dune

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@
1111
; There exist cyclic dependencies between core and frontend, therefore include frontend files here:
1212

1313
(copy_files# ../frontend/*.{ml,mli})
14+
15+
(copy_files# ../cmij/cmij_cache.{ml,mli})

0 commit comments

Comments
 (0)