jesseduffield.lazygit/pkg/gocui/search_test.go
Stefan Haller 0505e778b3 Work the search positions out when they are read, not on each line written
A view's search positions were worked out again from every write, and
each of those walks the whole view. Content arrives a line at a time, so
rendering into a searched view costs a walk per line. Streaming 2000
lines takes 565ms, where the same render into an unsearched view takes
about 10ms.

Mark the positions stale on a write instead, and work them out where they
are read: when the view is drawn, when a key steps through the matches,
when the status is asked for. That is at most once a frame, and the same
2000 lines now take 8ms.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-05 15:09:52 +02:00

59 lines
1.4 KiB
Go

package gocui
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
// writeLines writes the given lines to the view, as a task rendering content into it
// does: one line at a time.
func writeLines(v *View, lines ...string) {
for _, line := range lines {
fmt.Fprintf(v, "%s\n", line)
}
}
func TestSearchStatusAfterTheMatchesChange(t *testing.T) {
v := NewView("name", 0, 0, 40, 10, OutputNormal)
writeLines(v, "match", "other", "match", "other", "match")
v.Search("match", nil)
_ = v.gotoNextMatch()
_ = v.gotoNextMatch()
index, total := v.GetSearchStatus()
assert.Equal(t, 2, index)
assert.Equal(t, 3, total)
// The content is re-rendered with only the first of those matches left in it.
v.Clear()
writeLines(v, "match", "other", "other")
index, total = v.GetSearchStatus()
assert.Equal(t, 0, index)
assert.Equal(t, 1, total)
}
func TestSearchPositionsFollowStreamedContent(t *testing.T) {
v := NewView("name", 0, 0, 40, 10, OutputNormal)
v.Search("match", nil)
// A render arrives a line at a time, and the status describes all of it.
writeLines(v, "other", "match", "other", "match")
_, total := v.GetSearchStatus()
assert.Equal(t, 2, total)
}
func BenchmarkWriteToSearchedView(b *testing.B) {
for b.Loop() {
v := NewView("name", 0, 0, 100, 40, OutputNormal)
v.Search("match", nil)
for i := range 2000 {
fmt.Fprintf(v, "line %d of a diff, most of which does not match\n", i)
}
v.GetSearchStatus()
}
}