diff --git a/internal/driver/glfw/loop.go b/internal/driver/glfw/loop.go index a8ecc62a37..7d2fab3228 100644 --- a/internal/driver/glfw/loop.go +++ b/internal/driver/glfw/loop.go @@ -67,8 +67,15 @@ func runOnDraw(w *window, f func()) { <-done } +// Preallocate to avoid allocations on every drawSingleFrame. +// Note that the capacity of this slice can only grow, +// but its length will never be longer than the total number of +// window canvases that are dirty on a single frame. +// So its memory impact should be negligible and does not +// need periodic shrinking. +var refreshingCanvases []fyne.Canvas + func (d *gLDriver) drawSingleFrame() { - refreshingCanvases := make([]fyne.Canvas, 0) for _, win := range d.windowList() { w := win.(*window) w.viewLock.RLock() @@ -89,6 +96,12 @@ func (d *gLDriver) drawSingleFrame() { refreshingCanvases = append(refreshingCanvases, canvas) } cache.CleanCanvases(refreshingCanvases) + + // cleanup refreshingCanvases slice + for i := 0; i < len(refreshingCanvases); i++ { + refreshingCanvases[i] = nil + } + refreshingCanvases = refreshingCanvases[:0] } func (d *gLDriver) runGL() {