Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added line cache #678

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion ttcn3/v2/syntax/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down