Skip to content

Commit

Permalink
Improve filtered rendering performance (#145)
Browse files Browse the repository at this point in the history
* Add benchmark for rendering which highlight perf issue

* Ignore profiling files

* Cache visible rows for performance improvement
  • Loading branch information
Evertras authored Mar 18, 2023
1 parent aeeff68 commit 525e143
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
# Test binary, built with `go test -c`
*.test

# Profiling outputs
*.prof

# Outputs of the go coverage tool, specifically when used with LiteIDE
*.out
*.coverage
Expand Down
2 changes: 1 addition & 1 deletion table/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func isRowMatched(columns []Column, row Row, filter string) bool {
data = dataV.Data
}

target := ""
var target string
switch dataV := data.(type) {
case string:
target = dataV
Expand Down
51 changes: 51 additions & 0 deletions table/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,54 @@ func BenchmarkFilteredScrolling(b *testing.B) {
hitKey('j')
}
}

func BenchmarkFilteredScrollingPaged(b *testing.B) {
// Scrolling through a filtered table with many rows should be quick
// https://github.com/Evertras/bubble-table/issues/135
const rowCount = 40000
columns := []Column{NewColumn("title", "title", 10).WithFiltered(true)}
rows := make([]Row, rowCount)

for i := 0; i < rowCount; i++ {
rows[i] = NewRow(RowData{
"title": fmt.Sprintf("%d", i),
})
}

model := New(columns).WithRows(rows).Filtered(true).WithPageSize(50)
model = model.WithFilterInputValue("1")

b.ResetTimer()

for i := 0; i < b.N; i++ {
model, _ = model.Update(
tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune{'j'},
})
}
}

func BenchmarkFilteredRenders(b *testing.B) {
// Rendering a filtered table should be fast
// https://github.com/Evertras/bubble-table/issues/135
const rowCount = 40000
columns := []Column{NewColumn("title", "title", 10).WithFiltered(true)}
rows := make([]Row, rowCount)

for i := 0; i < rowCount; i++ {
rows[i] = NewRow(RowData{
"title": fmt.Sprintf("%d", i),
})
}

model := New(columns).WithRows(rows).Filtered(true).WithPageSize(50)
model = model.WithFilterInputValue("1")

b.ResetTimer()

for i := 0; i < b.N; i++ {
// Don't care about result, just rendering
_ = model.View()
}
}
4 changes: 4 additions & 0 deletions table/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type Model struct {
columns []Column
rows []Row

// Caches for optimizations
visibleRowCacheUpdated bool
visibleRowCache []Row

// Shown when data is missing from a row
missingDataIndicator interface{}

Expand Down
4 changes: 4 additions & 0 deletions table/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (m Model) HeaderStyle(style lipgloss.Style) Model {
// WithRows sets the rows to show as data in the table.
func (m Model) WithRows(rows []Row) Model {
m.rows = rows
m.visibleRowCacheUpdated = false

if m.rowCursorIndex >= len(m.rows) {
m.rowCursorIndex = len(m.rows) - 1
Expand Down Expand Up @@ -129,6 +130,7 @@ func (m Model) Focused(focused bool) Model {
// Filtered allows the table to show rows that match the filter.
func (m Model) Filtered(filtered bool) Model {
m.filtered = filtered
m.visibleRowCacheUpdated = false

return m
}
Expand Down Expand Up @@ -284,6 +286,7 @@ func (m Model) WithFilterInput(input textinput.Model) Model {
}

m.filterTextInput = input
m.visibleRowCacheUpdated = false

return m
}
Expand All @@ -298,6 +301,7 @@ func (m Model) WithFilterInputValue(value string) Model {

m.filterTextInput.SetValue(value)
m.filterTextInput.Blur()
m.visibleRowCacheUpdated = false

return m
}
Expand Down
9 changes: 8 additions & 1 deletion table/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ func (m *Model) GetCurrentFilter() string {
}

// GetVisibleRows returns sorted and filtered rows.
func (m Model) GetVisibleRows() []Row {
func (m *Model) GetVisibleRows() []Row {
if m.visibleRowCacheUpdated {
return m.visibleRowCache
}

rows := make([]Row, len(m.rows))
copy(rows, m.rows)
if m.filtered {
rows = m.getFilteredRows(rows)
}
rows = getSortedRows(m.sortOrder, rows)

m.visibleRowCache = rows
m.visibleRowCacheUpdated = true

return rows
}

Expand Down
8 changes: 8 additions & 0 deletions table/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func (m Model) SortByAsc(columnKey string) Model {
},
}

m.visibleRowCacheUpdated = false

return m
}

Expand All @@ -49,6 +51,8 @@ func (m Model) SortByDesc(columnKey string) Model {
},
}

m.visibleRowCacheUpdated = false

return m
}

Expand All @@ -62,6 +66,8 @@ func (m Model) ThenSortByAsc(columnKey string) Model {
},
}, m.sortOrder...)

m.visibleRowCacheUpdated = false

return m
}

Expand All @@ -75,6 +81,8 @@ func (m Model) ThenSortByDesc(columnKey string) Model {
},
}, m.sortOrder...)

m.visibleRowCacheUpdated = false

return m
}

Expand Down
4 changes: 4 additions & 0 deletions table/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (m *Model) toggleSelect() {
rows[m.rowCursorIndex].selected = !currentSelectedState

m.rows = rows
m.visibleRowCacheUpdated = false

m.appendUserEvent(UserEventRowSelectToggled{
RowIndex: m.rowCursorIndex,
Expand All @@ -55,6 +56,7 @@ func (m Model) updateFilterTextInput(msg tea.Msg) (Model, tea.Cmd) {
}
m.filterTextInput, cmd = m.filterTextInput.Update(msg)
m.pageFirst()
m.visibleRowCacheUpdated = false

return m, cmd
}
Expand Down Expand Up @@ -126,6 +128,8 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, nil
}

m.visibleRowCacheUpdated = false

if m.filterTextInput.Focused() {
var cmd tea.Cmd
m, cmd = m.updateFilterTextInput(msg)
Expand Down

0 comments on commit 525e143

Please sign in to comment.