widgetPool - switch from map to slice
This commit is contained in:
+12
-12
@@ -13,23 +13,26 @@ const (
|
|||||||
WidgetTypeArtistPageHeader
|
WidgetTypeArtistPageHeader
|
||||||
WidgetTypePlaylistPageHeader
|
WidgetTypePlaylistPageHeader
|
||||||
WidgetTypeTracklist
|
WidgetTypeTracklist
|
||||||
|
|
||||||
|
// keep at bottom
|
||||||
|
numWidgetTypes
|
||||||
)
|
)
|
||||||
|
|
||||||
// A pool to share commonly-used widgets across pages to reduce
|
// A pool to share commonly-used widgets across pages to reduce
|
||||||
// creation of new widgets and memory allocations.
|
// creation of new widgets and memory allocations.
|
||||||
// It is not thread-safe, which is fine for its current use.
|
// It is not thread-safe, which is fine for its current use.
|
||||||
type WidgetPool struct {
|
type WidgetPool struct {
|
||||||
cache map[WidgetType][]cachedWidget
|
pool [][]pooledWidget
|
||||||
}
|
}
|
||||||
|
|
||||||
type cachedWidget struct {
|
type pooledWidget struct {
|
||||||
widget fyne.CanvasObject
|
widget fyne.CanvasObject
|
||||||
releasedAt int64 // unixMillis
|
releasedAt int64 // unixMillis
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWidgetPool() WidgetPool {
|
func NewWidgetPool() WidgetPool {
|
||||||
return WidgetPool{
|
return WidgetPool{
|
||||||
cache: make(map[WidgetType][]cachedWidget),
|
pool: make([][]pooledWidget, numWidgetTypes),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,11 +40,11 @@ func NewWidgetPool() WidgetPool {
|
|||||||
// Returns nil if there is no available widget.
|
// Returns nil if there is no available widget.
|
||||||
func (w *WidgetPool) Obtain(typ WidgetType) fyne.CanvasObject {
|
func (w *WidgetPool) Obtain(typ WidgetType) fyne.CanvasObject {
|
||||||
var widget fyne.CanvasObject
|
var widget fyne.CanvasObject
|
||||||
if ws, ok := w.cache[typ]; ok && len(ws) > 0 {
|
if l := len(w.pool[typ]); l > 0 {
|
||||||
i := len(ws) - 1
|
i := l - 1
|
||||||
widget = ws[i].widget
|
widget = w.pool[typ][i].widget
|
||||||
ws[i].widget = nil
|
w.pool[typ][i].widget = nil
|
||||||
w.cache[typ] = ws[:i]
|
w.pool[typ] = w.pool[typ][:i]
|
||||||
}
|
}
|
||||||
return widget
|
return widget
|
||||||
}
|
}
|
||||||
@@ -50,10 +53,7 @@ func (w *WidgetPool) Obtain(typ WidgetType) fyne.CanvasObject {
|
|||||||
// The widget must not be modified by the releaser after release,
|
// The widget must not be modified by the releaser after release,
|
||||||
// since it may be Obtained for a new use at any time.
|
// since it may be Obtained for a new use at any time.
|
||||||
func (w *WidgetPool) Release(typ WidgetType, wid fyne.CanvasObject) {
|
func (w *WidgetPool) Release(typ WidgetType, wid fyne.CanvasObject) {
|
||||||
if _, ok := w.cache[typ]; !ok {
|
w.pool[typ] = append(w.pool[typ], pooledWidget{
|
||||||
w.cache[typ] = make([]cachedWidget, 0)
|
|
||||||
}
|
|
||||||
w.cache[typ] = append(w.cache[typ], cachedWidget{
|
|
||||||
widget: wid,
|
widget: wid,
|
||||||
releasedAt: time.Now().UnixMilli(),
|
releasedAt: time.Now().UnixMilli(),
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user