Skip to content

Commit e92a18f

Browse files
adonovangopherbot
authored andcommitted
internal/lsp/lsppos: reduce allocations in NewMapper
Before, NewMapper accounts for 2.1% of bytes allocated in the WorkspaceSymbols benchmark. This change causes the newline index table to be allocated once instead of by appending. The function now accounts for 0.55%. Change-Id: I9172dd34ee2be9e7175e311d4a6518f1e6660a5f Reviewed-on: https://go-review.googlesource.com/c/tools/+/415501 Auto-Submit: Alan Donovan <[email protected]> gopls-CI: kokoro <[email protected]> Run-TryBot: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent f487f36 commit e92a18f

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

internal/lsp/lsppos/lsppos.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package lsppos
1818

1919
import (
20+
"bytes"
2021
"errors"
2122
"sort"
2223
"unicode/utf8"
@@ -36,9 +37,10 @@ type Mapper struct {
3637

3738
// NewMapper creates a new Mapper for the given content.
3839
func NewMapper(content []byte) *Mapper {
40+
nlines := bytes.Count(content, []byte("\n"))
3941
m := &Mapper{
4042
content: content,
41-
lines: []int{0},
43+
lines: make([]int, 1, nlines+1), // initially []int{0}
4244
}
4345
for offset, b := range content {
4446
if b == '\n' {

0 commit comments

Comments
 (0)