Reduce defer sentences for performance

This commit is contained in:
Hajime Hoshi 2017-05-27 21:35:38 +09:00
parent 0ab46b1780
commit a816c9c7fe
2 changed files with 8 additions and 3 deletions

View File

@ -49,13 +49,14 @@ func (q *commandQueue) appendVertices(vertices []float32) {
} }
func (q *commandQueue) EnqueueDrawImageCommand(dst, src *Image, vertices []float32, clr *affine.ColorM, mode opengl.CompositeMode) { func (q *commandQueue) EnqueueDrawImageCommand(dst, src *Image, vertices []float32, clr *affine.ColorM, mode opengl.CompositeMode) {
// Avoid defer for performance
q.m.Lock() q.m.Lock()
defer q.m.Unlock()
q.appendVertices(vertices) q.appendVertices(vertices)
if 0 < len(q.commands) { if 0 < len(q.commands) {
if c, ok := q.commands[len(q.commands)-1].(*drawImageCommand); ok { if c, ok := q.commands[len(q.commands)-1].(*drawImageCommand); ok {
if c.isMergeable(dst, src, clr, mode) { if c.isMergeable(dst, src, clr, mode) {
c.verticesNum += len(vertices) c.verticesNum += len(vertices)
q.m.Unlock()
return return
} }
} }
@ -68,12 +69,13 @@ func (q *commandQueue) EnqueueDrawImageCommand(dst, src *Image, vertices []float
mode: mode, mode: mode,
} }
q.commands = append(q.commands, c) q.commands = append(q.commands, c)
q.m.Unlock()
} }
func (q *commandQueue) Enqueue(command command) { func (q *commandQueue) Enqueue(command command) {
q.m.Lock() q.m.Lock()
defer q.m.Unlock()
q.commands = append(q.commands, command) q.commands = append(q.commands, command)
q.m.Unlock()
} }
// commandGroups separates q.commands into some groups. // commandGroups separates q.commands into some groups.

View File

@ -66,19 +66,22 @@ func (i *images) resolveStalePixels(context *opengl.Context) error {
} }
func (i *images) resetPixelsIfDependingOn(target *Image) { func (i *images) resetPixelsIfDependingOn(target *Image) {
// Avoid defer for performance
i.m.Lock() i.m.Lock()
defer i.m.Unlock()
if target == nil { if target == nil {
// disposed // disposed
i.m.Unlock()
return return
} }
if i.lastChecked == target { if i.lastChecked == target {
i.m.Unlock()
return return
} }
i.lastChecked = target i.lastChecked = target
for img := range i.images { for img := range i.images {
img.makeStaleIfDependingOn(target) img.makeStaleIfDependingOn(target)
} }
i.m.Unlock()
} }
func (i *images) restore(context *opengl.Context) error { func (i *images) restore(context *opengl.Context) error {