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

LSP: Implement a cache for typechecking (Serokell, Milestone-1) #4931

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

GoPavel
Copy link

@GoPavel GoPavel commented Mar 4, 2025

In order to improve the performance of the LSP server we need to be able to cache and pass cached the type checking context (Scope.t). It's helpful in two situations:

  • When the server initializes, it scans all files in the vscode workspace, and a library code (e.g. mo:base) is parsed and type-checked as many times as it's imported (transitively) in any other file. Caching fixes this by allowing dependency type information to be reused.
  • Imagine that an IDE user is working on a file, so the content of the file is changed often while its dependencies are not. Scope caching then significantly reduces the compiler effort needed to get a new type information for the file.

Changes:

  • src/pipeline/
    • Add load_progs_cached/chase_imports_cached that are like load_progs/chase_imports but also take the scope cache and populates it. Note, expiring logic is implemented in the TS part of the server, see adjoint MR: Benchmarks & Dependecies' types caching (Serokell, Milestone-1) vscode-motoko#329).
    • Expose these changes in parseMotokoTyped. Create parseMotokoWithDeps that is like parseMotoko but also returns immediate dependencies
  • src/js (IDE API)
    • Replace parseMotokoTyped paths which returns typed AST with parseMotokoTyped path scopeCache which returns typed AST and updated scopeCache
    • Add new method parseMotokoWithDeps which is like parseMotoko but also returns a list of immediate imports

See also interface changes in the node-motoko: dfinity/node-motoko#113

Problem: When the language server loads a file, it goes through the
process of typechecking all of its dependencies. We can be smarter about
it and only load typecheck files that were invalidated.

Solution: Add `lsp_load_progs` that uses a cache to avoid doing
excessive typechecking work. Change `chase_imports` (renamed to
`chase_imports_cached`) to use the cache on valid files. Recreate
`chase_imports` to use `chase_imports_cached` but ignoring the cache.
Expose these changes in `parseMotokoTyped`. Create `parseMotokoWithDeps`
that is like `parseMotoko` but also returns immediate dependencies.
@GoPavel GoPavel requested a review from a team as a code owner March 4, 2025 13:35
@GoPavel GoPavel changed the title Draft: LSP: Implement a cache for typechecking (Serokell) Draft: LSP: Implement a cache for typechecking (Serokell, Milestone-1) Mar 4, 2025
@GoPavel GoPavel changed the title Draft: LSP: Implement a cache for typechecking (Serokell, Milestone-1) LSP: Implement a cache for typechecking (Serokell, Milestone-1) Mar 4, 2025
Copy link
Contributor

@rvanasa rvanasa left a comment

Choose a reason for hiding this comment

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

LGTM on the moc.js side, although I'll wait for @ggreif or @crusso to take a look as well since they're more familiar with this codebase.

heitor-lassarote and others added 2 commits March 7, 2025 15:15
Problem: We need to pass the filename to the import resolver, which
requires passing the filename to `parseMotokoWithDeps`.

Solution: Accept the file name and pass it to `resolve`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants