Skip to content

image: remove bounds check in "At" and "Set" methods, with re slice #14884

@pierrre

Description

@pierrre
  1. What version of Go are you using (go version)?
    1.6
  2. What operating system and processor architecture are you using (go env)?
    linux amd64

I just saw this https://go-review.googlesource.com/#/c/20654/

Could we rewrite

func (p *RGBA64) RGBA64At(x, y int) color.RGBA64 {
    if !(Point{x, y}.In(p.Rect)) {
        return color.RGBA64{}
    }
    i := p.PixOffset(x, y)
    return color.RGBA64{
        uint16(p.Pix[i+0])<<8 | uint16(p.Pix[i+1]),
        uint16(p.Pix[i+2])<<8 | uint16(p.Pix[i+3]),
        uint16(p.Pix[i+4])<<8 | uint16(p.Pix[i+5]),
        uint16(p.Pix[i+6])<<8 | uint16(p.Pix[i+7]),
    }
}

to

func (p *RGBA64) RGBA64At(x, y int) color.RGBA64 {
    if !(Point{x, y}.In(p.Rect)) {
        return color.RGBA64{}
    }
    i := p.PixOffset(x, y)
    s := p.Pix[i:i+8]
    return color.RGBA64{
        uint16(s[0])<<8 | uint16(s[1]),
        uint16(s[2])<<8 | uint16(s[3]),
        uint16(s[4])<<8 | uint16(s[5]),
        uint16(s[6])<<8 | uint16(s[7]),
    }
}

I did some benchmarks on my package https://github.com/pierrre/imageserver/tree/master/image/internal (that borrows code from image).
And I see interesting results.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions