Skip to content

Commit

Permalink
avoid allocation on every drawSingleFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Nov 8, 2023
1 parent 0635d3e commit 60474a4
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion internal/driver/glfw/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = make([]fyne.Canvas, 0)

func (d *gLDriver) drawSingleFrame() {
refreshingCanvases := make([]fyne.Canvas, 0)
for _, win := range d.windowList() {
w := win.(*window)
w.viewLock.RLock()
Expand All @@ -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() {
Expand Down

0 comments on commit 60474a4

Please sign in to comment.