Skip to content

Commit 33a04a6

Browse files
authored
flambda-backend: Attempt to shrink the heap before calling the assembler (ocaml#429)
* Attempt to shrink the heap before calling the assembler We have a fair amount of global state that's never collected, which is causing trouble in large parallel builds, particularly when large assembly files are generated, causing `as` to need a lot of memory itself. This patch simply clears out some data once it's no longer needed and, when memory use is high, calls `Gc.compact` to try and release some memory back to the OS. This has been observed to reduce the heap size by some 640MB (for an unusually large generated .ml file). This PR still leaves some relatively low-hanging fruit: after to_cmm, we can marshal the .cmx eagerly and clear out the exported info. However, it's not obvious how to handle this cleanly, and anyway one can use `-Oclassic` to avoid producing a large .cmx to begin with.
1 parent 8a36a16 commit 33a04a6

File tree

8 files changed

+30
-8
lines changed

8 files changed

+30
-8
lines changed

driver/compenv.ml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,11 +621,13 @@ let c_object_of_filename name =
621621
Filename.chop_suffix (Filename.basename name) ".c" ^ Config.ext_obj
622622

623623
let process_action
624-
(ppf, implementation, interface, ocaml_mod_ext, ocaml_lib_ext) action =
624+
(ppf, implementation, interface, ocaml_mod_ext, ocaml_lib_ext) action
625+
~keep_symbol_tables =
625626
let impl ~start_from name =
626627
readenv ppf (Before_compile name);
627628
let opref = output_prefix name in
628-
implementation ~start_from ~source_file:name ~output_prefix:opref;
629+
implementation ~start_from ~source_file:name ~output_prefix:opref
630+
~keep_symbol_tables;
629631
objfiles := (opref ^ ocaml_mod_ext) :: !objfiles
630632
in
631633
match action with
@@ -709,7 +711,14 @@ let process_deferred_actions env =
709711
| ProcessOtherFile name -> Filename.check_suffix name ".cmxa"
710712
| _ -> false) !deferred_actions then
711713
fatal "Option -a cannot be used with .cmxa input files.";
712-
List.iter (process_action env) (List.rev !deferred_actions);
714+
let compiling_multiple_impls =
715+
List.length (List.filter (function
716+
| ProcessImplementation _ -> true
717+
| _ -> false) !deferred_actions) > 1
718+
in
719+
let keep_symbol_tables = compiling_multiple_impls in
720+
List.iter (process_action env ~keep_symbol_tables)
721+
(List.rev !deferred_actions);
713722
output_name := final_output_name;
714723
stop_early :=
715724
!compile_only ||

driver/compenv.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ val intf : string -> unit
8282
val process_deferred_actions :
8383
Format.formatter *
8484
(start_from:Clflags.Compiler_pass.t ->
85-
source_file:string -> output_prefix:string -> unit) *
85+
source_file:string -> output_prefix:string ->
86+
keep_symbol_tables:bool -> unit) *
8687
(* compile implementation *)
8788
(source_file:string -> output_prefix:string -> unit) *
8889
(* compile interface *)

driver/compile.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ let emit_bytecode i (bytecode, required_globals) =
5757
(Emitcode.to_file oc i.module_name cmofile ~required_globals);
5858
)
5959

60-
let implementation ~start_from ~source_file ~output_prefix =
60+
let implementation ~start_from ~source_file ~output_prefix
61+
~keep_symbol_tables:_ =
6162
let backend info typed =
6263
let bytecode = to_bytecode info typed in
6364
emit_bytecode info bytecode

driver/compile.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ val interface:
1919
source_file:string -> output_prefix:string -> unit
2020
val implementation:
2121
start_from:Clflags.Compiler_pass.t ->
22-
source_file:string -> output_prefix:string -> unit
22+
source_file:string -> output_prefix:string -> keep_symbol_tables:bool -> unit
2323

2424
(** {2 Internal functions} **)
2525

driver/optcompile.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ let emit i =
8787
Compilenv.reset ?packname:!Clflags.for_package i.module_name;
8888
Asmgen.compile_implementation_linear i.output_prefix ~progname:i.source_file
8989

90-
let implementation ~backend ~start_from ~source_file ~output_prefix =
90+
let implementation ~backend ~start_from ~source_file
91+
~output_prefix ~keep_symbol_tables:_ =
9192
let backend info typed =
9293
Compilenv.reset ?packname:!Clflags.for_package info.module_name;
9394
if Config.flambda

driver/optcompile.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ val interface: source_file:string -> output_prefix:string -> unit
2020
val implementation:
2121
backend:(module Backend_intf.S)
2222
-> start_from:Clflags.Compiler_pass.t
23-
-> source_file:string -> output_prefix:string -> unit
23+
-> source_file:string -> output_prefix:string -> keep_symbol_tables:bool
24+
-> unit
2425

2526
(** {2 Internal functions} **)
2627

typing/typemod.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,3 +2943,8 @@ let () =
29432943
| _ ->
29442944
None
29452945
)
2946+
2947+
let reset () =
2948+
Env.reset_cache ();
2949+
Envaux.reset_cache ();
2950+
Typetexp.reset_type_variables ()

typing/typemod.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,7 @@ exception Error of Location.t * Env.t * error
136136
exception Error_forward of Location.error
137137

138138
val report_error: Env.t -> formatter -> error -> unit
139+
140+
(** Clear several bits of global state that may retain large amounts of memory
141+
after typechecking is finished. *)
142+
val reset : unit -> unit

0 commit comments

Comments
 (0)