Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/build-trace.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Write a timeline of the build, viewable in Perfetto, to the file that `RESCRIPT_TRACE` names.
2 changes: 2 additions & 0 deletions changes/build-without-artifact-files.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add an experimental build mode, `RESCRIPT_NO_ARTIFACTS=1`, that keeps ASTs,
interfaces and other artifacts in memory and writes only the JavaScript.
5 changes: 5 additions & 0 deletions compiler/driver/compile_phase_hook.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let key : (unit -> unit) Domain.DLS.key = Domain.DLS.new_key (fun () () -> ())

let set_typing_finished hook = Domain.DLS.set key hook

let typing_finished () = Domain.DLS.get key ()
11 changes: 11 additions & 0 deletions compiler/driver/compile_phase_hook.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(** Lets an embedding build system see where compiling an implementation moves
from type checking to code generation, for example to time the two parts
(the build trace of [RESCRIPT_TRACE]). The hook belongs to the domain that
runs the compile and does nothing by default. *)

val set_typing_finished : (unit -> unit) -> unit
(** Installs the hook for the current domain. *)

val typing_finished : unit -> unit
(** Called once an implementation is type-checked, before it is translated to
Lambda and compiled to JavaScript. *)
1 change: 1 addition & 0 deletions compiler/driver/dune
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
(modules
rescript_compiler_driver
rescript_compiler_build_identity
compile_phase_hook
js_implementation
ext_file_extensions)
(instrumentation
Expand Down
15 changes: 11 additions & 4 deletions compiler/driver/js_implementation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ let after_parsing_impl ppf outputprefix (ast : Parsetree.structure) =
(Loc_report.get_input_name ())
outputprefix modulename env ast
in
Compile_phase_hook.typing_finished ();
let typedtree_coercion = (typedtree, coercion) in
print_if ppf (Clflags.current ()).dump_typedtree
Printtyped.implementation_with_coercion typedtree_coercion;
Expand Down Expand Up @@ -183,10 +184,16 @@ let make_structure_item ~ns cunit : Parsetree.structure_item =
*)
let implementation_map ppf sourcefile =
let () = (Js_config.current ()).cmj_only := true in
let ichan = open_in_bin (Compiler_request_state.resolve_path sourcefile) in
seek_in ichan (Ext_digest.length + 1);
let list_of_modules = File_io.rev_lines_of_chann ichan in
close_in ichan;
(* Read through the build file store, which may keep the map in memory
only. *)
let contents = File_io.load_file sourcefile in
let header = Ext_digest.length + 1 in
let list_of_modules =
if String.length contents <= header then []
else
String.sub contents header (String.length contents - header)
|> String.split_on_char '\n' |> List.rev
in
let ns = Ext_filename.module_name (Filename.remove_extension sourcefile) in
let ml_ast =
List.fold_left
Expand Down
147 changes: 126 additions & 21 deletions compiler/typing/build_file_store.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@
So an entry changes only when it is written through the store, and the
store is emptied before each build. *)

(* What the disk holds for a path, as far as the build has read it. *)
type on_disk = Read of string | Missing | Unknown
(* What the disk holds for a path, as far as the build has read it. A path
kept in memory only (see {!set_memory_only}) holds what [Kept] says. *)
type on_disk =
| Read of string
| Missing
| Unknown
| Kept of {contents: string; modified: float}

type entry =
| Contents of string
| Exists (** On disk, contents not read yet. *)
| Absent
| Held of {contents: string; on_disk: on_disk}
(** Written by a request, not on disk until {!publish_held}. *)
| Memory of {contents: string; modified: float}
(** A file kept in memory only, with the time it was last changed. It
outlives builds. *)

(* [enabled] and [generation] are read without the lock: only [with_build]
changes them, before a build's requests start and after they finish. *)
Expand Down Expand Up @@ -61,11 +69,43 @@ let set_disk_reader reader = Atomic.set disk_reader reader
let held_writes = Atomic.make (fun (_ : string) -> false)
let set_held_writes held = Atomic.set held_writes held

(* Which paths live in memory only. [None] keeps every path on disk. *)
let memory_only : (string -> bool) option Atomic.t = Atomic.make None
let set_memory_only predicate = Atomic.set memory_only (Some predicate)

let is_memory_only key =
match Atomic.get memory_only with
| None -> false
| Some predicate -> predicate key

(* Modification times of files kept in memory. They increase strictly, so two
changes in a row are told apart like on a file system with fine times. *)
let last_stamp = ref 0.

let stamp () =
let now = Unix.gettimeofday () in
let next = if now > !last_stamp then now else !last_stamp +. 1e-6 in
last_stamp := next;
next

let enabled () = state.enabled
let generation () = state.generation

let empty () =
state.entries <- Hashtbl.create 4096;
(match Atomic.get memory_only with
| None -> state.entries <- Hashtbl.create 4096
| Some _ ->
(* Files kept in memory stay, and held contents end as if forgotten.
Everything read from disk is read again, since it may have changed
since. *)
Hashtbl.filter_map_inplace
(fun _ entry ->
match entry with
| Memory _ -> Some entry
| Held {on_disk = Kept {contents; modified}; _} ->
Some (Memory {contents; modified})
| Contents _ | Exists | Absent | Held _ -> None)
state.entries);
state.generation <- state.generation + 1

let with_build f =
Expand Down Expand Up @@ -144,8 +184,10 @@ let read ?(keep = true) path =
else
let key = key path in
match find key with
| Some (Contents contents | Held {contents; _}) -> Some contents
| Some (Contents contents | Held {contents; _} | Memory {contents; _}) ->
Some contents
| Some Absent -> None
| None when is_memory_only key -> None
| Some Exists | None ->
let contents = (Atomic.get disk_reader) path in
record key
Expand All @@ -159,8 +201,9 @@ let exists path =
else
let key = key path in
match find key with
| Some (Contents _ | Exists | Held _) -> true
| Some (Contents _ | Exists | Held _ | Memory _) -> true
| Some Absent -> false
| None when is_memory_only key -> false
| None ->
let exists = Sys.file_exists path in
record key (if exists then Exists else Absent);
Expand All @@ -185,9 +228,16 @@ let write ?(keep = true) path contents =
| Some (Contents contents) -> Read contents
| Some Absent -> Missing
| Some (Held {on_disk; _}) -> on_disk
| Some (Memory {contents; modified}) -> Kept {contents; modified}
| None when is_memory_only key -> Missing
| Some Exists | None -> Unknown
in
Hashtbl.replace state.entries key (Held {contents; on_disk}))
else if state.enabled && is_memory_only (key path) then
let key = key path in
locked (fun () ->
Hashtbl.replace state.entries key
(Memory {contents; modified = stamp ()}))
else
match write_to_disk path contents with
| () ->
Expand All @@ -205,36 +255,91 @@ let evict path =
locked (fun () ->
match Hashtbl.find_opt state.entries key with
| Some (Contents _) -> Hashtbl.replace state.entries key Exists
(* Held contents exist nowhere else. *)
| Some (Exists | Absent | Held _) | None -> ())
(* Held contents and files kept in memory exist nowhere else. *)
| Some (Exists | Absent | Held _ | Memory _) | None -> ())

let forget path =
if state.enabled then
let key = key path in
locked (fun () ->
match Hashtbl.find_opt state.entries key with
(* Like dropping held contents of a file on disk: the file kept
before the write is what reads see again. *)
| Some (Held {on_disk = Kept {contents; modified}; _}) ->
Hashtbl.replace state.entries key (Memory {contents; modified})
(* The only copy, as the file on disk is for other paths. *)
| Some (Memory _) -> ()
| Some (Contents _ | Exists | Absent | Held _) | None ->
Hashtbl.remove state.entries key)

let remove path =
if state.enabled then
locked (fun () -> Hashtbl.remove state.entries (key path))

let cached path =
if not state.enabled then None
else
match find (key path) with
| Some (Contents contents | Held {contents; _}) -> Some contents
| Some (Contents contents | Held {contents; _} | Memory {contents; _}) ->
Some contents
| Some (Exists | Absent) | None -> None

let modified path =
if not state.enabled then None
else
match find (key path) with
| Some (Memory {modified; _}) -> Some modified
| Some (Contents _ | Exists | Absent | Held _) | None -> None

let set_modified path modified =
if state.enabled then
let key = key path in
locked (fun () ->
match Hashtbl.find_opt state.entries key with
| Some (Memory {contents; _}) ->
Hashtbl.replace state.entries key (Memory {contents; modified})
| Some (Contents _ | Exists | Absent | Held _) | None -> ())

let touch path =
if state.enabled then
let key = key path in
locked (fun () ->
let contents =
match Hashtbl.find_opt state.entries key with
| Some (Memory {contents; _}) -> contents
| Some (Contents _ | Exists | Absent | Held _) | None -> ""
in
Hashtbl.replace state.entries key
(Memory {contents; modified = stamp ()}))

let memory_files () =
locked (fun () ->
Hashtbl.fold
(fun key entry files ->
match entry with
| Memory {modified; _} -> (key, modified) :: files
| Contents _ | Exists | Absent | Held _ -> files)
state.entries [])

let publish_held ~if_changed ~write path =
let key = key path in
match find key with
| Some (Held {contents; on_disk}) ->
let changed =
(not if_changed)
||
let on_disk =
match on_disk with
| Read on_disk -> Some on_disk
| Missing -> None
| Unknown -> (Atomic.get disk_reader) path
in
on_disk <> Some contents
let previous =
match on_disk with
| Read on_disk -> Some on_disk
| Kept {contents; _} -> Some contents
| Missing -> None
| Unknown -> (Atomic.get disk_reader) path
in
if changed then write path contents;
record key (Contents contents);
let changed = (not if_changed) || previous <> Some contents in
(match on_disk with
| Kept {modified; _} when not changed ->
record key (Memory {contents; modified})
| _ when is_memory_only key ->
record key (Memory {contents; modified = locked stamp})
| Read _ | Missing | Unknown | Kept _ ->
if changed then write path contents;
record key (Contents contents));
changed
| Some (Contents _ | Exists | Absent) | None -> raise Not_found
| Some (Contents _ | Exists | Absent | Memory _) | None -> raise Not_found
35 changes: 33 additions & 2 deletions compiler/typing/build_file_store.mli
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,48 @@ val set_held_writes : (string -> bool) -> unit
this way, so that one is written to disk only after its compile succeeded,
and only when it changed. Install it before requests run. *)

val set_memory_only : (string -> bool) -> unit
(** While the store is enabled, the paths the predicate selects (by their
{!key}) live in memory only: writes and {!publish_held} keep them instead
of writing them to disk, a read of one that was never written finds
nothing instead of reading the disk, and {!with_build} keeps them for the
next build instead of emptying them. Each has a modification time, given
when it is written or published changed, which {!modified} reads. A build
system that writes no artifacts installs it before its first build. *)

val modified : string -> float option
(** [modified path] is the modification time of [path] when it is kept in
memory only. *)

val set_modified : string -> float -> unit
(** [set_modified path time] dates [path], kept in memory only, back or
forward, like [Unix.utimes]. *)

val touch : string -> unit
(** [touch path] gives [path] a new modification time, keeping it in memory
only, empty unless it was kept already. *)

val memory_files : unit -> (string * float) list
(** The keys and modification times of the paths kept in memory only. *)

val publish_held :
if_changed:bool -> write:(string -> string -> unit) -> string -> bool
(** [publish_held ~if_changed ~write path] calls [write path contents] with the
held contents of [path], keeps them for later reads, and returns whether it
wrote them. With [if_changed], it writes them only when they differ from
the file on disk, so that an unchanged file keeps its modification time.
Raises [Not_found] when nothing is held for [path]. *)
Raises [Not_found] when nothing is held for [path]. A path kept in memory only is
kept instead of written, and keeps its modification time when unchanged. *)

val forget : string -> unit
(** [forget path] drops everything known about [path]. Call it after changing
[path] other than through {!write}. *)
[path] other than through {!write}. For a path kept in memory only, it
drops the held contents, and the file kept before them stays, like a file
on disk. *)

val remove : string -> unit
(** [remove path] drops everything known about [path], a file kept in memory
only included. Call it when removing [path] from disk. *)

val cached : string -> string option
(** [cached path] is the kept contents of [path], without reading the disk. *)
10 changes: 0 additions & 10 deletions compiler/typing/file_io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,5 @@ let unmarshal contents offset =
then raise End_of_file
else Marshal.from_string contents offset

let rev_lines_of_chann chan =
let rec loop acc chan =
match input_line chan with
| line -> loop (line :: acc) chan
| exception End_of_file ->
close_in chan;
acc
in
loop [] chan

let write_file f content =
Build_file_store.write (Compiler_request_state.resolve_path f) content
2 changes: 0 additions & 2 deletions compiler/typing/file_io.mli
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,4 @@ val unmarshal : string -> int -> 'a
[contents]. Raises [End_of_file] when [contents] ends before the value
does, as when a killed build cut a file short. *)

val rev_lines_of_chann : in_channel -> string list

val write_file : string -> string -> unit
2 changes: 1 addition & 1 deletion compiler/typing/misc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ let find_in_path_uncap path name =

let remove_file filename =
let filename = Compiler_request_state.resolve_path filename in
Build_file_store.forget filename;
Build_file_store.remove filename;
try if Sys.file_exists filename then Sys.remove filename
with Sys_error _msg -> ()

Expand Down
20 changes: 20 additions & 0 deletions rewatch-ocaml/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ the main domain. With `REWATCH_REPORT_WORKER_ALLOCATIONS` set, the build also
prints `worker_allocated_words: <n>`, the words its workers allocated, which
the compiler benchmarks (`scripts/compilerBenchmarks.js`) add to it.

With `RESCRIPT_TRACE=<file>`, the build writes a trace of what each domain did
to the file when it exits, in the Chrome trace event format. Open it in
Perfetto (ui.perfetto.dev) to see it as a timeline:

- The main track has a span per dependency graph (`parse`, `compile`,
`namespaces`, `format`) and per scheduler step, and counters for the tasks
that are ready and running. Gaps on the worker tracks while tasks are ready
are time lost to scheduling.
- Each worker track has a span per compiler request, named after its input file
and categorized by phase (`parse`, `interface`, `implementation`,
`namespace`, `format`). Its arguments hold the words the request allocated and the
garbage collections that ran meanwhile. Implementations have child spans for
type checking and code generation. A `publish` span follows each request,
for publishing the files it wrote.

A relative path is relative to the current directory. A command that records
nothing, such as `rescript clean`, leaves the file alone. A watch session keeps
its events in memory and writes them when it stops; a killed process writes
nothing. The format is for people and may change.

The driver gives each request fresh compiler state (flags, warnings, caches,
predefined types, identifier counters) and restores it on every exit, so
requests on different domains don't interfere and repeated requests produce
Expand Down
4 changes: 4 additions & 0 deletions rewatch-ocaml/ast_header.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ let read_file path =
let read path =
match Build_file_store.cached path with
| Some contents -> of_contents contents |> Option.value ~default:damaged
(* An AST kept in memory only is cached; one on disk is from another
build. *)
| None when No_artifacts.enabled ->
raise (Unix.Unix_error (Unix.ENOENT, "open", path))
| None -> (
try read_file path
with Unix.Unix_error (error, operation, "") ->
Expand Down
Loading
Loading