Skip to content

Commit bc71f76

Browse files
committed
only look up project files etc when needed
1 parent 51aec1c commit bc71f76

8 files changed

+148
-119
lines changed

analysis/src/Cache.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ let targetFileFromLibBs libBs = Filename.concat libBs ".project-files-cache"
2828
let cacheProject (package : package) =
2929
let cached =
3030
{
31-
projectFiles = package.projectFiles;
32-
dependenciesFiles = package.dependenciesFiles;
33-
pathsForModule = package.pathsForModule;
31+
projectFiles = Lazy.force package.projectFiles;
32+
dependenciesFiles = Lazy.force package.dependenciesFiles;
33+
pathsForModule = Lazy.force package.pathsForModule;
3434
}
3535
in
3636
match BuildSystem.getLibBs package.rootPath with

analysis/src/Cmt.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ let fullFromUri ~uri =
3333
if Debug.verbose () then Printf.printf "[cmt] Found incremental cmt\n";
3434
Some cmtInfo
3535
| None -> (
36-
match Hashtbl.find_opt package.pathsForModule moduleName with
36+
match Hashtbl.find_opt (Lazy.force package.pathsForModule) moduleName with
3737
| Some paths ->
3838
let cmt = getCmtPath ~uri paths in
3939
fullForCmt ~moduleName ~package ~uri cmt
@@ -42,8 +42,8 @@ let fullFromUri ~uri =
4242
None))
4343

4444
let fullsFromModule ~package ~moduleName =
45-
if Hashtbl.mem package.pathsForModule moduleName then
46-
let paths = Hashtbl.find package.pathsForModule moduleName in
45+
if Hashtbl.mem (Lazy.force package.pathsForModule) moduleName then
46+
let paths = Hashtbl.find (Lazy.force package.pathsForModule) moduleName in
4747
let uris = getUris paths in
4848
uris |> List.filter_map (fun uri -> fullFromUri ~uri)
4949
else []

analysis/src/CompletionBackEnd.ml

+4-3
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
10571057
resultModulePath;
10581058
regexpModulePath;
10591059
} =
1060-
package.builtInCompletionModules
1060+
Lazy.force package.builtInCompletionModules
10611061
in
10621062
Some
10631063
(match builtin with
@@ -1316,7 +1316,7 @@ let getOpens ~debug ~rawOpens ~package ~env =
13161316
^ string_of_int (List.length rawOpens)
13171317
^ " "
13181318
^ String.concat " ... " (rawOpens |> List.map pathToString));
1319-
let packageOpens = package.opens in
1319+
let packageOpens = Lazy.force package.opens in
13201320
if debug && packageOpens <> [] then
13211321
Printf.printf "%s\n"
13221322
("Package opens "
@@ -1812,7 +1812,8 @@ let rec completeTypedValue ?(typeArgContext : typeArgContext option) ~rawOpens
18121812
if Debug.verbose () then print_endline "[complete_typed_value]--> Texn";
18131813
[
18141814
create
1815-
(full.package.builtInCompletionModules.exnModulePath @ ["Error(error)"]
1815+
((Lazy.force full.package.builtInCompletionModules).exnModulePath
1816+
@ ["Error(error)"]
18161817
|> ident)
18171818
~kind:(Label "Catches errors from JavaScript errors.")
18181819
~docstring:

analysis/src/Packages.ml

+118-96
Original file line numberDiff line numberDiff line change
@@ -74,70 +74,91 @@ let newBsPackage ~rootPath =
7474
let projectFiles, dependenciesFiles, pathsForModule =
7575
match cached with
7676
| Some cached ->
77-
( cached.projectFiles,
78-
cached.dependenciesFiles,
79-
cached.pathsForModule )
77+
( Lazy.from_val cached.projectFiles,
78+
Lazy.from_val cached.dependenciesFiles,
79+
Lazy.from_val cached.pathsForModule )
8080
| None ->
8181
let dependenciesFilesAndPaths =
82-
match FindFiles.findDependencyFiles rootPath config with
83-
| None -> []
84-
| Some (_dependencyDirectories, dependenciesFilesAndPaths) ->
85-
dependenciesFilesAndPaths
82+
Lazy.from_fun (fun () ->
83+
match FindFiles.findDependencyFiles rootPath config with
84+
| None -> []
85+
| Some (_dependencyDirectories, dependenciesFilesAndPaths) ->
86+
dependenciesFilesAndPaths)
8687
in
8788
let sourceDirectories =
88-
FindFiles.getSourceDirectories ~includeDev:true ~baseDir:rootPath
89-
config
89+
Lazy.from_fun (fun () ->
90+
FindFiles.getSourceDirectories ~includeDev:true
91+
~baseDir:rootPath config)
9092
in
9193
let projectFilesAndPaths =
92-
FindFiles.findProjectFiles
93-
~public:(FindFiles.getPublic config)
94-
~namespace ~path:rootPath ~sourceDirectories ~libBs
94+
Lazy.from_fun (fun () ->
95+
FindFiles.findProjectFiles
96+
~public:(FindFiles.getPublic config)
97+
~namespace ~path:rootPath
98+
~sourceDirectories:(Lazy.force sourceDirectories)
99+
~libBs)
95100
in
96101
let pathsForModule =
97-
makePathsForModule ~projectFilesAndPaths
98-
~dependenciesFilesAndPaths
102+
Lazy.from_fun (fun () ->
103+
makePathsForModule
104+
~projectFilesAndPaths:(Lazy.force projectFilesAndPaths)
105+
~dependenciesFilesAndPaths:
106+
(Lazy.force dependenciesFilesAndPaths))
99107
in
100108
let projectFiles =
101-
projectFilesAndPaths |> List.map fst |> FileSet.of_list
109+
Lazy.from_fun (fun () ->
110+
projectFilesAndPaths |> Lazy.force |> List.map fst
111+
|> FileSet.of_list)
102112
in
103113
let dependenciesFiles =
104-
dependenciesFilesAndPaths |> List.map fst |> FileSet.of_list
114+
Lazy.from_fun (fun () ->
115+
dependenciesFilesAndPaths |> Lazy.force |> List.map fst
116+
|> FileSet.of_list)
105117
in
106118
(projectFiles, dependenciesFiles, pathsForModule)
107119
in
108120
Some
109121
(let opens_from_namespace =
110-
match namespace with
111-
| None -> []
112-
| Some namespace ->
113-
let cmt = Filename.concat libBs namespace ^ ".cmt" in
114-
Hashtbl.add pathsForModule namespace (Namespace {cmt});
115-
let path = [FindFiles.nameSpaceToName namespace] in
116-
[path]
122+
Lazy.from_fun (fun () ->
123+
match namespace with
124+
| None -> []
125+
| Some namespace ->
126+
let cmt = Filename.concat libBs namespace ^ ".cmt" in
127+
Hashtbl.add
128+
(Lazy.force pathsForModule)
129+
namespace
130+
(Namespace {cmt});
131+
let path = [FindFiles.nameSpaceToName namespace] in
132+
[path])
117133
in
118134
let opens_from_bsc_flags =
119-
let bind f x = Option.bind x f in
120-
match Json.get "bsc-flags" config |> bind Json.array with
121-
| Some l ->
122-
List.fold_left
123-
(fun opens item ->
124-
match item |> Json.string with
125-
| None -> opens
126-
| Some s -> (
127-
let parts = String.split_on_char ' ' s in
128-
match parts with
129-
| "-open" :: name :: _ ->
130-
let path = name |> String.split_on_char '.' in
131-
path :: opens
132-
| _ -> opens))
133-
[] l
134-
| None -> []
135+
Lazy.from_fun (fun () ->
136+
let bind f x = Option.bind x f in
137+
match Json.get "bsc-flags" config |> bind Json.array with
138+
| Some l ->
139+
List.fold_left
140+
(fun opens item ->
141+
match item |> Json.string with
142+
| None -> opens
143+
| Some s -> (
144+
let parts = String.split_on_char ' ' s in
145+
match parts with
146+
| "-open" :: name :: _ ->
147+
let path = name |> String.split_on_char '.' in
148+
path :: opens
149+
| _ -> opens))
150+
[] l
151+
| None -> [])
135152
in
136153
let opens =
137-
[(if uncurried then "PervasivesU" else "Pervasives"); "JsxModules"]
138-
:: opens_from_namespace
139-
|> List.rev_append opens_from_bsc_flags
140-
|> List.map (fun path -> path @ ["place holder"])
154+
Lazy.from_fun (fun () ->
155+
[
156+
(if uncurried then "PervasivesU" else "Pervasives");
157+
"JsxModules";
158+
]
159+
:: Lazy.force opens_from_namespace
160+
|> List.rev_append (Lazy.force opens_from_bsc_flags)
161+
|> List.map (fun path -> path @ ["place holder"]))
141162
in
142163
{
143164
genericJsxModule;
@@ -150,59 +171,60 @@ let newBsPackage ~rootPath =
150171
opens;
151172
namespace;
152173
builtInCompletionModules =
153-
(if
154-
opens_from_bsc_flags
155-
|> List.find_opt (fun opn ->
156-
match opn with
157-
| ["RescriptCore"] -> true
158-
| _ -> false)
159-
|> Option.is_some
160-
then
161-
{
162-
arrayModulePath = ["Array"];
163-
optionModulePath = ["Option"];
164-
stringModulePath = ["String"];
165-
intModulePath = ["Int"];
166-
floatModulePath = ["Float"];
167-
promiseModulePath = ["Promise"];
168-
listModulePath = ["List"];
169-
resultModulePath = ["Result"];
170-
exnModulePath = ["Exn"];
171-
regexpModulePath = ["RegExp"];
172-
}
173-
else if
174-
opens_from_bsc_flags
175-
|> List.find_opt (fun opn ->
176-
match opn with
177-
| ["Belt"] -> true
178-
| _ -> false)
179-
|> Option.is_some
180-
then
181-
{
182-
arrayModulePath = ["Array"];
183-
optionModulePath = ["Option"];
184-
stringModulePath = ["Js"; "String2"];
185-
intModulePath = ["Int"];
186-
floatModulePath = ["Float"];
187-
promiseModulePath = ["Js"; "Promise"];
188-
listModulePath = ["List"];
189-
resultModulePath = ["Result"];
190-
exnModulePath = ["Js"; "Exn"];
191-
regexpModulePath = ["Js"; "Re"];
192-
}
193-
else
194-
{
195-
arrayModulePath = ["Js"; "Array2"];
196-
optionModulePath = ["Belt"; "Option"];
197-
stringModulePath = ["Js"; "String2"];
198-
intModulePath = ["Belt"; "Int"];
199-
floatModulePath = ["Belt"; "Float"];
200-
promiseModulePath = ["Js"; "Promise"];
201-
listModulePath = ["Belt"; "List"];
202-
resultModulePath = ["Belt"; "Result"];
203-
exnModulePath = ["Js"; "Exn"];
204-
regexpModulePath = ["Js"; "Re"];
205-
});
174+
Lazy.from_fun (fun () ->
175+
if
176+
opens_from_bsc_flags |> Lazy.force
177+
|> List.find_opt (fun opn ->
178+
match opn with
179+
| ["RescriptCore"] -> true
180+
| _ -> false)
181+
|> Option.is_some
182+
then
183+
{
184+
arrayModulePath = ["Array"];
185+
optionModulePath = ["Option"];
186+
stringModulePath = ["String"];
187+
intModulePath = ["Int"];
188+
floatModulePath = ["Float"];
189+
promiseModulePath = ["Promise"];
190+
listModulePath = ["List"];
191+
resultModulePath = ["Result"];
192+
exnModulePath = ["Exn"];
193+
regexpModulePath = ["RegExp"];
194+
}
195+
else if
196+
opens_from_bsc_flags |> Lazy.force
197+
|> List.find_opt (fun opn ->
198+
match opn with
199+
| ["Belt"] -> true
200+
| _ -> false)
201+
|> Option.is_some
202+
then
203+
{
204+
arrayModulePath = ["Array"];
205+
optionModulePath = ["Option"];
206+
stringModulePath = ["Js"; "String2"];
207+
intModulePath = ["Int"];
208+
floatModulePath = ["Float"];
209+
promiseModulePath = ["Js"; "Promise"];
210+
listModulePath = ["List"];
211+
resultModulePath = ["Result"];
212+
exnModulePath = ["Js"; "Exn"];
213+
regexpModulePath = ["Js"; "Re"];
214+
}
215+
else
216+
{
217+
arrayModulePath = ["Js"; "Array2"];
218+
optionModulePath = ["Belt"; "Option"];
219+
stringModulePath = ["Js"; "String2"];
220+
intModulePath = ["Belt"; "Int"];
221+
floatModulePath = ["Belt"; "Float"];
222+
promiseModulePath = ["Js"; "Promise"];
223+
listModulePath = ["Belt"; "List"];
224+
resultModulePath = ["Belt"; "Result"];
225+
exnModulePath = ["Js"; "Exn"];
226+
regexpModulePath = ["Js"; "Re"];
227+
});
206228
uncurried;
207229
}))
208230
| None -> None

analysis/src/ProcessCmt.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ let fileForCmt ~moduleName ~cmt ~uri =
663663
Some file)
664664

665665
let fileForModule moduleName ~package =
666-
match Hashtbl.find_opt package.pathsForModule moduleName with
666+
match Hashtbl.find_opt (Lazy.force package.pathsForModule) moduleName with
667667
| Some paths ->
668668
let uri = getUri paths in
669669
let cmt = getCmtPath ~uri paths in

analysis/src/References.ml

+10-6
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ let getLocItem ~full ~pos ~debug =
6666
| [
6767
({locType = Typed (_, _, LocalReference _)} as li1);
6868
({locType = Typed (_, _, GlobalReference ("Js_OO", ["unsafe_downgrade"], _))}
69-
as li2);
69+
as li2);
7070
li3;
7171
]
7272
(* For older compiler 9.0 or earlier *)
@@ -211,7 +211,9 @@ let definedForLoc ~file ~package locKind =
211211

212212
(** Find alternative declaration: from res in case of interface, or from resi in case of implementation *)
213213
let alternateDeclared ~(file : File.t) ~package (declared : _ Declared.t) tip =
214-
match Hashtbl.find_opt package.pathsForModule file.moduleName with
214+
match
215+
Hashtbl.find_opt (Lazy.force package.pathsForModule) file.moduleName
216+
with
215217
| None -> None
216218
| Some paths -> (
217219
match paths with
@@ -362,7 +364,7 @@ let definitionForLocItem ~full:{file; package} locItem =
362364
None
363365
| TopLevelModule name -> (
364366
maybeLog ("Toplevel " ^ name);
365-
match Hashtbl.find_opt package.pathsForModule name with
367+
match Hashtbl.find_opt (Lazy.force package.pathsForModule) name with
366368
| None -> None
367369
| Some paths ->
368370
let uri = getUri paths in
@@ -486,7 +488,8 @@ let forLocalStamp ~full:{file; extra; package} stamp (tip : Tip.t) =
486488
maybeLog ("Now checking path " ^ pathToString path);
487489
let thisModuleName = file.moduleName in
488490
let externals =
489-
package.projectFiles |> FileSet.elements
491+
Lazy.force package.projectFiles
492+
|> FileSet.elements
490493
|> List.filter (fun name -> name <> file.moduleName)
491494
|> List.map (fun moduleName ->
492495
Cmt.fullsFromModule ~package ~moduleName
@@ -521,7 +524,8 @@ let allReferencesForLocItem ~full:({file; package} as full) locItem =
521524
match locItem.locType with
522525
| TopLevelModule moduleName ->
523526
let otherModulesReferences =
524-
package.projectFiles |> FileSet.elements
527+
Lazy.force package.projectFiles
528+
|> FileSet.elements
525529
|> Utils.filterMap (fun name ->
526530
match ProcessCmt.fileForModule ~package name with
527531
| None -> None
@@ -539,7 +543,7 @@ let allReferencesForLocItem ~full:({file; package} as full) locItem =
539543
|> List.flatten
540544
in
541545
let targetModuleReferences =
542-
match Hashtbl.find_opt package.pathsForModule moduleName with
546+
match Hashtbl.find_opt (Lazy.force package.pathsForModule) moduleName with
543547
| None -> []
544548
| Some paths ->
545549
let moduleSrcToRef src = {uri = Uri.fromPath src; locOpt = None} in

0 commit comments

Comments
 (0)