Skip to content

Commit

Permalink
internal/lsp/cache: improve snapshot clone perfomance
Browse files Browse the repository at this point in the history
The existing implementation uses a lot of URI.Filename() calls,
which are pretty expensive. Moreover, these calls are not necessary,
as long as all the actions could be done with the raw URI string.
This patch removes such calls and uses simple string casts.

Updates golang/go#45686
  • Loading branch information
anton-kuklin committed Apr 22, 2021
1 parent f946a15 commit 41b758d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions internal/lsp/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ func contains(views []*View, view *View) bool {
}

func inVendor(uri span.URI) bool {
toSlash := filepath.ToSlash(uri.Filename())
toSlash := filepath.ToSlash(string(uri))
if !strings.Contains(toSlash, "/vendor/") {
return false
}
Expand Down Expand Up @@ -1548,7 +1548,7 @@ func (s *snapshot) clone(ctx, bgCtx context.Context, changes map[span.URI]*fileC
// For internal tests, we need _test files, not just the normal
// ones. External tests only have _test files, but we can check
// them anyway.
if m.forTest != "" && !strings.HasSuffix(uri.Filename(), "_test.go") {
if m.forTest != "" && !strings.HasSuffix(string(uri), "_test.go") {
continue
}
if _, ok := result.files[uri]; ok {
Expand Down
4 changes: 2 additions & 2 deletions internal/lsp/cache/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (w *workspace) invalidate(ctx context.Context, changes map[span.URI]*fileCh
// here.
if result.moduleSource != goplsModWorkspace {
for uri, change := range changes {
if !isGoMod(uri) || !source.InDir(result.root.Filename(), uri.Filename()) {
if !isGoMod(uri) || !source.InDir(string(result.root), string(uri)) {
continue
}
changed = true
Expand All @@ -350,7 +350,7 @@ func (w *workspace) invalidate(ctx context.Context, changes map[span.URI]*fileCh
continue
}
// TODO(rFindley) factor out this URI mangling.
dir := filepath.Dir(uri.Filename())
dir := filepath.Dir(string(uri))
modURI := span.URIFromPath(filepath.Join(dir, "go.mod"))
if _, active := result.activeModFiles[modURI]; !active {
continue
Expand Down

0 comments on commit 41b758d

Please sign in to comment.