Skip to content

Commit

Permalink
reuse allocated mem from container.Objects slice
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Dec 10, 2023
1 parent 81f4c1b commit 5032db3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
22 changes: 15 additions & 7 deletions widget/gridwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,7 @@ type itemAndID struct {
}

type gridWrapLayout struct {
list *GridWrap
children []fyne.CanvasObject
list *GridWrap

itemPool syncPool
slicePool sync.Pool // *[]itemAndID
Expand Down Expand Up @@ -595,7 +594,9 @@ func (l *gridWrapLayout) updateGrid(refresh bool) {
oldVisibleLen := len(l.visible)
l.visible = l.visible[:0]

var cells []fyne.CanvasObject
c := l.list.scroller.Content.(*fyne.Container)
oldObjLen := len(c.Objects)
c.Objects = c.Objects[:0]
y := offY
curItemID := minItem
for row := minRow; row <= maxRow && curItemID <= maxItem; row++ {
Expand All @@ -617,21 +618,19 @@ func (l *gridWrapLayout) updateGrid(refresh bool) {

x += l.list.itemMin.Width + theme.Padding()
l.visible = append(l.visible, itemAndID{item: item, id: curItemID})
cells = append(cells, item)
c.Objects = append(c.Objects, item)
curItemID++
}
y += l.list.itemMin.Height + theme.Padding()
}
l.nilOldSliceData(c.Objects, len(c.Objects), oldObjLen)
l.nilOldVisibleSliceData(l.visible, len(l.visible), oldVisibleLen)

for _, old := range wasVisible {
if _, ok := l.searchVisible(l.visible, old.id); !ok {
l.itemPool.Release(old.item)
}
}
l.children = cells

l.list.scroller.Content.(*fyne.Container).Objects = l.children

// make a local deep copy of l.visible since rest of this function is unlocked
// and cannot safely access l.visible
Expand Down Expand Up @@ -667,6 +666,15 @@ func (l *gridWrapLayout) searchVisible(visible []itemAndID, id GridWrapItemID) (
return nil, false
}

func (l *gridWrapLayout) nilOldSliceData(objs []fyne.CanvasObject, len, oldLen int) {
if oldLen > len {
objs = objs[:oldLen] // gain view into old data
for i := len; i < oldLen; i++ {
objs[i] = nil
}
}
}

func (l *gridWrapLayout) nilOldVisibleSliceData(objs []itemAndID, len, oldLen int) {
if oldLen > len {
objs = objs[:oldLen] // gain view into old data
Expand Down
4 changes: 2 additions & 2 deletions widget/gridwrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestGridWrap_Focus(t *testing.T) {
assert.NotNil(t, canvas.Focused())
assert.Equal(t, 0, canvas.Focused().(*GridWrap).currentFocus)

children := list.scroller.Content.(*fyne.Container).Layout.(*gridWrapLayout).children
children := list.scroller.Content.(*fyne.Container).Objects
assert.True(t, children[0].(*gridWrapItem).hovered)
assert.False(t, children[1].(*gridWrapItem).hovered)
assert.False(t, children[6].(*gridWrapItem).hovered)
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestGridWrap_RefreshItem(t *testing.T) {
data[2] = "Replace"
list.RefreshItem(2)

children := list.scroller.Content.(*fyne.Container).Layout.(*gridWrapLayout).children
children := list.scroller.Content.(*fyne.Container).Objects
assert.Equal(t, children[1].(*gridWrapItem).child.(*Label).Text, "Text")
assert.Equal(t, children[2].(*gridWrapItem).child.(*Label).Text, "Replace")
}
Expand Down

0 comments on commit 5032db3

Please sign in to comment.