Skip to content

measured · · 10 min

Replacing Prawn: seven documents, one engine

Seven of a CRM's documents were drawn by Prawn inside the Rails process. Moving them to pdfblaze made 12 of 13 measured cases faster, up to 20.54×, and fixed Arabic. One case got slower, the memory budget turned out softer than its name, and the first production deploy failed on a health check. The numbers, the ceilings, and the rollout.

This is part 7 of the pdfblaze series. Quotes were the easy part: they were already HTML, printed by Chrome, and pdfblaze prints HTML. Seven other documents in the CRM were drawn by Prawn, a Ruby PDF library, with code, inside the Rails process that also served web requests and ran jobs.

All seven now go through pdfblaze 1.4.3. Twelve of the 13 cases I measured got faster, from 1.44× to 20.54×. The biggest win was a 1,000-message chat transcript: 16,468.4 ms before, 801.7 ms after. Arabic prints right. One case got slower, and the memory budget I set turned out to be an estimate rather than a cap. This post is the measurements, the ceilings, and a rollout whose first production deploy failed.

What Prawn did, and what it cost

The seven were: invoices, contracts, signed contracts, receipts, payouts, chat transcripts, and the plain quote that a quote with no template fell back to. Each was a Ruby class that placed text, lines and tables on a page by hand. That is how Prawn works, and for a long time it was fine.

Three things were not:

  • It ran inside the app. A PDF was drawn in the same Ruby process that answered web requests and ran jobs, so every big document grew that process.
  • It was slow on long documents. The 1,000-message transcript took 16.5 s.
  • It printed Arabic as "?" or with its letters reversed.

How much a big document grew the Rails process:

DocumentRails process growth
Invoice, 500 lines49.6 MB
Invoice, 1,000 lines52.4 MB
Payout, 1,000 entries62.3 MB
Contract, 150 clauses76.5 MB
Quote, 1,000 lines133.2 MB
Chat transcript, 1,000 messages389.1 MB

A long-running Ruby process seldom gives that memory back, and on a small server it is the same memory the web requests need.

The layouts also lived in Ruby code, a different system from the HTML quote templates. Two ways to draw a document is one too many.

How the new path works

Each document is now an HTML template that Rails renders, the same way it renders a page. pdfblaze prints it in a child process with two limits:

terminal
pdfblaze render invoice.html -o invoice.pdf --time-limit 20 --memory-budget 256

--time-limit 20 stops a document that takes longer than 20 seconds. --memory-budget 256 stops one whose estimated memory passes 256 MiB. Either way the document fails with a reason (limit.time or limit.memory) instead of taking the app down with it. The CLI docs describe both.

There is no fallback to Prawn. If pdfblaze cannot print a document, a web request is told to try again shortly and a background job retries. A fallback would have meant keeping two engines and two sets of layouts alive, and testing a path that almost never runs. Prawn and its table add-on are out of the Gemfile.

The benchmark

The setup: a laptop (M2 Pro), Ruby 3.4.13, 7 runs of each case, median. The laptop was busy, with a load average between 4.72 and 12.04 during the runs, so read differences of a few milliseconds as noise. "Old" is Prawn drawing the document inside a Rails process. "New" is pdfblaze 1.4.3 printing the same document in a process of its own, with the two limits above.

figure 1 · old engine vs pdfblaze, 13 documents
log scale · median of 7
  • old engine, in-process
  • pdfblaze 1.4.3, own process
  • slower than before
  • Invoice · 6 lines1.64×
  • Invoice · 100 lines4.19×
  • Invoice · 500 lines8.13×
  • Invoice · 1,000 lines8.46×
  • Contract · 150 clauses7.78×
  • Signed contract · 50 signers4.63×
  • Receipt · 4 lines1.44×
  • Receipt · 200 lines7.83×
  • Payout · 1,000 entries5.23×
  • Chat transcript · 1,000 messages20.54×
  • Chat transcript · 3,000 messages10.53×
  • Quote · 3 lines0.31×, slower
  • Quote · 1,000 lines9.18×

The 3-line quote went from 18.4 ms to 58.6 ms: drawing it inside the app had no process to start, and pdfblaze does. Control: a templated quote, pdfblaze in both builds, measured 35.8 / 42.7 ms at 3 lines and 78.3 / 83.1 ms at 200, so differences of a few milliseconds here are noise.

Each document has two bars: the old engine drawing it inside the Rails process, and pdfblaze 1.4.3 printing it in a process of its own. Median of 7 runs on a laptop (M2 Pro). The axis is a log scale, so a bar one tick shorter is ten times faster. The one case that got slower is marked.
DocumentSizeOldNewSpeed-up
Invoice6 lines67.8 ms41.3 ms1.64×
Invoice100 lines228.3 ms54.5 ms4.19×
Invoice500 lines1,102.8 ms135.6 ms8.13×
Invoice1,000 lines1,921.4 ms227.1 ms8.46×
Contract150 clauses1,060.4 ms136.3 ms7.78×
Signed contract50 signers944.9 ms204.1 ms4.63×
Receipt4 lines46.9 ms32.5 ms1.44×
Receipt200 lines428.3 ms54.7 ms7.83×
Payout1,000 entries2,500.5 ms478.3 ms5.23×
Chat transcript1,000 messages16,468.4 ms801.7 ms20.54×
Chat transcript3,000 messages19,178.1 ms1,821.1 ms10.53×
Quote3 lines18.4 ms58.6 ms0.31×, slower
Quote1,000 lines2,624.7 ms285.9 ms9.18×

There is a control row. A quote with a template was already on pdfblaze in both builds, so it should not change: 35.8 ms against 42.7 ms at 3 lines, 78.3 ms against 83.1 ms at 200. That is the size of the noise on this machine.

pdfblaze's own peak memory, where it was recorded, stayed low: 26.7 MB for a 6-line invoice, 58.5 MB for 1,000 lines, 74.6 MB for the 1,000-message transcript and 142.2 MB for 3,000 messages. That memory belongs to a child process that exits when the document is done, not to the app.

The one that got slower

The 3-line quote went from 18.4 ms to 58.6 ms, about three times slower.

The reason is the shape of the work, not the engine's layout speed. Prawn drew the quote inside a Ruby process that was already running, so a tiny document cost only the drawing. pdfblaze starts a process for every document. For a long document that start is lost in the work; for a 3-line quote it is most of the work. It is the only one of the 13 cases that Prawn drew in under 40 ms, and the only one that got slower.

Two ways to win it back exist, and I took neither yet:

  • A long-running worker. The engine already speaks a framed protocol to a worker that stays up between documents; a warm worker prints a one-page invoice in about 1.5 ms. But a pool of workers is shared state between requests, and part 6 is about how the documented pool handed one caller's PDF to the next. I would rather add that with care than in a hurry.
  • The C library, which skips the process start (the base engine measured that at about 5 ms).

40 ms more on a quote nobody prints in a loop is a cost I chose to keep for now.

How big can a document get?

Speed at normal sizes is half the story. The other half is what happens at absurd sizes: a payout with 15,000 entries, a transcript with 20,000 messages. I pushed each document up until it failed.

figure 2 · how big each document can get
  • pdfblaze 1.4.3
  • old engine
  • Invoicepdfblaze first fails at 17,500
  • Payoutpdfblaze first fails at 20,000
  • Transcriptpdfblaze first fails at 25,000
  • Quotepdfblaze first fails at 20,000
  • Contractpdfblaze: no failure recorded
  • Receiptpdfblaze: no failure recorded

The old engine printed a bigger invoice and payout than pdfblaze lets through. pdfblaze printed a transcript twice as long and a contract with 4,500 clauses where the old engine's benchmark went to 150.

The largest size of each document that printed, and how long it took. pdfblaze stops a document at its 256 MiB budget or 20 s limit, so its ceiling is a refusal with a reason; the old engine ran inside the Rails process with no stop, so its sizes are the largest it printed in the benchmark, however long that took. Both axes are log scales.
Documentpdfblaze, largest that passedpdfblaze, first failureOld engine
Invoice15,000 lines, 3.67 s, 415 MB17,50020,000 lines in 48.3 s
Payout15,000 entries, 5.06 s, 421 MB20,00030,000 entries in 90 s
Transcript20,000 messages, 11.4 s, 583 MB25,00010,000 messages in 47 s
Quote10,000 lines, 3.08 s20,00010,000 lines in 33.7 s
Contract4,500 clauses, 4.5 s, 274 MB—150 clauses in 1.49 s
Receipt10,000 lines, 1.63 s—21.5 s

pdfblaze fails first because it reaches its 256 MiB budget. That is the point of the budget: a 17,500-line invoice is refused with limit.memory, and the caller can say so. The old engine has no such stop. It printed a 30,000-entry payout, but it spent 90 s of a Rails process doing it, with nothing to stop it at 20 s or at any amount of memory.

So for invoices and payouts the old engine's ceiling is higher, and pdfblaze's is lower on purpose. If a real customer needs a 20,000-line invoice, the budget can go up for that document. I would rather raise a limit for one case than have no limit for any.

The budget is not a cap

Look at the peaks in that table again: 415 MB, 421 MB, 583 MB, all under --memory-budget 256. Failures went up to 694 MB.

The budget is an estimate. pdfblaze counts what the large parts of a document hold (the document tree, computed styles, layout fragments, decoded images, fonts) and stops the document when that estimate passes the budget. It does not count everything the process holds, and it does not cap the process. Measured today on 1.4.3, a document built to blow up stops at 0.42 s under a 64 MiB budget, with a real peak of 358 MiB. The stop works. The number on the flag is not the number in top.

The hard cap is a different flag:

terminal
pdfblaze render invoice.html -o invoice.pdf --time-limit 20 --memory-budget 256 \
  --memory-limit 1024

--memory-limit (Linux only) ends the process if its address space would pass the limit. The CRM does not set it yet. Adding --memory-limit 1024 is the planned follow-up, and until then the budget plus the 20 s limit is what keeps a big document from hurting anything else.

Staging, then production

The change merged at 08:47 UTC on 5 December. The staging deploy removed the Chrome container. At 09:07 UTC I printed the newest real record of each of the 8 document kinds on staging, the plain and the templated quote among them. Every one was a valid PDF, in 78–412 ms each, warm.

The first production deploy failed. The new container booted cleanly, but the deploy proxy checked its health only every 10 seconds, and it did not see the container healthy before the deploy gave up on it. The old version kept serving. Nothing about the failure was in pdfblaze or in the PDF code; it was a setting no one had needed to look at, because every earlier boot had happened to finish just before one of the checks.

The second attempt went live at 11:42 UTC. The Chrome container came out of production with it. A shorter interval is merged and runs on staging; production gets it with the next release.

Fork commit to production was under 20 hours. Most of it is in the timeline in part 1.

What's still open

  • No production numbers yet. Staging printed every kind in 78–412 ms. I have not measured latency or memory in production since it went live.
  • The hard memory cap is not set. The budget stops documents; real peaks reach 415–583 MB at the largest sizes. --memory-limit 1024 is the follow-up.
  • Copying Hindi or Arabic text out of a PDF gives the wrong characters. The page looks right; the text layer behind shaped letters is wrong.
  • No veraPDF run. pdfblaze can write PDF/A, but nothing in this benchmark was validated with veraPDF.
  • The 3-line quote is about three times slower, as above.

What I'd do differently

  • Benchmark on a quiet machine. A load average of up to 12.04 is the likely reason the control row moves by 7 ms. The big speed-ups are far outside that; the small ones are not.
  • Set --memory-limit with the budget, on the first day. I knew the budget was an estimate. I should have shipped the cap next to it rather than as a follow-up.
  • Read the deploy settings before the deploy that matters. A health check every 10 seconds gains nothing over a shorter one and costs a failed deploy when a boot runs long.
  • Write the ceilings into the product. The CRM should tell a user who builds a 17,500-line invoice why it will not print, before they try.

The next part, part 8, is how the binary the CRM runs is built, signed and checked.

Working through something like this? I help teams ship AI and cloud systems that hold up, and cost what they should.