From 66c4031ee97289f26922126111e0510398fb43fd Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 27 Feb 2015 21:43:53 +1100 Subject: [PATCH] image/draw: optimize drawFillSrc. benchmark old ns/op new ns/op delta BenchmarkFillSrc 46781 46000 -1.67% Change-Id: I0ab25d42d5763f1a0fe5a67ee00b83f0aa55f1f6 Reviewed-on: https://go-review.googlesource.com/6235 Reviewed-by: Rob Pike --- src/image/draw/draw.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/image/draw/draw.go b/src/image/draw/draw.go index 7f9127168e..6496341144 100644 --- a/src/image/draw/draw.go +++ b/src/image/draw/draw.go @@ -245,16 +245,20 @@ func drawFillOver(dst *image.RGBA, r image.Rectangle, src *image.Uniform) { func drawFillSrc(dst *image.RGBA, r image.Rectangle, src *image.Uniform) { sr, sg, sb, sa := src.RGBA() + sr8 := uint8(sr >> 8) + sg8 := uint8(sg >> 8) + sb8 := uint8(sb >> 8) + sa8 := uint8(sa >> 8) // The built-in copy function is faster than a straightforward for loop to fill the destination with // the color, but copy requires a slice source. We therefore use a for loop to fill the first row, and // then use the first row as the slice source for the remaining rows. i0 := dst.PixOffset(r.Min.X, r.Min.Y) i1 := i0 + r.Dx()*4 for i := i0; i < i1; i += 4 { - dst.Pix[i+0] = uint8(sr >> 8) - dst.Pix[i+1] = uint8(sg >> 8) - dst.Pix[i+2] = uint8(sb >> 8) - dst.Pix[i+3] = uint8(sa >> 8) + dst.Pix[i+0] = sr8 + dst.Pix[i+1] = sg8 + dst.Pix[i+2] = sb8 + dst.Pix[i+3] = sa8 } firstRow := dst.Pix[i0:i1] for y := r.Min.Y + 1; y < r.Max.Y; y++ { -- 2.48.1