Skip to content

Commit

Permalink
Cache project config on demand (#1000)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
zth authored Jun 4, 2024
1 parent 5734b9f commit 7c9f1bd
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 169 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

## 1.52.0

#### :rocket: New Feature

- Experimental support for caching the project config to reduce latency. https://github.com/rescript-lang/rescript-vscode/pull/1000

#### :bug: Bug Fix

- Fix highlighting of other languages being affected by rescript-vscode. https://github.com/rescript-lang/rescript-vscode/pull/973
Expand Down
17 changes: 17 additions & 0 deletions analysis/bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ let main () =
path line col
in
match args with
| [_; "cache-project"; rootPath] -> (
Cfg.readProjectConfigCache := false;
let uri = Uri.fromPath rootPath in
match Packages.getPackage ~uri with
| Some package -> Cache.cacheProject package
| None -> print_endline "\"ERR\"")
| [_; "cache-delete"; rootPath] -> (
Cfg.readProjectConfigCache := false;
let uri = Uri.fromPath rootPath in
match Packages.findRoot ~uri (Hashtbl.create 0) with
| Some (`Bs rootPath) -> (
match BuildSystem.getLibBs rootPath with
| None -> print_endline "\"ERR\""
| Some libBs ->
Cache.deleteCache (Cache.targetFileFromLibBs libBs);
print_endline "\"OK\"")
| _ -> print_endline "\"ERR: Did not find root \"")
| [_; "completion"; path; line; col; currentFile] ->
printHeaderInfo path line col;
Commands.completion ~debug ~path
Expand Down
41 changes: 41 additions & 0 deletions analysis/src/Cache.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
open SharedTypes

type cached = {
projectFiles: FileSet.t;
dependenciesFiles: FileSet.t;
pathsForModule: (file, paths) Hashtbl.t;
}

let writeCache filename (data : cached) =
let oc = open_out_bin filename in
Marshal.to_channel oc data [];
close_out oc

let readCache filename =
if !Cfg.readProjectConfigCache && Sys.file_exists filename then
try
let ic = open_in_bin filename in
let data : cached = Marshal.from_channel ic in
close_in ic;
Some data
with _ -> None
else None

let deleteCache filename = try Sys.remove filename with _ -> ()

let targetFileFromLibBs libBs = Filename.concat libBs ".project-files-cache"

let cacheProject (package : package) =
let cached =
{
projectFiles = package.projectFiles;
dependenciesFiles = package.dependenciesFiles;
pathsForModule = package.pathsForModule;
}
in
match BuildSystem.getLibBs package.rootPath with
| None -> print_endline "\"ERR\""
| Some libBs ->
let targetFile = targetFileFromLibBs libBs in
writeCache targetFile cached;
print_endline "\"OK\""
8 changes: 8 additions & 0 deletions analysis/src/Cfg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ let inIncrementalTypecheckingMode =
| "true" -> true
| _ -> false
with _ -> false)

let readProjectConfigCache =
ref
(try
match Sys.getenv "RESCRIPT_PROJECT_CONFIG_CACHE" with
| "true" -> true
| _ -> false
with _ -> false)
Loading

0 comments on commit 7c9f1bd

Please sign in to comment.