Skip to content

Commit 32ecd95

Browse files
committed
[src][state][loc_dep][2/n] add cache for external uids
1 parent 8951dc5 commit 32ecd95

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

src/state/location_dependencies.ml

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,31 @@ let fill_from_cmt_tbl uid_to_decl res_uid_to_loc =
4848
UidTbl.iter add_uid_loc uid_to_decl;
4949
res_uid_to_loc
5050

51-
let find_opt_external_uid_loc ~cm_paths = function
52-
| Shape.Uid.(Compilation_unit _ | Internal | Predef _) -> None
53-
| Item {comp_unit; _} as uid ->
54-
let ( let* ) x f = Option.bind x f in
55-
let* cm_path =
56-
Utils.StringSet.elements cm_paths
57-
|> List.rev
58-
|> List.find_opt (fun path -> Utils.Filepath.unit path = comp_unit)
59-
in
60-
let* cmt_infos = Cmt_format.read cm_path |> snd in
61-
let* item_decl = UidTbl.find_opt cmt_infos.cmt_uid_to_decl uid in
62-
loc_opt_of_item_decl item_decl
51+
let find_opt_external_uid_loc, clear_external_cache =
52+
let cache = Hashtbl.create 16 in
53+
let clear_external_cache () = Hashtbl.reset cache in
54+
let find_opt_external_uid_loc ~cm_paths = function
55+
| Shape.Uid.(Compilation_unit _ | Internal | Predef _) -> None
56+
| Item {comp_unit; _} as uid ->
57+
let ( let* ) x f = Option.bind x f in
58+
let* cmt_uid_to_decl =
59+
match Hashtbl.find_opt cache comp_unit with
60+
| Some _ as cmt_uid_to_decl -> cmt_uid_to_decl
61+
| None ->
62+
let* cm_path =
63+
Utils.StringSet.elements cm_paths
64+
|> List.rev
65+
|> List.find_opt (fun path -> Utils.Filepath.unit path = comp_unit)
66+
in
67+
let* cmt_infos = Cmt_format.read cm_path |> snd in
68+
let cmt_uid_to_decl = cmt_infos.cmt_uid_to_decl in
69+
Hashtbl.add cache comp_unit cmt_uid_to_decl;
70+
Some cmt_uid_to_decl
71+
in
72+
let* item_decl = UidTbl.find_opt cmt_uid_to_decl uid in
73+
loc_opt_of_item_decl item_decl
74+
in
75+
find_opt_external_uid_loc, clear_external_cache
6376

6477
let cmt_decl_dep_to_loc_dep ~cm_paths cmt_decl_dep uid_to_loc =
6578
let convert_pair (_dep_kind, uid_def, uid_decl) =
@@ -73,7 +86,9 @@ let cmt_decl_dep_to_loc_dep ~cm_paths cmt_decl_dep uid_to_loc =
7386
let* decl_loc = loc_opt_of_uid uid_decl in
7487
Some (def_loc, decl_loc)
7588
in
76-
List.filter_map convert_pair cmt_decl_dep
89+
let res = List.filter_map convert_pair cmt_decl_dep in
90+
clear_external_cache ();
91+
res
7792

7893
let init ~cm_paths cmt_infos cmti_uid_to_decl =
7994
match cmt_infos.Cmt_format.cmt_annots with

0 commit comments

Comments
 (0)