Skip to content

Commit

Permalink
Don't allocate a slice of windows 60 times per second
Browse files Browse the repository at this point in the history
We were allocating and appending to a slice 60 times per second to modify the window list if windows were supposed to close. This updates the runloop to keep track of how many windows need to be closed and only allocate a slice when we need to.

Updates #2506
  • Loading branch information
Jacalz authored and andydotxyz committed Dec 18, 2023
1 parent 03e6d10 commit fa3e65c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
45 changes: 27 additions & 18 deletions internal/driver/glfw/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,15 @@ func (d *gLDriver) runGL() {
}
case <-eventTick.C:
d.tryPollEvents()
newWindows := []fyne.Window{}
reassign := false
windowsToRemove := 0
for _, win := range d.windowList() {
w := win.(*window)
if w.viewport == nil {
continue
}

if w.viewport.ShouldClose() {
reassign = true
w.viewLock.Lock()
w.visible = false
v := w.viewport
w.viewLock.Unlock()

// remove window from window list
v.Destroy()
w.destroy(d)
windowsToRemove++
continue
}

Expand All @@ -177,20 +168,38 @@ func (d *gLDriver) runGL() {
}
}

newWindows = append(newWindows, win)

if drawOnMainThread {
d.drawSingleFrame()
}
}
if reassign {
if windowsToRemove > 0 {
oldWindows := d.windowList()
newWindows := make([]fyne.Window, 0, len(oldWindows)-windowsToRemove)

for _, win := range oldWindows {
w := win.(*window)
if w.viewport == nil {
continue
}

if w.viewport.ShouldClose() {
w.viewLock.Lock()
w.visible = false
v := w.viewport
w.viewLock.Unlock()

// remove window from window list
v.Destroy()
w.destroy(d)
continue
}

newWindows = append(newWindows, win)
}

d.windowLock.Lock()
d.windows = newWindows
d.windowLock.Unlock()

if len(newWindows) == 0 {
d.Quit()
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/driver/glfw/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (w *window) destroy(d *gLDriver) {
w.DestroyEventQueue()
cache.CleanCanvas(w.canvas)

if w.master {
if w.master || len(w.driver.windowList()) == 1 {
d.Quit()
} else if runtime.GOOS == "darwin" {
go d.focusPreviousWindow()
Expand Down

0 comments on commit fa3e65c

Please sign in to comment.