Skip to content

Commit

Permalink
refactor: Improve [Findlib] handling (#8401)
Browse files Browse the repository at this point in the history
Switch to memoizing the current [Findlib] instance per context.

This allows us to do even less work for [Sites.create] which is a
sparsely used feature.

Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
  • Loading branch information
rgrinberg authored Aug 15, 2023
1 parent bf1d9d3 commit e11fd63
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 50 deletions.
4 changes: 1 addition & 3 deletions bin/installed_libraries.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ let term =
let open Memo.O in
let* ctxs = Context.DB.all () in
let ctx = List.hd ctxs in
let* findlib =
Findlib.create ~paths:ctx.findlib_paths ~lib_config:ctx.ocaml.lib_config
in
let* findlib = Findlib.create ctx.name in
let* all_packages = Findlib.all_packages findlib in
if na
then (
Expand Down
15 changes: 15 additions & 0 deletions src/dune_rules/context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,21 @@ module DB = struct
Memo.exec memo
;;

let create_db ~name f =
let map =
Memo.lazy_ ~name (fun () ->
let+ map = all () in
Context_name.Map.of_list_map_exn map ~f:(fun context ->
context.name, Memo.lazy_ (fun () -> f context)))
in
Staged.stage (fun context ->
let* map = Memo.Lazy.force map in
match Context_name.Map.find map context with
| Some v -> Memo.Lazy.force v
| None ->
Code_error.raise "invalid context" [ "context", Context_name.to_dyn context ])
;;

let by_dir dir =
let context =
match Dune_engine.Dpath.analyse_dir (Path.build dir) with
Expand Down
5 changes: 5 additions & 0 deletions src/dune_rules/context.mli
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,9 @@ module DB : sig
val get : Context_name.t -> t Memo.t
val all : unit -> t list Memo.t
val by_dir : Path.Build.t -> t Memo.t

val create_db
: name:string
-> (t -> 'a Memo.t)
-> (Context_name.t -> 'a Memo.t) Staged.t
end
2 changes: 1 addition & 1 deletion src/dune_rules/dep_conf_eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ let rec dep expander = function
let context = Expander.context expander in
Action_builder.of_memo
(let open Memo.O in
let* sites = Sites.create context in
let* sites = Sites.create context.name in
Sites.find_package sites pkg)
>>= function
| Some (Local pkg) ->
Expand Down
37 changes: 13 additions & 24 deletions src/dune_rules/findlib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,6 @@ let all_packages t =
Lib_name.compare (Dune_package.Entry.name a) (Dune_package.Entry.name b))
;;

let create ~paths ~(lib_config : Lib_config.t) =
let stdlib_dir = lib_config.stdlib_dir in
let version = lib_config.ocaml_version in
let+ builtins = Meta.builtins ~stdlib_dir ~version in
{ DB.stdlib_dir; paths; builtins; lib_config }
;;

let lib_config (t : t) = t.lib_config

let all_broken_packages t =
Expand All @@ -474,23 +467,19 @@ let all_broken_packages t =
|> List.sort ~compare:(fun (a, _) (b, _) -> Package.Name.compare a b)
;;

let create =
let module Input = struct
type t = Path.t list * Lib_config.t
let create ~paths ~(lib_config : Lib_config.t) =
let stdlib_dir = lib_config.stdlib_dir in
let version = lib_config.ocaml_version in
let+ builtins = Meta.builtins ~stdlib_dir ~version in
{ DB.stdlib_dir; paths; builtins; lib_config }
;;

let equal (paths, libs) (paths', libs') =
List.equal Path.equal paths paths' && Lib_config.equal libs libs'
;;
module For_tests = struct
let create = create
end

let hash = Tuple.T2.hash (List.hash Path.hash) Lib_config.hash
let to_dyn = Dyn.pair (Dyn.list Path.to_dyn) Lib_config.to_dyn
end
in
let memo =
Memo.create
"lib-installed"
~input:(module Input)
(fun (paths, lib_config) -> create ~paths ~lib_config)
in
fun ~paths ~lib_config -> Memo.exec memo (paths, lib_config)
let create =
Context.DB.create_db ~name:"findlib" (fun context ->
create ~paths:context.findlib_paths ~lib_config:context.ocaml.lib_config)
|> Staged.unstage
;;
7 changes: 6 additions & 1 deletion src/dune_rules/findlib.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ open Import
(** Findlib database *)
type t

val create : paths:Path.t list -> lib_config:Lib_config.t -> t Memo.t
val lib_config : t -> Lib_config.t
val findlib_predicates_set_by_dune : Variant.Set.t

Expand Down Expand Up @@ -33,3 +32,9 @@ val all_packages : t -> Dune_package.Entry.t list Memo.t

(** List all the packages that have broken [dune-package] files *)
val all_broken_packages : t -> (Package.Name.t * User_message.t) list Memo.t

val create : Context_name.t -> t Memo.t

module For_tests : sig
val create : paths:Path.t list -> lib_config:Lib_config.t -> t Memo.t
end
2 changes: 1 addition & 1 deletion src/dune_rules/install_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ end = struct
Install.Entry.Sourced.create entry :: acc)
else acc))
and+ l =
let* sites = Sites.create ctx in
let* sites = Sites.create ctx.name in
Dune_file.fold_stanzas stanzas ~init:[] ~f:(fun dune_file stanza acc ->
let dir = Path.Build.append_source ctx.build_dir dune_file.dir in
let named_entries =
Expand Down
4 changes: 1 addition & 3 deletions src/dune_rules/lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1799,9 +1799,7 @@ module DB = struct

let installed (context : Context.t) =
let open Memo.O in
let+ findlib =
Findlib.create ~paths:context.findlib_paths ~lib_config:context.ocaml.lib_config
in
let+ findlib = Findlib.create context.name in
create_from_findlib findlib ~instrument_with:context.instrument_with
;;

Expand Down
21 changes: 7 additions & 14 deletions src/dune_rules/sites.ml
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
open Import
open Memo.O

type t =
{ packages : Package.t Package.Name.Map.t
; findlib : Findlib.t
}
type t = Context_name.t

type any_package =
| Local of Package.t
| Installed of Dune_package.t

let find_package t pkg =
match Package.Name.Map.find t.packages pkg with
let find_package ctx pkg =
let* packages = Only_packages.get () in
match Package.Name.Map.find packages pkg with
| Some p -> Memo.return (Some (Local p))
| None ->
let open Memo.O in
Findlib.find_root_package t.findlib pkg
let* findlib = Findlib.create ctx in
Findlib.find_root_package findlib pkg
>>| (function
| Ok p -> Some (Installed p)
| Error Not_found -> None
| Error (Invalid_dune_package user_message) ->
User_error.raise [ User_message.pp user_message ])
;;

let create (context : Context.t) =
let* packages = Only_packages.get () in
let+ findlib =
Findlib.create ~paths:context.findlib_paths ~lib_config:context.ocaml.lib_config
in
{ packages; findlib }
;;
let create ctx = Memo.return ctx

let section_of_site t ~loc ~pkg ~site =
let+ sites =
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/sites.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type any_package =
| Local of Package.t
| Installed of Dune_package.t

val create : Context.t -> t Memo.t
val create : Context_name.t -> t Memo.t
val find_package : t -> Package.Name.t -> any_package option Memo.t

val section_of_site
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/super_context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ let create ~(context : Context.t) ~host ~packages ~stanzas =
| Some s -> Some (Section.Set.add s section))
in
let+ package_sections =
let* sites = Sites.create context in
let* sites = Sites.create context.name in
Dune_file.Memo_fold.fold_stanzas
stanzas
~init:Package.Name.Map.empty
Expand Down
2 changes: 1 addition & 1 deletion test/expect-tests/findlib_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let findlib =
}
in
Memo.lazy_ (fun () ->
Findlib.create ~paths:[ Path.outside_build_dir db_path ] ~lib_config)
Findlib.For_tests.create ~paths:[ Path.outside_build_dir db_path ] ~lib_config)
;;

let resolve_pkg s =
Expand Down

0 comments on commit e11fd63

Please sign in to comment.