on main help sign in
Merge pull request #29 from charmbracelet/perf-lsp-watcher

perf: lsp batching

commit5def75653aeb7d890521d76d766c0b44d1078740 authorRaphael Amorim <rapha850@gmail.com> dateMon 16 Jun 2025, 23:05 (1 year ago) parent20a170454d5622
1 file changed+25−9
internal/lsp/watcher/watcher.go+25−9
internal/lsp/watcher/watcher.go+25−9
@@ -257,7 +257,10 @@ func (w *WorkspaceWatcher) openHighPriorityFiles(ctx context.Context, serverName
257257 }
258258 }
259259
260− // For each pattern, find and open matching files
260+ // Collect all files to open first
261+ var filesToOpen []string
262+
263+ // For each pattern, find matching files
261264 for _, pattern := range patterns {
262265 // Use doublestar.Glob to find files matching the pattern (supports ** patterns)
263266 matches, err := doublestar.Glob(os.DirFS(w.workspacePath), pattern)
@@ -278,7 +281,23 @@ func (w *WorkspaceWatcher) openHighPriorityFiles(ctx context.Context, serverName
278281 continue
279282 }
280283
281− // Open the file
284+ filesToOpen = append(filesToOpen, fullPath)
285+
286+ // Limit the number of files per pattern
287+ if len(filesToOpen) >= 5 && (serverName != "java" && serverName != "jdtls") {
288+ break
289+ }
290+ }
291+ }
292+
293+ // Open files in batches to reduce overhead
294+ batchSize := 3
295+ for i := 0; i < len(filesToOpen); i += batchSize {
296+ end := min(i+batchSize, len(filesToOpen))
297+
298+ // Open batch of files
299+ for j := i; j < end; j++ {
300+ fullPath := filesToOpen[j]
282301 if err := w.client.OpenFile(ctx, fullPath); err != nil {
283302 if cnf.DebugLSP {
284303 logging.Debug("Error opening high-priority file", "path", fullPath, "error", err)
@@ -289,14 +308,11 @@ func (w *WorkspaceWatcher) openHighPriorityFiles(ctx context.Context, serverName
289308 logging.Debug("Opened high-priority file", "path", fullPath)
290309 }
291310 }
311+ }
292312
293− // Add a small delay to prevent overwhelming the server
294− time.Sleep(20 * time.Millisecond)
295−
296− // Limit the number of files opened per pattern
297− if filesOpened >= 5 && (serverName != "java" && serverName != "jdtls") {
298− break
299− }
313+ // Only add delay between batches, not individual files
314+ if end < len(filesToOpen) {
315+ time.Sleep(50 * time.Millisecond)
300316 }
301317 }
302318