[gold] diff.go: Optimize PixelDiff()'s same-size branch by setting each pixel in the output diff exactly once.
This results in a ~10% reduction in the time it takes to diff images of different sizes. See benchmarks "BenchmarkDiffDifferentSize" below.
Benchmarks before this change:
$ go test -bench=. -benchtime=10s
goos: linux
goarch: amd64
pkg: go.skia.org/infra/golden/go/diff
BenchmarkDiffIdentical-12 50000 302783 ns/op
BenchmarkDiffSameSize-12 1000 18130019 ns/op
BenchmarkDiffDifferentSize-12 500 38916445 ns/op
PASS
ok go.skia.org/infra/golden/go/diff 61.967s
Benchmarks after this change:
$ go test -bench=. -benchtime=10s
goos: linux
goarch: amd64
pkg: go.skia.org/infra/golden/go/diff
BenchmarkDiffIdentical-12 50000 296255 ns/op
BenchmarkDiffSameSize-12 1000 18129664 ns/op
BenchmarkDiffDifferentSize-12 500 34837620 ns/op
PASS
ok go.skia.org/infra/golden/go/diff 59.099s
Bug: skia:9287
Change-Id: Ic87f62f59a33da72702b82ac4d73aa44ea1740a6
Reviewed-on: https://skia-review.googlesource.com/c/buildbot/+/233464
Reviewed-by: Kevin Lubick <kjlubick@google.com>
Commit-Queue: Leandro Lovisolo <lovisolo@google.com>
diff --git a/golden/go/diff/diff.go b/golden/go/diff/diff.go
index 3289451..a4cbd73 100644
--- a/golden/go/diff/diff.go
+++ b/golden/go/diff/diff.go
@@ -341,20 +341,22 @@
}
}
} else {
- // Fill the entire image with maximum diff color.
+ // Set pixels outside of the comparison area with the with maximum diff color.
maxDiffColor := uint8ToColor(PixelDiffColor[deltaOffset(1024)])
- draw.Draw(resultImg, resultImg.Bounds(), &image.Uniform{maxDiffColor}, image.ZP, draw.Src)
+ for x := 0; x < resultWidth; x++ {
+ for y := 0; y < resultHeight; y++ {
+ if x < cmpWidth && y < cmpHeight {
+ color1 := img1.At(x, y)
+ color2 := img2.At(x, y)
- for x := 0; x < cmpWidth; x++ {
- for y := 0; y < cmpHeight; y++ {
- color1 := img1.At(x, y)
- color2 := img2.At(x, y)
-
- dc := diffColors(color1, color2, maxRGBADiffs)
- if dc == PixelMatchColor {
- numDiffPixels--
+ dc := diffColors(color1, color2, maxRGBADiffs)
+ if dc == PixelMatchColor {
+ numDiffPixels--
+ }
+ resultImg.Set(x, y, dc)
+ } else {
+ resultImg.Set(x, y, maxDiffColor)
}
- resultImg.Set(x, y, dc)
}
}
}