Skip to content

Commit

Permalink
Merge pull request #4835 from dweymouth/list-size-optimization
Browse files Browse the repository at this point in the history
Improve performance of content size calculation in List
  • Loading branch information
dweymouth authored May 18, 2024
2 parents ecf8c68 + b8b6b5c commit 4fa888a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
10 changes: 5 additions & 5 deletions widget/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,15 @@ func (l *List) contentMinSize() fyne.Size {
}

height := float32(0)
totalCustom := 0
templateHeight := l.itemMin.Height
for item := 0; item < items; item++ {
itemHeight, ok := l.itemHeights[item]
if ok {
for id, itemHeight := range l.itemHeights {
if id < items {
totalCustom++
height += itemHeight
} else {
height += templateHeight
}
}
height += float32(items-totalCustom) * templateHeight

return fyne.NewSize(l.itemMin.Width, height+separatorThickness*float32(items-1))
}
Expand Down
26 changes: 26 additions & 0 deletions widget/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,3 +672,29 @@ func TestList_RefreshUpdatesAllItems(t *testing.T) {
list.Refresh()
assert.Equal(t, "0.0.", printOut)
}

var minSize fyne.Size

func BenchmarkContentMinSize(b *testing.B) {
b.StopTimer()

l := NewList(
func() int { return 1000000 },
func() fyne.CanvasObject {
return NewLabel("Test")
},
func(id ListItemID, item fyne.CanvasObject) {
item.(*Label).SetText(fmt.Sprintf("%d", id))
},
)
l.SetItemHeight(10, 55)
l.SetItemHeight(12345, 2)

min := fyne.Size{}
b.StartTimer()
for i := 0; i < b.N; i++ {
min = l.contentMinSize()
}

minSize = min
}

0 comments on commit 4fa888a

Please sign in to comment.