Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid allocating a new slice on every call to drawSingleFrame #4375

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 []fyne.Canvas

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