Zinewright

Blog

Compiling Is the Cheap Part: Where the Time Goes When Typst Renders a Magazine

By Yuluo Han ·

Zinewright turns a manuscript into a page-through publication: Typst sets the pages, and each page becomes an image for an online reader.

Before measuring, I assumed typesetting would be the expensive step. It's the clever part, after all. It turned out to be the cheapest step by a very long way.

The pipeline

Worker → Queue → Durable Object + container
                  ├─ typst compile main.typ issue.pdf
                  ├─ pdftoppm, one process per page
                  ├─ Pillow → WebP (quality 84)
                  └─ upload pages, PDF and manifest to R2

The Worker handles routing and the public reader. D1 holds metadata. R2 holds the output. Each generation is one queue message, and a Durable Object runs it in a container with 2 vCPUs and 6 GiB of memory, with a maximum of three instances at a time.

The container exists because Typst, Poppler and a large font set don't belong in a Worker. Typst is pinned, as are the fonts, because any change to either can move line breaks in a published issue.

Typst is absurdly fast

Measured on an arm64 Mac with Typst 0.15.1, median of three runs. The long manuscript is a synthetic one: 30 chapters, about 195,000 Chinese characters.

Job Time
Compiling a 14-page sample to PDF 0.19 s
Compiling a 280-page manuscript to PDF 0.43 s
3 preview pages straight to PNG at 96 ppi 0.18–0.20 s

That's under half a second to set a long book. By contrast, a similar-length manuscript of about 198,000 characters took 79 seconds end to end on the same Mac, using two render threads. That manuscript isn't identical to the 280-page one, so don't read this as an exact ratio. The size of the gap is the point: nearly all of the run is rasterising and encoding.

Two decisions came straight out of those numbers.

Live preview became possible. A real preview in the style editor had to come in under two seconds, or it wouldn't be built. The preview compiles a cut-down document and asks Typst for PNGs directly, skipping Poppler altogether:

typst compile --pages 1,5,6 --format png --ppi 96 main.typ 'page-{0p}.png'

About 0.2 seconds locally. The container has larger fonts and slower cores, so I still need to measure it there, but the margin is wide.

Page images became the thing to optimise. An early version produced two sizes per page, 1000px and 2000px. Encoding was the slowest stage and the high-resolution pages took up most of the output, so we went to a single 1600px WebP per page. The manifest format keeps both of its keys, pointing at the same files, and the reader's zoom steps changed to suit.

Parallel by page

Pages are independent, so rasterising is easy to parallelise once it's arranged properly. Each page is its own pdftoppm subprocess writing a temporary PPM, which Pillow encodes and deletes:

def render_page(index):
    target = dest / "render" / f"page-{index:04d}"
    run(["pdftoppm", "-f", index, "-l", index, "-singlefile",
         "-scale-to-x", str(width), "-scale-to-y", "-1", pdf, target])
    raster = target.with_suffix(".ppm")
    with Image.open(raster) as image:
        save_page(image, index, dest, ratios[index - 1], width)
    raster.unlink()

Two small choices there:

Python threads are fine here, because the work happens in a subprocess and in Pillow's encoder, which releases the GIL.

Before this change, the benchmark told a clear story: 2 vCPUs ran no faster than 1, because the loop was serial. With 1 vCPU and 3 GiB, a 234-page PDF took 148 seconds. Memory never went above about 251 MiB, and CPU was the only constraint. It's worth checking, since sizing a container by memory would have been wasted money.

The bug that made everything look fast

Then, reading on my iPhone, I noticed that the phone edition looked blurrier than the original pages.

The line in question used to look like this:

run(["pdftoppm", "-f", index, "-l", index, "-singlefile", "-scale-to", str(width), pdf, target])

-scale-to N doesn't set the width. It scales the longer side to N. For a phone page of 390×844pt, asking for 1080 produced an image 499 pixels wide and 1080 tall. The next step in the pipeline then enlarged that to the target width with a good resampling filter. So the result was the right size, looked plausible, and had 2.16 times too few pixels across.

Every automated check passed. Dimensions were correct, and the images were valid and pleasantly small. It was also why the pipeline looked so fast.

The fix is to fix the width and let the height follow:

"-scale-to-x", str(width), "-scale-to-y", "-1"

Doing the real work had a real cost:

Mac, 2 render threads Before After
Desktop, 243 pages 56.8 s / 76.9 MB 79.0 s / 81.1 MB
Phone, 285 pages 43.9 s / 75.7 MB 88.0 s / 91.4 MB

The phone edition went from 0.77 times the desktop time to 1.11 times. On the real container the ratio was lower, 220 s against 228 s, or about 0.96. Earlier cost estimates, based on the numbers from before the fix, had to be thrown out, along with a target of keeping the phone edition under 0.7 times the desktop time.

There's a regression test now: a page must arrive at the save step already at the target width. Enlarging it is treated as a failure.

What I'd do differently

Measure before designing. The preview benchmark didn't just clear its two-second gate. It changed the design, by showing that the fast path should skip Poppler entirely.

Test the property you care about. "The output is 1080 pixels wide" was true and useless. "No page is enlarged on its way to the output" is what I actually needed.

Look at the output on the device. No test noticed the blur. Holding a phone did.

Be suspicious of good numbers. A phone edition with more pages than desktop, finishing in 77% of the time, should have made me ask why.

If you're building something similar: Typst's speed is real, and it changes what you can offer interactively. Budget your time for what happens after the PDF.

The result of all this is in the sample issue. This post explains why there's a second edition for phones in the first place.