Skip to content

Carry a rectangular clip as a rectangle instead of rasterising it - #66

Merged
tannevaled merged 1 commit into
mainfrom
rect-clip
Sep 25, 2026
Merged

tannevaled merged 1 commit into
mainfrom
rect-clip

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

91% of the clips a PDF sets are re W n — 57 367 of 63 038 over two corpora of 3215 documents — and every one of the 192 on the page where this renderer was furthest behind poppler.

Each was rasterised into a coverage grid: an edge list, a sweep, and w*h float32 to store a shape four numbers describe.

The interpreter already knows

A path built by one re and nothing else, under a transform that does not rotate it, is an axis-aligned rectangle before it is built. So the clip keeps the rectangle and never asks the rasteriser.

the page it was found on draw
v0.40.0 165 ms
this 12 ms — 14×

pdftoppm draws that page in 70 ms for the whole process. It was our worst document at 2.33× behind; it is not any more.

Bit for bit, not approximately

What go-gfx/gfx v0.34.0's vector.Rect answers is what the rasteriser would have put in that grid — through float32 and back, because a link that stores a grid stores float32.

3215 documents rendered before and after and compared as PNG bytes: 3215 byte-identical, 0 different.

A correction

Rect.Whole — the pixels a rectangle covers entirely, answered with two comparisons and no memory touched — was built because the rectangle form measured 13% slower on a scanned page. That measurement was taken while a 3215-document sweep was running, and does not reproduce on a quiet machine:

v0.40.0 rectangle rectangle + Whole
irrigationrequir1340fort.pdf 578–588 ms 576–584 ms 579–580 ms

The optimisation is sound and well tested; its reason was not. Saying so here rather than letting the commit message stand as the record.

What the coverage gate asked for

The one case in which a rectangle link answers zero, and it is real: the rasteriser samples four sub-scanlines a row, so a rectangle thinner than a quarter of a pixel can fall between two of them — a box with nothing in it. TestARectangleThinnerThanASubScanline pins it, and checks the rectangle against the rasteriser pixel for pixel on the same shape.

🤖 Generated with Claude Code

91% of the clips a PDF sets are `re W n` -- 57367 of 63038 over two
corpora of 3215 documents -- and every one of the 192 on the page where
this renderer was furthest behind poppler. Each was rasterised into a
coverage grid: an edge list, a sweep, and w*h float32 to store a shape
four numbers describe.

The interpreter already knows. A path built by one `re` and nothing else,
under a transform that does not rotate it, is an axis-aligned rectangle
before it is built, so the clip keeps the rectangle and never asks the
rasteriser.

    the page it was found on   165ms -> 12ms draw   14x

What go-gfx/gfx v0.34.0's vector.Rect answers is what the rasteriser
would have put in that grid, BIT FOR BIT, which is what makes this a
substitution rather than an approximation -- through float32 and back,
because a link that stores a grid stores float32.

Rect.Whole gives the pixels it covers entirely, which answers most of
them with two comparisons and no memory touched at all. It was built
because the rectangle form measured 13% slower on a scanned page; that
measurement was taken while a 3215-document sweep was running, and does
not reproduce on a quiet machine. The optimisation is sound and its
reason was not.

3215 documents rendered before and after and compared as PNG bytes: 3215
byte-identical, 0 different.

The coverage gate asked for the one case in which a rectangle link
answers zero, and it is a real one: the rasteriser samples four
sub-scanlines a row, so a rectangle thinner than a quarter of a pixel can
fall BETWEEN two of them -- a box with nothing in it.
@tannevaled
tannevaled merged commit 165e725 into main Sep 25, 2026
1 check passed
@tannevaled
tannevaled deleted the rect-clip branch September 25, 2026 17:22
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