internal/restorable: remove unnecessary copying

After 6e5361c328, 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 38ce46328a
in order to reuse the same slice regions.
This commit is contained in:
Hajime Hoshi 2023-10-09 01:14:09 +09:00
parent 6e5361c328
commit 2405b7e825
2 changed files with 3 additions and 10 deletions

View File

@ -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)
}

View File

@ -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
}