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/draw: drawPaletted hot path function optimization #22375
Comments
Looks like the spec guarantees good behaviour for unsigned ints:
|
Seems like there are two occurences, too:
|
It's acceptable as a CL, but the optimized func sqDiff would need a comment to explain correctness wrt overflow, and some tests, especially for inputs near 0 (both positive and negative) and near -0x80000000. If that func sqDiff is copy/pasted twice, just comment and test one of them (probably color/color.go being canonical) and add a shorter comment in the other pointing to the first one. |
Change https://golang.org/cl/72373 mentions this issue: |
Current implementation of
drawPaletted()
function has the following calls tosqDiff()
function in its hot path:go/src/image/draw/draw.go
Line 628 in a31e0a4
Number of executions of this line for each
drawPaletted()
call is betweenwidth×height
andwidth×height×palette size
.Here's how
sqDiff()
currently implemented:go/src/image/draw/draw.go
Lines 562 to 574 in a31e0a4
It can be reduced to:
This relies on overflows but produces the same result, see https://play.golang.org/p/6q3Cvqk1k7
While the change itself is rather miniscule, the net effect of it being in the hot path of the
drawPaletted()
is noticeable in benchmarks, i.e.QuantizedEncode
benchmark from theimage/gif
package shows significant improvements after such change applied:Please let me know if this is something that can be accepted as a CL.
The text was updated successfully, but these errors were encountered: