Skip to content

Stop allocating a coverage grid for every mark made through a clip - #79

Merged
tannevaled merged 1 commit into
mainfrom
mask-scratch
Sep 26, 2026
Merged

tannevaled merged 1 commit into
mainfrom
mask-scratch

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

mask() copied its input into a fresh []float64 on every call. It has to
copy — the rasteriser hands back its own scratch and a tiling pattern reads that
again for the next tile — but it does not have to allocate.

Reuse is safe although paint() is re-entrant, because the buffer's whole life is
inside one paint: mask fills it, Composite reads it, record reads it, and
nothing between those runs a content stream. Argued, then checked: the
corpus's worst page is a 2.4 KB file whose Type 3 glyph fills with a pattern that
shows the same glyph — exactly the re-entrant case — and it renders
byte-identically.

What this buys is memory, not time

page before after
the Type 3 cycle (2.4 KB!) 2267.61 MB 1378.90 MB −39%
cerfa_12496.pdf 547.86 MB 525.19 MB −4%
a uk-govuk form 69.04 MB 63.32 MB −8%
b31820797.pdf 1192.60 MB 1187.95 MB −0.4%

mask alone accounted for 907.79 MB on the first of those.

Time: no measurable change. 1266 ms against 1339 ms on the worst page, min of
four, either side of a noise floor of about 1%. GOGC=off does not make that page
faster either — so its 1.3 s is not the collector, and removing the garbage was
never going to be the fix. Where that 1.3 s actually goes is still open.

All 3 215 corpus documents render byte-identical. Coverage stays at 100.0%.

🤖 Generated with Claude Code

mask() copied its input into a fresh []float64 on every call. It has to copy --
the rasteriser hands back its own scratch and a tiling pattern reads that again
for the next tile -- but it does not have to allocate. The buffer is now kept on
the renderer.

Reuse is safe although paint() is re-entrant, because the buffer's whole life is
inside one paint: mask fills it, Composite reads it, record reads it, and nothing
between those runs a content stream. Argued, and then checked: the corpus's worst
page is a 2.4 KB file whose Type 3 glyph fills with a pattern that shows the same
glyph, which is exactly the re-entrant case, and it renders byte-identically.

WHAT THIS BUYS IS MEMORY, NOT TIME, and the measurement says so plainly:

  that page          2267.61 MB -> 1378.90 MB   -39%   (mask alone was 907.79 MB)
  cerfa_12496.pdf     547.86 MB ->  525.19 MB    -4%
  a uk-govuk form      69.04 MB ->   63.32 MB    -8%
  b31820797.pdf      1192.60 MB -> 1187.95 MB    -0.4%

Time: no measurable change. 1266 ms against 1339 ms on the worst page, min of
four, either side of a noise floor of about 1%. GOGC=off does not make that page
faster either, so its 1.3 s is not the collector and removing the garbage was
never going to be the fix.

All 3 215 corpus documents render byte-identical. Coverage stays at 100.0%.
@tannevaled
tannevaled merged commit 437da1d into main Sep 26, 2026
9 checks passed
@tannevaled
tannevaled deleted the mask-scratch branch September 26, 2026 17:55
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