From 7e7751bd43e900aa67c0684be191daf06dcc496e Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 4 Oct 2019 02:02:33 +0900 Subject: [PATCH] restorable: Performance tuning This is based on the result of examples/set (Wasm). --- internal/restorable/rect.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/internal/restorable/rect.go b/internal/restorable/rect.go index b13453a02..f8e924dcb 100644 --- a/internal/restorable/rect.go +++ b/internal/restorable/rect.go @@ -24,7 +24,8 @@ import ( type rectToPixels struct { m map[image.Rectangle][]byte - last image.Rectangle + lastr image.Rectangle + lastpix []byte } func (rtp *rectToPixels) addOrReplace(pixels []byte, x, y, width, height int) { @@ -44,6 +45,9 @@ func (rtp *rectToPixels) addOrReplace(pixels []byte, x, y, width, height int) { if r == newr { // Replace the region. rtp.m[r] = copied + if r == rtp.lastr { + rtp.lastpix = copied + } return } if r.Overlaps(newr) { @@ -53,6 +57,9 @@ func (rtp *rectToPixels) addOrReplace(pixels []byte, x, y, width, height int) { // Add the region. rtp.m[newr] = copied + if newr == rtp.lastr { + rtp.lastpix = copied + } } func (rtp *rectToPixels) remove(x, y, width, height int) { @@ -74,15 +81,19 @@ func (rtp *rectToPixels) at(i, j int) (byte, byte, byte, byte, bool) { return 0, 0, 0, 0, false } + var pix []byte + var r *image.Rectangle - pt := image.Pt(i, j) - if pt.In(rtp.last) { - r = &rtp.last + if pt := image.Pt(i, j); pt.In(rtp.lastr) { + r = &rtp.lastr + pix = rtp.lastpix } else { for rr := range rtp.m { if pt.In(rr) { r = &rr - rtp.last = rr + rtp.lastr = rr + pix = rtp.m[rr] + rtp.lastpix = pix break } } @@ -92,7 +103,6 @@ func (rtp *rectToPixels) at(i, j int) (byte, byte, byte, byte, bool) { return 0, 0, 0, 0, false } - pix := rtp.m[*r] idx := 4 * ((j-r.Min.Y)*r.Dx() + (i - r.Min.X)) return pix[idx], pix[idx+1], pix[idx+2], pix[idx+3], true }