Skip to content

Commit 43a9714

Browse files
authored
refactor(gen_rules): use a map for cctxs (#13171)
the only usage of `cctxs` is for a lookup in the menhir rules Signed-off-by: Antonio Nuno Monteiro <anmonteiro@gmail.com>
1 parent a7bf3d1 commit 43a9714

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/dune_rules/gen_rules.ml

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ module For_stanza : sig
3939

4040
val of_stanzas
4141
: Stanza.t list
42-
-> cctxs:(Loc.t * Compilation_context.t) list
42+
-> cctxs:Compilation_context.t Loc.Map.t
4343
-> sctx:Super_context.t
4444
-> src_dir:Path.Source.t
4545
-> ctx_dir:Path.Build.t
4646
-> scope:Scope.t
4747
-> dir_contents:Dir_contents.t
4848
-> expander:Expander.t
4949
-> ( Merlin.t list
50-
, (Loc.t * Compilation_context.t) list
50+
, Compilation_context.t Loc.Map.t
5151
, Path.Build.t list
5252
, Path.Source.t list )
5353
t
@@ -61,7 +61,13 @@ end = struct
6161
}
6262

6363
let empty_none = { merlin = None; cctx = None; js = None; source_dirs = None }
64-
let empty_list = { merlin = []; cctx = []; js = []; source_dirs = [] }
64+
let empty_list = { merlin = []; cctx = Loc.Map.empty; js = []; source_dirs = [] }
65+
66+
let add_map_maybe hd_o tl =
67+
match hd_o with
68+
| Some (loc, hd) -> Loc.Map.add_exn tl loc hd
69+
| None -> tl
70+
;;
6571

6672
let cons_maybe hd_o tl =
6773
match hd_o with
@@ -71,7 +77,7 @@ end = struct
7177

7278
let cons acc x =
7379
{ merlin = cons_maybe x.merlin acc.merlin
74-
; cctx = cons_maybe x.cctx acc.cctx
80+
; cctx = add_map_maybe x.cctx acc.cctx
7581
; source_dirs = cons_maybe x.source_dirs acc.source_dirs
7682
; js =
7783
(match x.js with
@@ -80,13 +86,7 @@ end = struct
8086
}
8187
;;
8288

83-
let rev t =
84-
{ t with
85-
merlin = List.rev t.merlin
86-
; cctx = List.rev t.cctx
87-
; source_dirs = List.rev t.source_dirs
88-
}
89-
;;
89+
let rev t = { t with merlin = List.rev t.merlin; source_dirs = List.rev t.source_dirs }
9090

9191
let if_available f = function
9292
| false -> Memo.return empty_none
@@ -187,7 +187,7 @@ end = struct
187187
Memo.parallel_map
188188
stanzas
189189
~f:(of_stanza ~sctx ~src_dir ~ctx_dir ~scope ~dir_contents ~expander)
190-
>>| List.fold_left ~init:{ empty_list with cctx = cctxs } ~f:(fun acc x -> cons acc x)
190+
>>| List.fold_left ~init:{ empty_list with cctx = cctxs } ~f:cons
191191
>>| rev
192192
;;
193193
end
@@ -257,12 +257,8 @@ let gen_rules_for_stanzas sctx dir_contents cctxs expander ~dune_file ~dir:ctx_d
257257
Menhir_rules.module_names m
258258
|> Memo.List.find_map ~f:(fun name ->
259259
let path = Nonempty_list.(base_path @ [ name ]) in
260-
Ml_sources.find_origin ml_sources ~libs:(Scope.libs scope) path
261-
>>| function
262-
| None -> None
263-
| Some origin ->
264-
List.find_map cctxs ~f:(fun (loc, cctx) ->
265-
Option.some_if (Loc.equal loc (Ml_sources.Origin.loc origin)) cctx))
260+
Ml_sources.find_origin ml_sources ~libs:(Scope.libs scope) path)
261+
>>| Option.bind ~f:(fun loc -> Loc.Map.find cctxs (Ml_sources.Origin.loc loc))
266262
>>= (function
267263
| Some cctx ->
268264
Menhir_rules.gen_rules cctx m ~module_path:base_path ~dir:ctx_dir
@@ -330,7 +326,7 @@ let gen_rules_source_only sctx ~dir source_dir =
330326
;;
331327

332328
let gen_rules_group_part_or_root sctx dir_contents cctxs ~source_dir ~dir
333-
: (Loc.t * Compilation_context.t) list Memo.t
329+
: Compilation_context.t Loc.Map.t Memo.t
334330
=
335331
let+ () = gen_format_and_cram_rules sctx ~dir source_dir
336332
and+ contexts =
@@ -343,7 +339,7 @@ let gen_rules_group_part_or_root sctx dir_contents cctxs ~source_dir ~dir
343339
| None ->
344340
let project = Source_tree.Dir.project source_dir in
345341
let+ () = Alias_builder.define_all_alias ~js_targets:[] ~project dir in
346-
[]
342+
Loc.Map.empty
347343
in
348344
contexts
349345
;;
@@ -514,11 +510,13 @@ let gen_rules_standalone_or_root sctx ~dir ~source_dir =
514510
let* rules' =
515511
Rules.collect_unit (fun () ->
516512
let* dir_contents = Dir_contents.Standalone_or_root.root standalone_or_root in
517-
let* cctxs = gen_rules_group_part_or_root sctx dir_contents [] ~source_dir ~dir in
513+
let* cctxs =
514+
gen_rules_group_part_or_root sctx dir_contents Loc.Map.empty ~source_dir ~dir
515+
in
518516
Dir_contents.Standalone_or_root.subdirs standalone_or_root
519517
>>= Memo.parallel_iter ~f:(fun dc ->
520518
let source_dir = Option.value_exn (Dir_contents.source_dir dc) in
521-
let+ (_ : (Loc.t * Compilation_context.t) list) =
519+
let+ (_ : Compilation_context.t Loc.Map.t) =
522520
gen_rules_group_part_or_root
523521
sctx
524522
dir_contents

0 commit comments

Comments
 (0)