]> Cypherpunks repositories - gostls13.git/commitdiff
image/draw: remove some bounds checks from DrawYCbCr
authorJosh Bleecher Snyder <josharian@gmail.com>
Sun, 17 Apr 2016 01:36:07 +0000 (18:36 -0700)
committerAlexandru Moșoi <alexandru@mosoi.ro>
Sun, 17 Apr 2016 06:25:28 +0000 (06:25 +0000)
It’d be nicer to write just

_ = dpix[x+3]

but the compiler isn’t able to reason about offsets
from symbols (yet).

image/draw benchmark:

YCbCr-8   722µs ± 3%   682µs ± 3%  -5.54%  (p=0.000 n=50+50)

Change-Id: Ia1e399496ed87c282bf0f9ca56c0b2d4948a0df9
Reviewed-on: https://go-review.googlesource.com/22146
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/image/internal/imageutil/gen.go
src/image/internal/imageutil/impl.go

index b9158d0ce96bd0e09aad071ab452c6f0e70468fb..6792b28a45b5b1e2c65d9335118d3bb2a446d1dd 100644 (file)
@@ -133,10 +133,13 @@ const sratioCase = `
                                        b = ^(b >> 31)
                                }
 
-                               dpix[x+0] = uint8(r)
-                               dpix[x+1] = uint8(g)
-                               dpix[x+2] = uint8(b)
-                               dpix[x+3] = 255
+
+                               // use a temp slice to hint to the compiler that a single bounds check suffices
+                               rgba := dpix[x : x+4 : len(dpix)]
+                               rgba[0] = uint8(r)
+                               rgba[1] = uint8(g)
+                               rgba[2] = uint8(b)
+                               rgba[3] = 255
                        }
                }
 `
index 39b455fdfa41687d63333f5779954024f7f906cc..3696b08e4196793d6a7c8eed35db678a6ff7da2a 100644 (file)
@@ -82,10 +82,12 @@ func DrawYCbCr(dst *image.RGBA, r image.Rectangle, src *image.YCbCr, sp image.Po
                                        b = ^(b >> 31)
                                }
 
-                               dpix[x+0] = uint8(r)
-                               dpix[x+1] = uint8(g)
-                               dpix[x+2] = uint8(b)
-                               dpix[x+3] = 255
+                               // use a temp slice to hint to the compiler that a single bounds check suffices
+                               rgba := dpix[x : x+4 : len(dpix)]
+                               rgba[0] = uint8(r)
+                               rgba[1] = uint8(g)
+                               rgba[2] = uint8(b)
+                               rgba[3] = 255
                        }
                }
 
@@ -137,10 +139,12 @@ func DrawYCbCr(dst *image.RGBA, r image.Rectangle, src *image.YCbCr, sp image.Po
                                        b = ^(b >> 31)
                                }
 
-                               dpix[x+0] = uint8(r)
-                               dpix[x+1] = uint8(g)
-                               dpix[x+2] = uint8(b)
-                               dpix[x+3] = 255
+                               // use a temp slice to hint to the compiler that a single bounds check suffices
+                               rgba := dpix[x : x+4 : len(dpix)]
+                               rgba[0] = uint8(r)
+                               rgba[1] = uint8(g)
+                               rgba[2] = uint8(b)
+                               rgba[3] = 255
                        }
                }
 
@@ -192,10 +196,12 @@ func DrawYCbCr(dst *image.RGBA, r image.Rectangle, src *image.YCbCr, sp image.Po
                                        b = ^(b >> 31)
                                }
 
-                               dpix[x+0] = uint8(r)
-                               dpix[x+1] = uint8(g)
-                               dpix[x+2] = uint8(b)
-                               dpix[x+3] = 255
+                               // use a temp slice to hint to the compiler that a single bounds check suffices
+                               rgba := dpix[x : x+4 : len(dpix)]
+                               rgba[0] = uint8(r)
+                               rgba[1] = uint8(g)
+                               rgba[2] = uint8(b)
+                               rgba[3] = 255
                        }
                }
 
@@ -246,10 +252,12 @@ func DrawYCbCr(dst *image.RGBA, r image.Rectangle, src *image.YCbCr, sp image.Po
                                        b = ^(b >> 31)
                                }
 
-                               dpix[x+0] = uint8(r)
-                               dpix[x+1] = uint8(g)
-                               dpix[x+2] = uint8(b)
-                               dpix[x+3] = 255
+                               // use a temp slice to hint to the compiler that a single bounds check suffices
+                               rgba := dpix[x : x+4 : len(dpix)]
+                               rgba[0] = uint8(r)
+                               rgba[1] = uint8(g)
+                               rgba[2] = uint8(b)
+                               rgba[3] = 255
                        }
                }