Skip to content

Commit

Permalink
Merge pull request #2047 from rgrinberg/remove-scope-cache
Browse files Browse the repository at this point in the history
Remove caching of scopes
  • Loading branch information
rgrinberg authored Apr 11, 2019
2 parents 44b1a74 + c202a15 commit d98e5a4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/scope.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/stdune/map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/stdune/map_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d98e5a4

Please sign in to comment.