From 2405b7e825c63e02694c516c975d305b92a990b1 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 9 Oct 2023 01:14:09 +0900 Subject: [PATCH] internal/restorable: remove unnecessary copying After 6e5361c3284f3d642b46ccde76edca71f54ed8ac, WritePixels can expect that the given pixel byte slice is always new, and now can be assumed immutable. Do not copy the slice for restoring. Actually, these copying were introduced at 38ce46328af338a5fa8171f3a08360f5eab92e7c in order to reuse the same slice regions. --- internal/restorable/image.go | 12 ++---------- internal/restorable/images_test.go | 1 + 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 0c6eb40b5..89e7e19da 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -278,11 +278,7 @@ func (i *Image) WritePixels(pixels []byte, region image.Rectangle) { if region.Eq(image.Rect(0, 0, w, h)) { if pixels != nil { - // pixels can point to a shared region. - // This function is responsible to copy this. - copiedPixels := make([]byte, len(pixels)) - copy(copiedPixels, pixels) - i.basePixels.AddOrReplace(copiedPixels, image.Rect(0, 0, w, h)) + i.basePixels.AddOrReplace(pixels, image.Rect(0, 0, w, h)) } else { i.basePixels.Clear(image.Rect(0, 0, w, h)) } @@ -299,11 +295,7 @@ func (i *Image) WritePixels(pixels []byte, region image.Rectangle) { } if pixels != nil { - // pixels can point to a shared region. - // This function is responsible to copy this. - copiedPixels := make([]byte, len(pixels)) - copy(copiedPixels, pixels) - i.basePixels.AddOrReplace(copiedPixels, region) + i.basePixels.AddOrReplace(pixels, region) } else { i.basePixels.Clear(region) } diff --git a/internal/restorable/images_test.go b/internal/restorable/images_test.go index 8ecbbb3a1..9b1f456e3 100644 --- a/internal/restorable/images_test.go +++ b/internal/restorable/images_test.go @@ -461,6 +461,7 @@ func TestWritePixels(t *testing.T) { } img.WritePixels(pix, image.Rect(5, 7, 9, 11)) // Check the region (5, 7)-(9, 11). Outside state is indeterminate. + pix = make([]byte, 4*4*4) for i := range pix { pix[i] = 0 }