terraform-ls icon indicating copy to clipboard operation
terraform-ls copied to clipboard

Avoid parsing files if content has not changed

Open radeksimko opened this issue 3 years ago • 0 comments

Background

This is another (different) attempt at improving performance which follows an earlier abandoned attempt captured in https://github.com/hashicorp/terraform-ls/issues/989 - Using ModTime would not be reliable and also we would have no way of capturing ModTime for the virtual documents anyway! 😅

As part of https://github.com/hashicorp/terraform-ls/pull/1006 we are removing de-duplication of jobs upon enqueuing and instead starting to rely on internal state. This is good strategy for the walker, which can check the internal state (i.e. avoid running if the job has already run at least once), but not so much for textDocument/didOpen.

In an ideal case the client would tell the server as part of LSP whether the document being sent in textDocument/didOpen differs from what's on disk. Unfortunately this is not possible today. The request does contain document version, but that's always 1 (at least from testing with VS Code) regardless of whether I open document w/ unsaved changes or not.

Therefore the work of figuring out the difference is left on the server.

See also:

  • https://github.com/microsoft/language-server-protocol/issues/1369
  • gopls way of dealing with this: https://github.com/golang/tools/blob/3e0a5031e3cec09527c92842fdf38af0fb48c252/internal/lsp/source/view.go#L560-L564

Proposal

TODO: add logic to calculate hash (sha256) for any file & compare hashes of newly received document content as part of ParseVariables and ParseModuleConfiguration - then return StateNotChangedErr early. Check for that error in downstream jobs and avoid running those downstream jobs if upstream returned that error.

radeksimko avatar Aug 09 '22 12:08 radeksimko