Skip to content

Commit e092266

Browse files
authored
Temporarily work around GetLineMap CPU usage (#1368)
1 parent 09e7953 commit e092266

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

internal/project/project.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type snapshot struct {
4242
project *Project
4343
positionEncoding lsproto.PositionEncodingKind
4444
program *compiler.Program
45+
lineMaps collections.SyncMap[*ast.SourceFile, *ls.LineMap]
4546
}
4647

4748
// GetLineMap implements ls.Host.
@@ -51,7 +52,13 @@ func (s *snapshot) GetLineMap(fileName string) *ls.LineMap {
5152
if s.project.getFileVersion(file) == scriptInfo.Version() {
5253
return scriptInfo.LineMap()
5354
}
54-
return ls.ComputeLineStarts(file.Text())
55+
// The version changed; recompute the line map.
56+
// !!! This shouldn't happen so often, but does. Probably removable once snapshotting is finished.
57+
if cached, ok := s.lineMaps.Load(file); ok {
58+
return cached
59+
}
60+
lineMap, _ := s.lineMaps.LoadOrStore(file, ls.ComputeLineStarts(file.Text()))
61+
return lineMap
5562
}
5663

5764
// GetPositionEncoding implements ls.Host.

0 commit comments

Comments
 (0)