Skip to content

Commit

Permalink
Add init.compact_before_start.
Browse files Browse the repository at this point in the history
  • Loading branch information
toots committed Oct 29, 2024
1 parent 4f387c2 commit f26a670
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
7 changes: 5 additions & 2 deletions doc/content/language.md
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,11 @@ One side-benefit from loading a script from cache is that the entire typecheckin
This can result is significant reduction in the initial memory consumption, typically down from about `375MB` to about `80MB`!

If memory consumption is a concern but you are not sure you can cache your script, you can also set the environment variable
`LIQ_COMPACT_AFTER_TYPECHECK` to `true`.
`settings.init.compact_before_start` to `true`:

```liquidsoap
settings.init.compact_before_start := true
```

This will run the OCaml memory compaction algorithm after typechecking your script but before running it. This will result
in a similar memory footprint when running the script but will delay its initial startup time.
Expand All @@ -1525,4 +1529,3 @@ The following environment variables control the cache behavior:
- `LIQ_CACHE_USER_FILE_PERMS`: set the permissions used when creating a user cache file. Default: `0o600`
- `LIQ_CACHE_MAX_DAYS`: set the maximum days a cache file can be stored before it is eligible to be deleted during the next cache maintenance pass.
- `LIQ_CACHE_MAX_FILES`: set the maximum number of files in each cache directory. Older files are removed first.
- `LIQ_COMPACT_AFTER_TYPECHECK`: Set to compact memory after typechecking when caching is not available.
4 changes: 0 additions & 4 deletions src/core/hooks_implementations.ml
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,6 @@ let cache_max_files =
try int_of_string (Sys.getenv "LIQ_CACHE_MAX_FILES") with _ -> 20

let () =
(try
Liquidsoap_lang.Runtime.compact_after_typecheck :=
bool_of_string (Sys.getenv "LIQ_COMPACT_AFTER_TYPECHECK")
with _ -> ());
(try
Liquidsoap_lang.Cache.system_dir_perms :=
int_of_string (Sys.getenv "LIQ_CACHE_SYSTEM_DIR_PERMS")
Expand Down
5 changes: 1 addition & 4 deletions src/lang/runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,6 @@ let report :
throw exn;
default ())
let compact_after_typecheck = ref false
let type_term ?name ?stdlib ?term ?ty ?cache_dirtype ~cache ~trim ~lib
parsed_term =
let cached_term =
Expand Down Expand Up @@ -275,6 +273,7 @@ let type_term ?name ?stdlib ?term ?ty ?cache_dirtype ~cache ~trim ~lib
if Lazy.force Term.debug then
Printf.eprintf "Checking for unused variables...\n%!";
(* Check for unused variables, relies on types *)
report
~default:(fun () -> ())
(fun ~throw () -> Term.check_unused ~throw ~lib full_term);
Expand All @@ -283,8 +282,6 @@ let type_term ?name ?stdlib ?term ?ty ?cache_dirtype ~cache ~trim ~lib
in
if cache then
Term_cache.cache ?dirtype:cache_dirtype ~trim ~parsed_term full_term;
(* Check for unused variables, relies on types *)
if !compact_after_typecheck then Gc.compact ();
full_term
let eval_term ?name ~toplevel ast =
Expand Down
3 changes: 0 additions & 3 deletions src/lang/runtime.mli
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ val type_term :
Parsed_term.t ->
Term.t

(* If [true], OCaml memory is compacted after typecheck. *)
val compact_after_typecheck : bool ref

(** Evaluate a term. *)
val eval_term : ?name:string -> toplevel:bool -> Term.t -> Value.t

Expand Down
15 changes: 15 additions & 0 deletions src/libs/settings.liq
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,22 @@ def frame.duration =
settings.frame.duration
end

let settings.init.compact_before_start =
settings.make(
description=
"Run the OCaml memory compaction algorithm before starting your script. \
This is useful when script caching is not possible but initial memory \
consumption is a concern. This will result in a large chunk of memory \
being freed right before starting the script. This also increases the \
script's initial startup time.",
false
)

# Top-level init module for convenience.
# @category Settings
# @flag hidden
init = settings.init

on_start(
{if settings.init.compact_before_start() then runtime.gc.compact() end}
)

0 comments on commit f26a670

Please sign in to comment.