Skip to content

Multiply the clips at the pixels that are painted, not at every pixel they cover - #62

Merged
tannevaled merged 2 commits into
mainfrom
lazy-clip
Sep 25, 2026
Merged

tannevaled merged 2 commits into
mainfrom
lazy-clip

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

A page composes far more pixels into its clips than it ever paints through them.

pixels composed into clips pixels masked while painting ratio
the form this was found on 32 148 788 226 639 141.9×
both corpora, 3215 documents 8 433 010 263 293 645 790 28.7×

narrow was multiplying the clip already in force into the new one at every pixel of the new clip's box, which is 34% of the time this renderer spends drawing — second only to the rasteriser's inner loop. Almost all of that product is never read: it is computed for pixels no mark ever reaches.

So the product is left to the pixels that are painted

A clip now holds its own shape only, and a pointer to the clip it narrowed. at walks the chain and multiplies. The chain's box is the intersection of every shape in it, so one test still rejects a pixel no link needs to be asked about — and because a chain box only ever shrinks, a pixel inside it is inside every link's grid and no link needs a bounds test of its own.

With a bound, because a page can say W n without end

A page that narrows itself repeatedly without a Q between would otherwise make every painted pixel walk the lot. Past clipDepth = 8 links, narrow composes the chain into one grid: composing costs the box once, walking costs the chain at every painted pixel.

Measured

The draw phase, timed in-process. A process-per-document harness cannot measure this on an Apple Silicon machine — a short-lived process is placed on an efficiency or a performance core by the scheduler, and that difference is larger than the effect. The harness reads +0.2% when run against itself.

before after
the form it was found on, draw 98.9 ms 85.1 ms (−14%)
37 documents, total draw 1198.1 ms 1195.8 ms (−0.2%)

The aggregate is inside the floor, and that is the honest reading: one document in 37 clips heavily enough for this to matter. Beside go-gfx/gfx#49, which is independent and already released as v0.32.0, the same form goes 96.23 ms → 70.72 ms, −26.5% — the two compose.

And it draws the same picture

3215 documents of both corpora rendered before and after and compared as PNG bytes — not a metric over them, the bytes: 3215 byte-identical, 0 different. Run twice: once for the change, and again after a dead branch was removed from at.

One dead branch, found by the coverage gate

at checked each link for a nil grid. Every link has one — narrow gives one to each and flatten to what it composes — and the only clip without one is the empty clip, whose box the bounds test has already answered for. The check could not fire, and the gate is what said so.

And the brake had to be tested by hand

No page in either corpus nests clips past clipDepth, so nothing in the corpus pulls it. A safety brake nothing exercises is where the next defect lives, so TestALongChainIsComposedRatherThanWalked narrows eleven times and checks both that the chain is composed and that it still answers the same thing.

🤖 Generated with Claude Code

… they cover

A page composes far more pixels into its clips than it ever paints
through them:

                              composed      painted    ratio
  the form this was found on  32 148 788     226 639   141.9x
  both corpora, 3215 docs     8 433 010 263  293 645 790  28.7x

`narrow` was multiplying the clip already in force into the new one at
every pixel of the new clip's box -- 34% of the time this renderer spends
drawing, second only to the rasteriser's inner loop. Almost all of that
product is never read: it is computed for pixels no mark reaches.

So a clip now holds its own shape and a pointer to the clip it narrowed,
and `at` walks the chain. The chain's box is the intersection of every
shape in it, so one test still rejects a pixel no link needs to be asked
about; and because a chain box only ever shrinks, a pixel inside it is
inside every link's grid and no link needs a bounds test of its own.

## With a bound, because a page can say `W n` without end

Past clipDepth = 8 links, narrow composes the chain into one grid.
Composing costs the box once; walking costs the chain at every painted
pixel. No page in either corpus nests clips that deep, which is exactly
why the test that pulls that brake had to be written by hand: a safety
brake nothing exercises is where the next defect lives.

## Measured, against a harness that was calibrated first

The draw phase timed in-process: a process-per-document harness cannot
measure this on this machine, where a short-lived process is placed on an
efficiency or a performance core by the scheduler and that difference is
larger than the effect. Run against itself the harness reads +0.2%.

  the form it was found on        98.9ms -> 85.1ms    -14%
  37 documents, total draw      1198.1 -> 1195.8ms    -0.2%

The aggregate is within the floor: one document in 37 clips heavily
enough for this to matter. Beside the rasteriser change in go-gfx/gfx#49,
which is independent, the same form goes 96.23ms -> 70.72ms, -26.5%.

And it draws the same picture: 3215 documents rendered before and after
and compared as PNG bytes, 3215 byte-identical.

## One dead branch, found by the gate

`at` checked each link for a nil grid. Every link has one -- narrow gives
one to each and flatten to what it composes -- and the only clip without
one is the empty clip, whose box the bounds test has already answered
for. The check could not fire, and the coverage gate is what said so.
go-gfx/gfx v0.32.0. The two are independent and compose: the form this
branch was measured on goes 96.23ms to 70.72ms, -26.5%, where each alone
is -14% and -12%.
@tannevaled
tannevaled merged commit 28233e2 into main Sep 25, 2026
1 check passed
@tannevaled
tannevaled deleted the lazy-clip branch September 25, 2026 10:13
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