Skip to content

Commit fc3cdae

Browse files
committed
refactor cmt cache
1 parent 18bbe59 commit fc3cdae

File tree

2 files changed

+13
-34
lines changed

2 files changed

+13
-34
lines changed

analysis/src/ProcessCmt.ml

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,17 @@ let forCmt ~moduleName ~uri ({cmt_modname; cmt_annots} : Cmt_format.cmt_infos) =
512512
{uri; moduleName = cmt_modname; stamps = env.stamps; structure}
513513
| _ -> File.create moduleName uri
514514

515-
let fileForCmt ~moduleName ~uri cmt =
516-
match Shared.tryReadCmt cmt with
517-
| None -> None
518-
| Some infos -> Some (forCmt ~moduleName ~uri infos)
515+
let fileForCmt ~moduleName ~cmt ~uri =
516+
if Hashtbl.mem state.cmtCache cmt then
517+
let file = Hashtbl.find state.cmtCache cmt in
518+
Some file
519+
else
520+
match Shared.tryReadCmt cmt with
521+
| None -> None
522+
| Some infos ->
523+
let file = forCmt ~moduleName ~uri infos in
524+
Hashtbl.replace state.cmtCache cmt file;
525+
Some file
519526

520527
let addLocItem extra loc locType =
521528
if not loc.Warnings.loc_ghost then
@@ -673,42 +680,14 @@ let extraForCmt ~(iterator : Tast_iterator.iterator)
673680
extraForParts parts
674681
| _ -> extraForStructureItems ~iterator []
675682

676-
let newFileForCmt ~moduleName cmtCache changed ~cmt ~uri =
677-
match fileForCmt ~moduleName ~uri cmt with
678-
| None -> None
679-
| Some file ->
680-
Hashtbl.replace cmtCache cmt (changed, file);
681-
Some file
682-
683-
let fileForCmt ~moduleName ~cmt ~uri state =
684-
if Hashtbl.mem state.cmtCache cmt then
685-
let mtime, docs = Hashtbl.find state.cmtCache cmt in
686-
(* TODO: I should really throttle this mtime checking to like every 50 ms or so *)
687-
match Files.getMtime cmt with
688-
| None ->
689-
Log.log
690-
("\226\154\160\239\184\143 cannot get docs for nonexistant cmt " ^ cmt);
691-
None
692-
| Some changed ->
693-
if changed > mtime then
694-
newFileForCmt ~moduleName state.cmtCache changed ~cmt ~uri
695-
else Some docs
696-
else
697-
match Files.getMtime cmt with
698-
| None ->
699-
Log.log
700-
("\226\154\160\239\184\143 cannot get docs for nonexistant cmt " ^ cmt);
701-
None
702-
| Some changed -> newFileForCmt ~moduleName state.cmtCache changed ~cmt ~uri
703-
704683
let fileForModule modname ~package =
705684
if Hashtbl.mem package.pathsForModule modname then (
706685
let paths = Hashtbl.find package.pathsForModule modname in
707686
(* TODO: do better *)
708687
let uri = getUri paths in
709688
let cmt = getCmtPath ~uri paths in
710689
Log.log ("fileForModule " ^ showPaths paths);
711-
match fileForCmt ~moduleName:modname ~cmt ~uri state with
690+
match fileForCmt ~moduleName:modname ~cmt ~uri with
712691
| None -> None
713692
| Some docs -> Some docs)
714693
else (

analysis/src/SharedTypes.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ let initExtra () =
365365
type state = {
366366
packagesByRoot : (string, package) Hashtbl.t;
367367
rootForUri : (Uri2.t, string) Hashtbl.t;
368-
cmtCache : (filePath, float * File.t) Hashtbl.t;
368+
cmtCache : (filePath, File.t) Hashtbl.t;
369369
}
370370

371371
(* There's only one state, so it can as well be global *)

0 commit comments

Comments
 (0)