From a0e4704975554f4a03f4dca8b01974ae8a90da04 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Fri, 10 Nov 2023 08:21:49 -0800 Subject: [PATCH] pull in unreleased optimization from fyne-io/fyne/pull/4375 --- internal/driver/glfw/loop.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/driver/glfw/loop.go b/internal/driver/glfw/loop.go index c20d6e9af8..d69eeb12b5 100644 --- a/internal/driver/glfw/loop.go +++ b/internal/driver/glfw/loop.go @@ -74,8 +74,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() @@ -96,6 +103,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() {