From c202a150ba25e45788b89d087d65910841a29b61 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Thu, 11 Apr 2019 11:55:28 +0700 Subject: [PATCH] Remove caching of scopes It does not seem to buy us anything performance wise Signed-off-by: Rudi Grinberg --- src/scope.ml | 32 +++++++++++++++----------------- src/stdune/map.ml | 5 +++++ src/stdune/map_intf.ml | 4 ++++ 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/scope.ml b/src/scope.ml index ea902d98ba5..95dfd8efad8 100644 --- a/src/scope.ml +++ b/src/scope.ml @@ -16,31 +16,29 @@ module DB = struct type scope = t type t = - { by_dir : (Path.t, scope) Hashtbl.t - ; by_name : scope Dune_project.Name.Map.t + { by_name : scope Dune_project.Name.Map.t + ; by_dir : scope Path.Map.t ; context : string } let find_by_dir t dir = let rec loop d = - match Hashtbl.find t.by_dir d with - | Some scope -> scope + if Path.is_root d || not (Path.is_managed d) then + Exn.code_error "Scope.DB.find_by_dir got an invalid path" + [ "dir" , Path.to_sexp dir + ; "context", Sexp.Encoder.string t.context + ]; + match Path.Map.find t.by_dir d with + | Some s -> s | None -> - if Path.is_root d || not (Path.is_managed d) then - Exn.code_error "Scope.DB.find_by_dir got an invalid path" - [ "dir" , Path.to_sexp dir - ; "context", Sexp.Encoder.string t.context - ]; - match Path.parent d with + begin match Path.parent d with | None -> Exn.code_error "find_by_dir: invalid directory" [ "d", Path.to_sexp d ; "dir", Path.to_sexp dir ] - | Some d -> - let scope = loop d in - Hashtbl.add t.by_dir d scope; - scope + | Some d -> loop d + end in loop dir @@ -142,9 +140,9 @@ module DB = struct let by_name = sccopes_by_name ~context ~projects ~lib_config ~public_libs internal_libs in - let by_dir = Hashtbl.create 1024 in - Dune_project.Name.Map.iter by_name ~f:(fun scope -> - Hashtbl.add by_dir scope.root scope); + let by_dir = + Dune_project.Name.Map.values by_name + |> Path.Map.of_list_map_exn ~f:(fun scope -> (scope.root, scope)) in Fdecl.set t { by_name ; by_dir ; context}; (Fdecl.get t, public_libs) end diff --git a/src/stdune/map.ml b/src/stdune/map.ml index 5413e535939..dff8ce176ff 100644 --- a/src/stdune/map.ml +++ b/src/stdune/map.ml @@ -108,6 +108,11 @@ module Make(Key : Comparable.S) : S with type key = Key.t = struct | x :: y :: _ -> Error (k, x, y) | _ -> assert false + let of_list_map_exn t ~f = + match of_list_map t ~f with + | Ok x -> x + | Error _ -> Exn.code_error "Map.of_list_map_exn" [] + let of_list_exn l = match of_list l with | Ok x -> x diff --git a/src/stdune/map_intf.ml b/src/stdune/map_intf.ml index ce289bac5e4..112a36d985b 100644 --- a/src/stdune/map_intf.ml +++ b/src/stdune/map_intf.ml @@ -58,6 +58,10 @@ module type S = sig : 'a list -> f:('a -> key * 'b) -> ('b t, key * 'a * 'a) Result.t + val of_list_map_exn + : 'a list + -> f:('a -> key * 'b) + -> 'b t val of_list_exn : (key * 'a) list -> 'a t val of_list_multi : (key * 'a) list -> 'a list t