Skip to content

perf: DrawImage copies an *image.RGBA's samples instead of calling At - #46

Merged
tannevaled merged 1 commit into
mainfrom
perf/drawimage-rgba
Oct 18, 2026
Merged

tannevaled merged 1 commit into
mainfrom
perf/drawimage-rgba

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

What

Page.DrawImage read every pixel through image.Image.At, which boxes each colour in an interface: 921,600 allocations for a single 1280×720 page. An *image.RGBA already stores exactly the alpha-premultiplied 8-bit samples that At(...).RGBA() returns, so its rows are now copied directly. Any other image type keeps the generic path.

Output

Byte-identical. TestSamplesRGBAFastPath compares 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 to io.Discard, Apple M4 Max, 5 iterations each.

path time/op bytes/op allocs/op
generic (At) 72.8 ms 11.1 MB 921,772
*image.RGBA 29.4 ms 7.4 MB 171

Most 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

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>
@tannevaled
tannevaled merged commit ec52df6 into main Oct 18, 2026
1 check passed
@tannevaled
tannevaled deleted the perf/drawimage-rgba branch October 18, 2026 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant