From 56a17a7f79c480efb6d029359eb421d75f24c966 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 5 Aug 2017 20:19:08 +0900 Subject: [PATCH] loop: Reduce defer for performance and readability --- internal/loop/run.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/internal/loop/run.go b/internal/loop/run.go index 2adcc479f..fcc5f928e 100644 --- a/internal/loop/run.go +++ b/internal/loop/run.go @@ -47,26 +47,28 @@ var ( func (c *runContext) startRunning() { c.m.Lock() - defer c.m.Unlock() c.running = true + c.m.Unlock() } func (c *runContext) isRunning() bool { c.m.RLock() - defer c.m.RUnlock() - return c.running + v := c.running + c.m.RUnlock() + return v } func (c *runContext) endRunning() { c.m.Lock() - defer c.m.Unlock() c.running = false + c.m.Unlock() } func (c *runContext) getCurrentFPS() float64 { c.m.RLock() - defer c.m.RUnlock() - if !c.running { + v := c.running + c.m.RUnlock() + if !v { // TODO: Should panic here? return 0 } @@ -75,8 +77,8 @@ func (c *runContext) getCurrentFPS() float64 { func (c *runContext) updateFPS(fps float64) { c.m.Lock() - defer c.m.Unlock() c.currentFPS = fps + c.m.Unlock() } type GraphicsContext interface {