Skip to content

Commit

Permalink
image/draw: optimize drawFillSrc.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
nigeltao committed Feb 28, 2015
1 parent 344f424 commit 66c4031
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/image/draw/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down

0 comments on commit 66c4031

Please sign in to comment.