diff --git a/ttcn3/v2/syntax/syntax.go b/ttcn3/v2/syntax/syntax.go index ac2122a1..d226efb2 100644 --- a/ttcn3/v2/syntax/syntax.go +++ b/ttcn3/v2/syntax/syntax.go @@ -297,6 +297,9 @@ func (t *tree) position(pos int) Position { func (t *tree) searchLines(pos int) int { // TODO(5nord) add line cache i, j := 0, len(t.lines) + if t.cachedLineBegin <= pos && pos < t.cachedLineEnd { + return t.cachedLine + } for i < j { h := int(uint(i+j) >> 1) // avoid overflow when computing h // i ≤ h < j @@ -306,7 +309,16 @@ func (t *tree) searchLines(pos int) int { j = h } } - return int(i) - 1 + line := int(i) - 1 + t.cachedLine = line + t.cachedLineBegin = t.lines[line] + if line > len(t.lines) { + t.cachedLineEnd = t.lines[line+1] + } else { + t.cachedLineEnd = len(t.lines) + } + + return line } // Err returns the error of the subtree. @@ -349,6 +361,9 @@ type tree struct { lines []int content []byte errs []error + cachedLine int + cachedLineBegin int + cachedLineEnd int } // event represents a single event in a Tree.