buffered: Avoid creating func objects whenever possible

Fixes #1220
This commit is contained in:
Hajime Hoshi 2020-06-29 21:45:57 +09:00
parent dea3785750
commit 09322dfdc8
2 changed files with 60 additions and 48 deletions

View File

@ -64,16 +64,14 @@ func getDelayedFuncsAndClearSlow() []func() error {
return nil return nil
} }
func tryAddDelayedCommand(f func(obj interface{}) error, ondelayed func() interface{}) bool { // maybeCanAddDelayedCommand returns false if the delayed commands cannot be added.
if atomic.LoadUint32(&delayedCommandsFlushed) == 0 { // Otherwise, maybeCanAddDelayedCommand's returning value is not determined.
// Outline the slow-path to expect the fast-path is inlined. // For example, maybeCanAddDelayedCommand can return true even when flusing is being processed.
return tryAddDelayedCommandSlow(f, ondelayed) func maybeCanAddDelayedCommand() bool {
} return atomic.LoadUint32(&delayedCommandsFlushed) == 0
return false
} }
func tryAddDelayedCommandSlow(f func(obj interface{}) error, ondelayed func() interface{}) bool { func tryAddDelayedCommand(f func(obj interface{}) error, ondelayed func() interface{}) bool {
delayedCommandsM.Lock() delayedCommandsM.Lock()
defer delayedCommandsM.Unlock() defer delayedCommandsM.Unlock()

View File

@ -55,11 +55,13 @@ func NewImage(width, height int, volatile bool) *Image {
} }
func (i *Image) initialize(width, height int, volatile bool) { func (i *Image) initialize(width, height int, volatile bool) {
if tryAddDelayedCommand(func(obj interface{}) error { if maybeCanAddDelayedCommand() {
i.initialize(width, height, volatile) if tryAddDelayedCommand(func(obj interface{}) error {
return nil i.initialize(width, height, volatile)
}, nil) { return nil
return }, nil) {
return
}
} }
i.img = mipmap.New(width, height, volatile) i.img = mipmap.New(width, height, volatile)
i.width = width i.width = width
@ -73,11 +75,13 @@ func NewScreenFramebufferImage(width, height int) *Image {
} }
func (i *Image) initializeAsScreenFramebuffer(width, height int) { func (i *Image) initializeAsScreenFramebuffer(width, height int) {
if tryAddDelayedCommand(func(obj interface{}) error { if maybeCanAddDelayedCommand() {
i.initializeAsScreenFramebuffer(width, height) if tryAddDelayedCommand(func(obj interface{}) error {
return nil i.initializeAsScreenFramebuffer(width, height)
}, nil) { return nil
return }, nil) {
return
}
} }
i.img = mipmap.NewScreenFramebufferMipmap(width, height) i.img = mipmap.NewScreenFramebufferMipmap(width, height)
@ -114,11 +118,13 @@ func (i *Image) resolvePendingFill() {
} }
func (i *Image) MarkDisposed() { func (i *Image) MarkDisposed() {
if tryAddDelayedCommand(func(obj interface{}) error { if maybeCanAddDelayedCommand() {
i.MarkDisposed() if tryAddDelayedCommand(func(obj interface{}) error {
return nil i.MarkDisposed()
}, nil) { return nil
return }, nil) {
return
}
} }
i.invalidatePendingPixels() i.invalidatePendingPixels()
i.img.MarkDisposed() i.img.MarkDisposed()
@ -165,11 +171,13 @@ func (i *Image) Dump(name string, blackbg bool) error {
} }
func (i *Image) Fill(clr color.RGBA) { func (i *Image) Fill(clr color.RGBA) {
if tryAddDelayedCommand(func(obj interface{}) error { if maybeCanAddDelayedCommand() {
i.Fill(clr) if tryAddDelayedCommand(func(obj interface{}) error {
return nil i.Fill(clr)
}, nil) { return nil
return }, nil) {
return
}
} }
// Defer filling the image so that successive fillings will be merged into one (#1134). // Defer filling the image so that successive fillings will be merged into one (#1134).
@ -183,15 +191,17 @@ func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) error {
panic(fmt.Sprintf("buffered: len(pix) was %d but must be %d", len(pix), l)) panic(fmt.Sprintf("buffered: len(pix) was %d but must be %d", len(pix), l))
} }
if tryAddDelayedCommand(func(copied interface{}) error { if maybeCanAddDelayedCommand() {
i.ReplacePixels(copied.([]byte), x, y, width, height) if tryAddDelayedCommand(func(copied interface{}) error {
return nil i.ReplacePixels(copied.([]byte), x, y, width, height)
}, func() interface{} { return nil
copied := make([]byte, len(pix)) }, func() interface{} {
copy(copied, pix) copied := make([]byte, len(pix))
return copied copy(copied, pix)
}) { return copied
return nil }) {
return nil
}
} }
if x == 0 && y == 0 && width == i.width && height == i.height { if x == 0 && y == 0 && width == i.width && height == i.height {
@ -238,11 +248,13 @@ func (i *Image) DrawImage(src *Image, bounds image.Rectangle, a, b, c, d, tx, ty
Ty: ty, Ty: ty,
} }
if tryAddDelayedCommand(func(obj interface{}) error { if maybeCanAddDelayedCommand() {
i.drawImage(src, bounds, g, colorm, mode, filter) if tryAddDelayedCommand(func(obj interface{}) error {
return nil i.drawImage(src, bounds, g, colorm, mode, filter)
}, nil) { return nil
return }, nil) {
return
}
} }
i.drawImage(src, bounds, g, colorm, mode, filter) i.drawImage(src, bounds, g, colorm, mode, filter)
@ -275,12 +287,14 @@ func (i *Image) DrawTriangles(src *Image, vertices []float32, indices []uint16,
} }
} }
if tryAddDelayedCommand(func(obj interface{}) error { if maybeCanAddDelayedCommand() {
// Arguments are not copied. Copying is the caller's responsibility. if tryAddDelayedCommand(func(obj interface{}) error {
i.DrawTriangles(src, vertices, indices, colorm, mode, filter, address, shader, uniforms) // Arguments are not copied. Copying is the caller's responsibility.
return nil i.DrawTriangles(src, vertices, indices, colorm, mode, filter, address, shader, uniforms)
}, nil) { return nil
return }, nil) {
return
}
} }
for _, src := range srcs { for _, src := range srcs {