Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache project config on demand #1000

Merged
merged 10 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.useProjectConfigCache := false;
zth marked this conversation as resolved.
Show resolved Hide resolved
let uri = Uri.fromPath rootPath in
match Packages.getPackage ~uri with
| Some package -> Cache.cacheProject package
| None -> print_endline "\"ERR\"")
| [_; "cache-delete"; rootPath] -> (
Cfg.useProjectConfigCache := false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not seem to be used?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

@cristianoc cristianoc Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I meant to say the line Cfg.useProjectConfigCache := false does not seem to be necessary in the delete command, as none of the code below is affected by it.

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.useProjectConfigCache && 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 useProjectConfigCache =
ref
(try
match Sys.getenv "RESCRIPT_PROJECT_CONFIG_CACHE" with
| "true" -> true
| _ -> false
with _ -> false)
Loading
Loading