Skip to content

Commit

Permalink
Remove constant debug field from canvas
Browse files Browse the repository at this point in the history
Thisclears out a small amount of memory usageby moving a build-constant field to a global constant variable. Most importantly, it makes Go have a better time deadcode eliminating methods that we only want to compile in for debug (especially the reflect code that gets the name of the canvasobject).
  • Loading branch information
Jacalz committed Nov 18, 2023
1 parent 802f92b commit 294322b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
8 changes: 8 additions & 0 deletions internal/debug_disabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !debug
// +build !debug

package internal

// BuildTypeIsDebug is true if built with -tags debug. This value exists as a constant
// so the Go compiler can deadcode eliminate reflect from non-debug builds.
const BuildTypeIsDebug = false
8 changes: 8 additions & 0 deletions internal/debug_enabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build debug
// +build debug

package internal

// BuildTypeIsDebug is true if built with -tags debug. This value exists as a constant
// so the Go compiler can deadcode eliminate reflect from non-debug builds.
const BuildTypeIsDebug = true
11 changes: 5 additions & 6 deletions internal/driver/glfw/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ var _ fyne.Canvas = (*glCanvas)(nil)
type glCanvas struct {
common.Canvas

content fyne.CanvasObject
menu fyne.CanvasObject
padded, debug bool
size fyne.Size
content fyne.CanvasObject
menu fyne.CanvasObject
padded bool
size fyne.Size

onTypedRune func(rune)
onTypedKey func(*fyne.KeyEvent)
Expand Down Expand Up @@ -298,7 +298,7 @@ func (c *glCanvas) paint(size fyne.Size) {
}
}

if c.debug {
if internal.BuildTypeIsDebug {
c.DrawDebugOverlay(node.Obj(), pos, size)
}
}
Expand Down Expand Up @@ -339,6 +339,5 @@ func newCanvas() *glCanvas {
c := &glCanvas{scale: 1.0, texScale: 1.0, padded: true}
c.Initialize(c, c.overlayChanged)
c.setContent(&canvas.Rectangle{FillColor: theme.BackgroundColor()})
c.debug = fyne.CurrentApp().Settings().BuildType() == fyne.BuildDebug
return c
}
5 changes: 2 additions & 3 deletions internal/driver/mobile/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type mobileCanvas struct {
scale float32
size fyne.Size

touched map[int]mobile.Touchable
padded, debug bool
touched map[int]mobile.Touchable
padded bool

onTypedRune func(rune)
onTypedKey func(event *fyne.KeyEvent)
Expand All @@ -50,7 +50,6 @@ type mobileCanvas struct {
// NewCanvas creates a new gomobile mobileCanvas. This is a mobileCanvas that will render on a mobile device using OpenGL.
func NewCanvas() fyne.Canvas {
ret := &mobileCanvas{padded: true}
ret.debug = fyne.CurrentApp().Settings().BuildType() == fyne.BuildDebug
ret.scale = fyne.CurrentDevice().SystemScaleForWindow(nil) // we don't need a window parameter on mobile
ret.touched = make(map[int]mobile.Touchable)
ret.lastTapDownPos = make(map[int]fyne.Position)
Expand Down
2 changes: 1 addition & 1 deletion internal/driver/mobile/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (d *mobileDriver) paintWindow(window fyne.Window, size fyne.Size) {
}
}

if c.debug {
if internal.BuildTypeIsDebug {
c.DrawDebugOverlay(node.Obj(), pos, size)
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/painter/gl/gl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"
"runtime"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/internal"
)

const floatSize = 4
Expand All @@ -17,7 +17,7 @@ const max16bit = float32(255 * 255)
// Receives a function as parameter, to lazily get the error code only when
// needed, avoiding unneeded overhead.
func logGLError(getError func() uint32) {
if fyne.CurrentApp().Settings().BuildType() != fyne.BuildDebug {
if !internal.BuildTypeIsDebug {
return
}

Expand Down

0 comments on commit 294322b

Please sign in to comment.