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

Avoid re-processing files on textDocument/didOpen if content is the same as on disk #1375

Open
radeksimko opened this issue Sep 1, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request performance Gotta go fast

Comments

@radeksimko
Copy link
Member

Context

Currently we re-process whole module whenever a file gets opened as we cannot be sure that the client isn't sending us modified content along with the open notification. e.g. the IDE may have restored some unsaved changes to the document.

This should not be a very common case (restoring unsaved changes) and our current implementation is somewhat pessimistic to ensure that we always catch any such changes if they come.

This in turn means we do much more work than we possibly need to upon opening every file.

// We reparse because the file being opened may not match
// (originally parsed) content on the disk
// TODO: Do this only if we can verify the file differs?
modHandle := document.DirHandleFromPath(mod.Path)
jobIds, err := svc.indexer.DocumentOpened(ctx, modHandle)
if err != nil {
return err
}

Proposal

Consider comparing the received file content against the file content on disk to understand if it differs and hence if it's necessary to re-process the module.

Implementation Notes

We could try to calculate and compare checksums, e.g.

func documentDiffersFromDisk(dh document.Handle, newText []byte) (bool, error) {
	f, err := os.Open(dh.FullPath())
	if err != nil {
		return true, err
	}
	oldHash := sha256.New()
	if _, err := io.Copy(oldHash, f); err != nil {
		return true, err
	}
	f.Close()

	newHash := sha256.New()
	newHash.Write(newText)

	return bytes.Equal(oldHash.Sum(nil), newHash.Sum(nil)), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request performance Gotta go fast
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants