Commit 66c4031e authored by Nigel Tao's avatar Nigel Tao

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/6235Reviewed-by: 's avatarRob Pike <r@golang.org>
parent 344f4240
......@@ -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++ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment