Skip to content

Commit 2d56a14

Browse files
committed
fix: cache countLines during export
1 parent 795b36b commit 2d56a14

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

lang/collect/export.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ func (c *Collector) fileLine(loc Location) uniast.FileLine {
4444
rel = filepath.Base(loc.URI.File())
4545
}
4646
text := c.cli.GetFile(loc.URI).Text
47+
uri_str := string(loc.URI)
4748
return uniast.FileLine{
4849
File: rel,
4950
Line: loc.Range.Start.Line,
50-
StartOffset: lsp.PositionOffset(text, loc.Range.Start),
51-
EndOffset: lsp.PositionOffset(text, loc.Range.End),
51+
StartOffset: lsp.PositionOffsetIdentified(uri_str, text, loc.Range.Start),
52+
EndOffset: lsp.PositionOffsetIdentified(uri_str, text, loc.Range.End),
5253
}
5354
}
5455

lang/cxx/spec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (c *CxxSpec) FunctionSymbol(sym lsp.DocumentSymbol) (int, []int, []int, []i
164164
// TODO: attributes may contain parens. also inline structs.
165165

166166
endRelOffset := 0
167-
lines := utils.CountLinesCached(sym.Text)
167+
lines := utils.CountLinesPooled(sym.Text)
168168
phase := 0
169169
for i, tok := range sym.Tokens {
170170
switch phase {

lang/lsp/utils.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
)
3737

3838
func GetDistance(text string, start Position, pos Position) int {
39-
lines := utils.CountLinesCached(text)
39+
lines := utils.CountLinesPooled(text)
4040
defer utils.PutCount(lines)
4141
// find the line of the position
4242
return (*lines)[pos.Line-start.Line] + pos.Character - start.Character
@@ -59,12 +59,23 @@ func RelativePostionWithLines(lines []int, textPos Position, pos Position) int {
5959
return lines[l] + pos.Character - textPos.Character
6060
}
6161

62+
func PositionOffsetIdentified(uri string, text string, pos Position) int {
63+
if pos.Line < 0 || pos.Character < 0 {
64+
log.Error("invalid text position: %+v", pos)
65+
return -1
66+
}
67+
lines := utils.CountLinesCached(uri, text)
68+
defer utils.PutCount(lines)
69+
70+
return RelativePostionWithLines(*lines, Position{Line: 0, Character: 0}, pos)
71+
}
72+
6273
func PositionOffset(text string, pos Position) int {
6374
if pos.Line < 0 || pos.Character < 0 {
6475
log.Error("invalid text position: %+v", pos)
6576
return -1
6677
}
67-
lines := utils.CountLinesCached(text)
78+
lines := utils.CountLinesPooled(text)
6879
defer utils.PutCount(lines)
6980

7081
return RelativePostionWithLines(*lines, Position{Line: 0, Character: 0}, pos)

lang/rust/spec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func (c *RustSpec) FunctionSymbol(sym lsp.DocumentSymbol) (int, []int, []int, []
261261
if where == -1 {
262262
where = len(tokens) - 1
263263
}
264-
lines := utils.CountLinesCached(sym.Text)
264+
lines := utils.CountLinesPooled(sym.Text)
265265

266266
// find the typeParam's type token between "fn" and "("
267267
var typeParams []int

lang/utils/strings.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,19 @@ func PutCount(count *[]int) {
2828
countPool.Put(count)
2929
}
3030

31-
func CountLinesCached(text string) *[]int {
31+
var cachedLines = sync.Map{}
32+
33+
func CountLinesCached(ident string, text string) *[]int {
34+
if v, ok := cachedLines.Load(ident); ok {
35+
res := v.([]int)
36+
return &res
37+
}
38+
tmp := CountLines(text)
39+
cachedLines.Store(ident, tmp)
40+
return &tmp
41+
}
42+
43+
func CountLinesPooled(text string) *[]int {
3244
tmp := countPool.Get().(*[]int)
3345
*tmp = append(*tmp, 0)
3446
for i, c := range text {

0 commit comments

Comments
 (0)