perf(search): share immutable copy documents
- commit
- 2f5b5672ca1a814d9488448787afcd4231f9d976
- author
- brice · · 2026-07-11 06:06 UTC
- committer
- brice · · 2026-07-11 06:15 UTC
- parent
- 459c8d4
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
2 files · +22 -13
M internal/usecase/visualsearch/model.go +2 -11
| @@ -18,7 +18,7 @@ type Model struct { | |||
| 18 | 18 | } | |
| 19 | 19 | ||
| 20 | 20 | func New(snapshot scopy.Snapshot) *Model { | |
| 21 | - | m := &Model{snapshot: cloneSnapshot(snapshot), selected: -1} | |
| 21 | + | m := &Model{snapshot: snapshot, selected: -1} | |
| 22 | 22 | m.refresh() | |
| 23 | 23 | return m | |
| 24 | 24 | } | |
| @@ -28,7 +28,6 @@ func (m *Model) Clone() *Model { | |||
| 28 | 28 | return nil | |
| 29 | 29 | } | |
| 30 | 30 | clone := *m | |
| 31 | - | clone.snapshot = cloneSnapshot(m.snapshot) | |
| 32 | 31 | clone.input.SetValue(m.input.Value()) | |
| 33 | 32 | clone.matches = append([]scopy.SearchMatch(nil), m.matches...) | |
| 34 | 33 | return &clone | |
| @@ -38,7 +37,7 @@ func (m *Model) Snapshot() scopy.Snapshot { | |||
| 38 | 37 | if m == nil { | |
| 39 | 38 | return scopy.NewSnapshotFromRows(nil, 0, 0) | |
| 40 | 39 | } | |
| 41 | - | return cloneSnapshot(m.snapshot) | |
| 40 | + | return m.snapshot | |
| 42 | 41 | } | |
| 43 | 42 | ||
| 44 | 43 | func (m *Model) Insert(r rune) { | |
| @@ -146,14 +145,6 @@ func (m *Model) ensureVisible(visible int) { | |||
| 146 | 145 | } | |
| 147 | 146 | } | |
| 148 | 147 | ||
| 149 | - | func cloneSnapshot(s scopy.Snapshot) scopy.Snapshot { | |
| 150 | - | rows := make([][]renderer.Cell, s.Len()) | |
| 151 | - | for i := range rows { | |
| 152 | - | rows[i] = s.Row(i) | |
| 153 | - | } | |
| 154 | - | return scopy.NewSnapshotFromRows(rows, s.Width, s.Height) | |
| 155 | - | } | |
| 156 | - | ||
| 157 | 148 | func (m *Model) Render(inner domain.Size, selectedStyle ...renderer.Style) renderer.Frame { | |
| 158 | 149 | frame := renderer.NewFrame(max(inner.Cols, 0), max(inner.Rows, 0)) | |
| 159 | 150 | if frame.Width == 0 || frame.Height == 0 { | |
| @@ -18,7 +18,7 @@ type Model struct { | |||||
| 18 | } | 18 | } | ||
| 19 | 19 | ||||
| 20 | func New(snapshot scopy.Snapshot) *Model { | 20 | func New(snapshot scopy.Snapshot) *Model { | ||
| 21 | - | m := &Model{snapshot: cloneSnapshot(snapshot), selected: -1} | 21 | + | m := &Model{snapshot: snapshot, selected: -1} |
| 22 | m.refresh() | 22 | m.refresh() | ||
| 23 | return m | 23 | return m | ||
| 24 | } | 24 | } | ||
| @@ -28,7 +28,6 @@ func (m *Model) Clone() *Model { | |||||
| 28 | return nil | 28 | return nil | ||
| 29 | } | 29 | } | ||
| 30 | clone := *m | 30 | clone := *m | ||
| 31 | - | clone.snapshot = cloneSnapshot(m.snapshot) | |||
| 32 | clone.input.SetValue(m.input.Value()) | 31 | clone.input.SetValue(m.input.Value()) | ||
| 33 | clone.matches = append([]scopy.SearchMatch(nil), m.matches...) | 32 | clone.matches = append([]scopy.SearchMatch(nil), m.matches...) | ||
| 34 | return &clone | 33 | return &clone | ||
| @@ -38,7 +37,7 @@ func (m *Model) Snapshot() scopy.Snapshot { | |||||
| 38 | if m == nil { | 37 | if m == nil { | ||
| 39 | return scopy.NewSnapshotFromRows(nil, 0, 0) | 38 | return scopy.NewSnapshotFromRows(nil, 0, 0) | ||
| 40 | } | 39 | } | ||
| 41 | - | return cloneSnapshot(m.snapshot) | 40 | + | return m.snapshot |
| 42 | } | 41 | } | ||
| 43 | 42 | ||||
| 44 | func (m *Model) Insert(r rune) { | 43 | func (m *Model) Insert(r rune) { | ||
| @@ -146,14 +145,6 @@ func (m *Model) ensureVisible(visible int) { | |||||
| 146 | } | 145 | } | ||
| 147 | } | 146 | } | ||
| 148 | 147 | ||||
| 149 | - | func cloneSnapshot(s scopy.Snapshot) scopy.Snapshot { | |||
| 150 | - | rows := make([][]renderer.Cell, s.Len()) | |||
| 151 | - | for i := range rows { | |||
| 152 | - | rows[i] = s.Row(i) | |||
| 153 | - | } | |||
| 154 | - | return scopy.NewSnapshotFromRows(rows, s.Width, s.Height) | |||
| 155 | - | } | |||
| 156 | - | ||||
| 157 | func (m *Model) Render(inner domain.Size, selectedStyle ...renderer.Style) renderer.Frame { | 148 | func (m *Model) Render(inner domain.Size, selectedStyle ...renderer.Style) renderer.Frame { | ||
| 158 | frame := renderer.NewFrame(max(inner.Cols, 0), max(inner.Rows, 0)) | 149 | frame := renderer.NewFrame(max(inner.Cols, 0), max(inner.Rows, 0)) | ||
| 159 | if frame.Width == 0 || frame.Height == 0 { | 150 | if frame.Width == 0 || frame.Height == 0 { | ||
M internal/usecase/visualsearch/model_test.go +20 -2
| @@ -45,18 +45,36 @@ func TestVisualSearchModelFiltersAndSelectsLineMatches(t *testing.T) { | |||
| 45 | 45 | require.Equal(t, matches[1], selected) | |
| 46 | 46 | } | |
| 47 | 47 | ||
| 48 | - | func TestVisualSearchCloneDoesNotAliasInput(t *testing.T) { | |
| 48 | + | func TestVisualSearchNewSnapshotOwnsSourceRows(t *testing.T) { | |
| 49 | + | rows := [][]renderer.Cell{{{Rune: 'a'}, {Rune: 'l'}, {Rune: 'p'}, {Rune: 'h'}, {Rune: 'a'}}} | |
| 50 | + | m := New(scopy.NewSnapshotFromRows(rows, 40, 1)) | |
| 51 | + | ||
| 52 | + | rows[0][0].Rune = 'z' | |
| 53 | + | for _, r := range "alpha" { | |
| 54 | + | m.Insert(r) | |
| 55 | + | } | |
| 56 | + | ||
| 57 | + | matches := m.Matches() | |
| 58 | + | require.Len(t, matches, 1) | |
| 59 | + | require.Equal(t, "alpha", matches[0].Text) | |
| 60 | + | } | |
| 61 | + | ||
| 62 | + | func TestVisualSearchCloneSharesDocumentAndKeepsUIStateIndependent(t *testing.T) { | |
| 49 | 63 | m := New(testSnapshot("alpha", "beta alpha")) | |
| 50 | 64 | for _, r := range "alpha" { | |
| 51 | 65 | m.Insert(r) | |
| 52 | 66 | } | |
| 53 | 67 | clone := m.Clone() | |
| 54 | 68 | ||
| 69 | + | require.Same(t, &m.snapshot.Row(0)[0], &clone.snapshot.Row(0)[0]) | |
| 70 | + | clone.Down() | |
| 55 | 71 | clone.Insert('z') | |
| 56 | 72 | ||
| 57 | 73 | require.Equal(t, "alpha", m.Query()) | |
| 58 | - | require.Equal(t, "alphaz", clone.Query()) | |
| 74 | + | require.Equal(t, 0, m.SelectedIndex()) | |
| 59 | 75 | require.Len(t, m.Matches(), 2) | |
| 76 | + | require.Equal(t, "alphaz", clone.Query()) | |
| 77 | + | require.Equal(t, -1, clone.SelectedIndex()) | |
| 60 | 78 | require.Len(t, clone.Matches(), 0) | |
| 61 | 79 | } | |
| 62 | 80 | ||
| @@ -45,18 +45,36 @@ func TestVisualSearchModelFiltersAndSelectsLineMatches(t *testing.T) { | |||||
| 45 | require.Equal(t, matches[1], selected) | 45 | require.Equal(t, matches[1], selected) | ||
| 46 | } | 46 | } | ||
| 47 | 47 | ||||
| 48 | - | func TestVisualSearchCloneDoesNotAliasInput(t *testing.T) { | 48 | + | func TestVisualSearchNewSnapshotOwnsSourceRows(t *testing.T) { |
| 49 | + | rows := [][]renderer.Cell{{{Rune: 'a'}, {Rune: 'l'}, {Rune: 'p'}, {Rune: 'h'}, {Rune: 'a'}}} | |||
| 50 | + | m := New(scopy.NewSnapshotFromRows(rows, 40, 1)) | |||
| 51 | + | ||||
| 52 | + | rows[0][0].Rune = 'z' | |||
| 53 | + | for _, r := range "alpha" { | |||
| 54 | + | m.Insert(r) | |||
| 55 | + | } | |||
| 56 | + | ||||
| 57 | + | matches := m.Matches() | |||
| 58 | + | require.Len(t, matches, 1) | |||
| 59 | + | require.Equal(t, "alpha", matches[0].Text) | |||
| 60 | + | } | |||
| 61 | + | ||||
| 62 | + | func TestVisualSearchCloneSharesDocumentAndKeepsUIStateIndependent(t *testing.T) { | |||
| 49 | m := New(testSnapshot("alpha", "beta alpha")) | 63 | m := New(testSnapshot("alpha", "beta alpha")) | ||
| 50 | for _, r := range "alpha" { | 64 | for _, r := range "alpha" { | ||
| 51 | m.Insert(r) | 65 | m.Insert(r) | ||
| 52 | } | 66 | } | ||
| 53 | clone := m.Clone() | 67 | clone := m.Clone() | ||
| 54 | 68 | ||||
| 69 | + | require.Same(t, &m.snapshot.Row(0)[0], &clone.snapshot.Row(0)[0]) | |||
| 70 | + | clone.Down() | |||
| 55 | clone.Insert('z') | 71 | clone.Insert('z') | ||
| 56 | 72 | ||||
| 57 | require.Equal(t, "alpha", m.Query()) | 73 | require.Equal(t, "alpha", m.Query()) | ||
| 58 | - | require.Equal(t, "alphaz", clone.Query()) | 74 | + | require.Equal(t, 0, m.SelectedIndex()) |
| 59 | require.Len(t, m.Matches(), 2) | 75 | require.Len(t, m.Matches(), 2) | ||
| 76 | + | require.Equal(t, "alphaz", clone.Query()) | |||
| 77 | + | require.Equal(t, -1, clone.SelectedIndex()) | |||
| 60 | require.Len(t, clone.Matches(), 0) | 78 | require.Len(t, clone.Matches(), 0) | ||
| 61 | } | 79 | } | ||
| 62 | 80 | ||||