Skip to content

Commit 6c0a9e8

Browse files
authored
flambda-backend: Don't add a module to the environment when saving it (#1667)
When saving a .cmi, we produce all the same data we would if we were importing it and add this data to the environment. This is done only so that the CRC for the module is added to our CRCs and we can look up the CRC later. This is wholly unnecessary, however: we can just add the CRC to the `Consistbl`. Removing the extra steps * gets rid of a decidedly awkward code path, * keeps the environment clean (we currently never observe the fact that a saved .cmi is treated as bound in the environment, but it's not good!), and * will also enable further simplification, since `sign_of_cmi` now freshens unconditionally (the only call with `~freshen:false` is gone). This patch removes the saved .cmi from the persistent environment, and also changes `crc_of_unit` to look up in the global `crc_units` table first so that it bypasses `find_pers_struct` for a saved .cmi.
1 parent 562eb7b commit 6c0a9e8

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

typing/env.ml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -972,8 +972,6 @@ let sign_of_cmi ~freshen { Persistent_env.Persistent_signature.cmi; _ } =
972972

973973
let read_sign_of_cmi = sign_of_cmi ~freshen:true
974974

975-
let save_sign_of_cmi = sign_of_cmi ~freshen:false
976-
977975
let persistent_env : module_data Persistent_env.t ref =
978976
s_table Persistent_env.empty ()
979977

@@ -2670,10 +2668,8 @@ let save_signature_with_transform cmi_transform ~alerts sg modname filename =
26702668
let cmi =
26712669
Persistent_env.make_cmi !persistent_env modname sg alerts
26722670
|> cmi_transform in
2673-
let pm = save_sign_of_cmi
2674-
{ Persistent_env.Persistent_signature.cmi; filename } in
26752671
Persistent_env.save_cmi !persistent_env
2676-
{ Persistent_env.Persistent_signature.filename; cmi } pm;
2672+
{ Persistent_env.Persistent_signature.filename; cmi };
26772673
cmi
26782674

26792675
let save_signature ~alerts sg modname filename =

typing/persistent_env.ml

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,9 @@ let fold {persistent_structures; _} f x =
168168

169169
(* Reading persistent structures from .cmi files *)
170170

171-
let save_pers_struct penv crc ps pm =
172-
let {persistent_structures; crc_units; _} = penv in
171+
let save_pers_struct penv crc ps =
172+
let {crc_units; _} = penv in
173173
let modname = CU.name ps.ps_name in
174-
Hashtbl.add persistent_structures modname (Found (ps, pm));
175174
List.iter
176175
(function
177176
| Rectypes -> ()
@@ -334,13 +333,16 @@ module Array = struct
334333
end
335334

336335
let crc_of_unit penv f name =
337-
let (ps, _pm) = find_pers_struct penv f true name in
338-
match Array.find_opt (Import_info.has_name ~name) ps.ps_crcs with
339-
| None -> assert false
340-
| Some import_info ->
341-
match Import_info.crc import_info with
336+
match Consistbl.find penv.crc_units name with
337+
| Some (_, crc) -> crc
338+
| None ->
339+
let (ps, _pm) = find_pers_struct penv f true name in
340+
match Array.find_opt (Import_info.has_name ~name) ps.ps_crcs with
342341
| None -> assert false
343-
| Some crc -> crc
342+
| Some import_info ->
343+
match Import_info.crc import_info with
344+
| None -> assert false
345+
| Some crc -> crc
344346

345347
let imports {imported_units; crc_units; _} =
346348
let imports =
@@ -377,7 +379,7 @@ let make_cmi penv modname sign alerts =
377379
cmi_flags = flags
378380
}
379381

380-
let save_cmi penv psig pm =
382+
let save_cmi penv psig =
381383
let { Persistent_signature.filename; cmi } = psig in
382384
Misc.try_finally (fun () ->
383385
let {
@@ -390,7 +392,7 @@ let save_cmi penv psig pm =
390392
output_to_file_via_temporary (* see MPR#7472, MPR#4991 *)
391393
~mode: [Open_binary] filename
392394
(fun temp_filename oc -> output_cmi temp_filename oc cmi) in
393-
(* Enter signature in persistent table so that imports()
395+
(* Enter signature in consistbl so that imports()
394396
will also return its crc *)
395397
let ps =
396398
{ ps_name = modname;
@@ -401,7 +403,7 @@ let save_cmi penv psig pm =
401403
ps_filename = filename;
402404
ps_flags = flags;
403405
} in
404-
save_pers_struct penv crc ps pm
406+
save_pers_struct penv crc ps
405407
)
406408
~exceptionally:(fun () -> remove_file filename)
407409

typing/persistent_env.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ val register_import_as_opaque : 'a t -> Compilation_unit.Name.t -> unit
8989
val make_cmi : 'a t -> Compilation_unit.t -> Subst.Lazy.signature -> alerts
9090
-> Cmi_format.cmi_infos_lazy
9191

92-
val save_cmi : 'a t -> Persistent_signature.t -> 'a -> unit
92+
val save_cmi : 'a t -> Persistent_signature.t -> unit
9393

9494
val can_load_cmis : 'a t -> can_load_cmis
9595
val set_can_load_cmis : 'a t -> can_load_cmis -> unit

0 commit comments

Comments
 (0)