Skip to content

Commit 7c9f1bd

Browse files
authored
Cache project config on demand (#1000)
* cache project config on demand * e2e for the new cache mode * only look up project files etc when needed * comment * Revert "only look up project files etc when needed" This reverts commit bc71f76. * remove now irrelevant comment * changelog * conditionally * rename * replace instead of add
1 parent 5734b9f commit 7c9f1bd

File tree

11 files changed

+307
-169
lines changed

11 files changed

+307
-169
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
## 1.52.0
2020

21+
#### :rocket: New Feature
22+
23+
- Experimental support for caching the project config to reduce latency. https://github.com/rescript-lang/rescript-vscode/pull/1000
24+
2125
#### :bug: Bug Fix
2226

2327
- Fix highlighting of other languages being affected by rescript-vscode. https://github.com/rescript-lang/rescript-vscode/pull/973

analysis/bin/main.ml

+17
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,23 @@ let main () =
110110
path line col
111111
in
112112
match args with
113+
| [_; "cache-project"; rootPath] -> (
114+
Cfg.readProjectConfigCache := false;
115+
let uri = Uri.fromPath rootPath in
116+
match Packages.getPackage ~uri with
117+
| Some package -> Cache.cacheProject package
118+
| None -> print_endline "\"ERR\"")
119+
| [_; "cache-delete"; rootPath] -> (
120+
Cfg.readProjectConfigCache := false;
121+
let uri = Uri.fromPath rootPath in
122+
match Packages.findRoot ~uri (Hashtbl.create 0) with
123+
| Some (`Bs rootPath) -> (
124+
match BuildSystem.getLibBs rootPath with
125+
| None -> print_endline "\"ERR\""
126+
| Some libBs ->
127+
Cache.deleteCache (Cache.targetFileFromLibBs libBs);
128+
print_endline "\"OK\"")
129+
| _ -> print_endline "\"ERR: Did not find root \"")
113130
| [_; "completion"; path; line; col; currentFile] ->
114131
printHeaderInfo path line col;
115132
Commands.completion ~debug ~path

analysis/src/Cache.ml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
open SharedTypes
2+
3+
type cached = {
4+
projectFiles: FileSet.t;
5+
dependenciesFiles: FileSet.t;
6+
pathsForModule: (file, paths) Hashtbl.t;
7+
}
8+
9+
let writeCache filename (data : cached) =
10+
let oc = open_out_bin filename in
11+
Marshal.to_channel oc data [];
12+
close_out oc
13+
14+
let readCache filename =
15+
if !Cfg.readProjectConfigCache && Sys.file_exists filename then
16+
try
17+
let ic = open_in_bin filename in
18+
let data : cached = Marshal.from_channel ic in
19+
close_in ic;
20+
Some data
21+
with _ -> None
22+
else None
23+
24+
let deleteCache filename = try Sys.remove filename with _ -> ()
25+
26+
let targetFileFromLibBs libBs = Filename.concat libBs ".project-files-cache"
27+
28+
let cacheProject (package : package) =
29+
let cached =
30+
{
31+
projectFiles = package.projectFiles;
32+
dependenciesFiles = package.dependenciesFiles;
33+
pathsForModule = package.pathsForModule;
34+
}
35+
in
36+
match BuildSystem.getLibBs package.rootPath with
37+
| None -> print_endline "\"ERR\""
38+
| Some libBs ->
39+
let targetFile = targetFileFromLibBs libBs in
40+
writeCache targetFile cached;
41+
print_endline "\"OK\""

analysis/src/Cfg.ml

+8
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ let inIncrementalTypecheckingMode =
99
| "true" -> true
1010
| _ -> false
1111
with _ -> false)
12+
13+
let readProjectConfigCache =
14+
ref
15+
(try
16+
match Sys.getenv "RESCRIPT_PROJECT_CONFIG_CACHE" with
17+
| "true" -> true
18+
| _ -> false
19+
with _ -> false)

0 commit comments

Comments
 (0)