Repository navigation
perf: DrawImage copies an *image.RGBA's samples instead of calling At - #46
Merged
Merged
Conversation
DrawImage read every pixel through image.Image's At, which boxes each color in an interface: 921,600 allocations for one 1280x720 page. An *image.RGBA already stores the alpha-premultiplied 8-bit samples At's RGBA method returns, so its rows are now copied directly; any other image keeps the generic path. The output is byte-identical (a test compares the two paths on a transparent sub-image with a non-zero origin, an opaque image and an empty one). BenchmarkDrawImage, one 1280x720 page written to io.Discard, Apple M4 Max: generic 72.8 ms/op 11.1 MB/op 921,772 allocs/op rgba 29.4 ms/op 7.4 MB/op 171 allocs/op Found by go-slides' benchmark against marp-cli, where writing the PDF of a 100-slide deck spent most of its time here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Page.DrawImageread every pixel throughimage.Image.At, which boxes each colour in an interface: 921,600 allocations for a single 1280×720 page. An*image.RGBAalready stores exactly the alpha-premultiplied 8-bit samples thatAt(...).RGBA()returns, so its rows are now copied directly. Any other image type keeps the generic path.Output
Byte-identical.
TestSamplesRGBAFastPathcompares the two paths on a transparent sub-image (non-zero origin, stride wider than the bounds), an opaque image and an empty one.Measured
BenchmarkDrawImage: one 1280×720 page written toio.Discard, Apple M4 Max, 5 iterations each.At)*image.RGBAMost of the time left is zlib compression.
Why now
This came out of go-slides' benchmark against marp-cli: writing the PDF of a 100-slide deck spent most of its time here.
🤖 Generated with Claude Code