Skip to content

Conversation

jakebailey
Copy link
Member

Something I'm experiencing, even more after #1363, is that a load of CPU time is spent in ComputeLineStarts because when I'm typing, the versions changed and the mutable-ness of the script infos and projects means we skip reusing the line map.

This leads to a catastrophic CPU usage where we never cache any line starts at all once the version is wrong, so requests in files like checker.ts for document symbols just keep going forever, recomputing the same info.

I believe that this all should go away in the new snapshot model, but for now this trick seems to contain the problem enough to make the editor usable again while typing, including making completions in checker.ts basically instant again.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a temporary caching layer for line maps in the snapshot to reduce repeated CPU-heavy calls to ComputeLineStarts when script versions change frequently.

  • Adds a SyncMap field to store and reuse computed LineMap instances.
  • Updates GetLineMap to check the cache before recomputing on version mismatch.
Comments suppressed due to low confidence (1)

internal/project/project.go:57

  • There are no tests verifying that the cache is used on subsequent calls. Add tests to ensure that after the first computation, further calls return the cached LineMap instead of recomputing.
	if cached, ok := s.lineMaps.Load(file); ok {

program *compiler.Program
lineMaps collections.SyncMap[*ast.SourceFile, *ls.LineMap]
}

Copy link
Preview

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

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

[nitpick] The lineMaps cache is unbounded and may retain entries even after files are closed or snapshots are discarded. Consider implementing eviction logic or clearing entries when files are removed to prevent potential memory growth.

Suggested change
// RemoveLineMap removes the line map for a given source file from the cache.
func (s *snapshot) RemoveLineMap(file *ast.SourceFile) {
s.lineMaps.Delete(file)
}

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, these snapshots are request-scoped

func (s *snapshot) GetLineMap(fileName string) *ls.LineMap {
file := s.program.GetSourceFile(fileName)
scriptInfo := s.project.host.DocumentStore().GetScriptInfoByPath(file.Path())
if s.project.getFileVersion(file) == scriptInfo.Version() {
Copy link
Member Author

Choose a reason for hiding this comment

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

I still don't really understand why we're doing this at all; it seems to me that we should not cache line maps on script infos at all, instead do it per-source-file, and then just always use that. But I seem to remember someone saying that was wrong for some reason, so, this hack seems better than nothing?

Copy link
Member

Choose a reason for hiding this comment

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

I recall a conversation around line maps differing between LS and CLI

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a different thing but yeah they do.

Copy link
Member

@gabritto gabritto left a comment

Choose a reason for hiding this comment

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

Seems ok for a temporary fix, but I also don't get why a version change would be more frequent than a change to a source file object...

@jakebailey
Copy link
Member Author

I'm pretty sure this is a race condition while typing, when the ScriptInfo has been updated with new text, but the request is still running on the old Program and therefore has a file with a different version.

@jakebailey jakebailey enabled auto-merge July 7, 2025 23:03
@jakebailey jakebailey added this pull request to the merge queue Jul 8, 2025
Merged via the queue into main with commit e092266 Jul 8, 2025
22 checks passed
@jakebailey jakebailey deleted the jabaile/cache-line-starts-temporarily branch July 8, 2025 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants