Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image/jpeg: encoding an *image.YCbCr is sub-optimal #18487

Closed
tbonfort opened this issue Jan 1, 2017 · 2 comments
Closed

image/jpeg: encoding an *image.YCbCr is sub-optimal #18487

tbonfort opened this issue Jan 1, 2017 · 2 comments
Milestone

Comments

@tbonfort
Copy link
Contributor

tbonfort commented Jan 1, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

master

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/tbonfort/dev/go"
GORACE=""
GOROOT="/usr/lib/go-1.6"
GOTOOLDIR="/usr/lib/go-1.6/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

What did you do?

func BenchmarkEncodeYCbCr(b *testing.B) {
	b.StopTimer()
	img := image.NewYCbCr(image.Rect(0, 0, 640, 480), image.YCbCrSubsampleRatio420)
	bo := img.Bounds()
	rnd := rand.New(rand.NewSource(123))
	for y := bo.Min.Y; y < bo.Max.Y; y++ {
		for x := bo.Min.X; x < bo.Max.X; x++ {
			cy := img.YOffset(x, y)
			ci := img.COffset(x, y)
			img.Y[cy] = uint8(rnd.Intn(256))
			img.Cb[ci] = uint8(rnd.Intn(256))
			img.Cr[ci] = uint8(rnd.Intn(256))
		}
	}
	b.SetBytes(640 * 480 * 4)
	b.StartTimer()
	options := &Options{Quality: 90}
	for i := 0; i < b.N; i++ {
		Encode(ioutil.Discard, img, options)
	}
}

What did you expect to see?

jpeg.Encode() of *image.YCbCr should be faster than for *image.RGBA

What did you see instead?

This is slow as jpeg.Encode() uses the generic image.At() which involves unnecessary ycbr->rgba->ycbcr conversions, and more importantly image.color allocations

@gopherbot
Copy link

CL https://golang.org/cl/34773 mentions this issue.

@tbonfort
Copy link
Contributor Author

tbonfort commented Jan 1, 2017

quick benchmark:

benchmark                  old ns/op     new ns/op     delta
BenchmarkEncodeYCbCr-4     43990846      24201148      -44.99%

benchmark                  old MB/s     new MB/s     speedup
BenchmarkEncodeYCbCr-4     20.95        38.08        1.82x

@josharian josharian changed the title jpeg encoding an *image.YCbCr is sub-optimal image/jpeg: encoding an *image.YCbCr is sub-optimal Jan 1, 2017
@rsc rsc added this to the Go1.9 milestone Jan 4, 2017
@golang golang locked and limited conversation to collaborators Feb 1, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants