How can I figure out why my typst document is slow to compile?

What is the best way to track down what is slowing down compilation in a Typst project? Are there any packages or built-in approaches to identify where optimization needs to be done?

I’m creating 200 versions of an assignment about phylogenetic trees (project linked here, with assignment answers obscured). Each version includes a unique tree made using a fletcher diagram. To make unique versions, I’m using suiji to randomly sample subsets of an array of ~40 species. Each subset is used to make one assignment version. However, in the web app, generating 100 versions takes over 1.5 mins, and attempting to generate 200 versions did not give me any result after 5 mins. I have several ideas of where this slow-down might be occurring, but trial-and-error testing for slowdown points takes a very long time, obviously.

How can I optimize compile time?

You might want to try running typst locally instead of through the web app.

It took 6s for me to generate 100 assignments on my laptop and 13s for 200.

  1. Go to Releases · typst/typst · GitHub to download a typst binary
  2. Download your project from the web app as a zip file and unzip it
  3. In a command line, run [path-to-typst-binary] compile main.typ
3 Likes

Thanks for your reply. Generating 200 assignments via the CLI was much slower for me, over 5.5 mins. Maybe the difference between us comes down to hardware.

Regardless, I am hoping to more broadly be able to find what is slowing down compilation for a given document. Is that something that exists for Typst?

typst c --help | grep -A 6 -- --timings

      --timings [<OUTPUT_JSON>]
          Produces performance timings of the compilation process.
          (experimental)
          
          The resulting JSON file can be loaded into a tracing tool such as
          https://ui.perfetto.dev. It does not contain any sensitive information
          apart from file names and line numbers.

typst c --help | grep -A 6 -- --timings

Have you even tried using that? The timings output is not helpful at all without tooling. For the current document it generates a 2.8GB json file.

Without what tooling? The docs say how to use it. However, the bigger the document, the longer it takes to save the timings, and the bigger they get. You can try it on some small portion of the document and increase it, if needed.

If the difference is that large, I would suspect incremental compilation makes the difference. typst compile always starts from scratch (typst watch doesn’t, and the web app’s document compilation cycle seems to work similarly to typst watch).

1 Like

Thanks all, I reduced the number of assignments to 10 and ran the following:

typst c main.typ --timings --jobs 1

I obviously have a lot more to learn about how Typst works before I can understand the output well, but at least I’m pretty certain that fletcher and cetz are the biggest slowdowns here.

if your figures won’t change, or don’t change for every generation, then you can always pre-generate them from a separate document first, and use the output in your documents.

1 Like

You might also want to try batching your document. For example, you can set the seed from the command line by changing

#let rng_seed = 42
#let version_count = 200

to

#let rng_seed = int(sys.inputs.seed)
#let version_count = 20

and compiling with (if using bash)

for i in {1..10};
   do echo $i;
   typst compile main.typ --input seed=$i batch-$i.pdf;
done

This doesn’t actually speed things up, but at least you can see the progress.

1 Like