Skip to content

Loading system fonts takes extremely long #8561

Open

Description

@noakodaren

Description

Launching the compiler takes around 50s on my system (for example, echo a > test.typ; time /path/to/latest/typst compile test.typ). Using strace, I can see that during this time, typst opens a lot of font files:

...
17:19:25.688702 open("/usr/share/fonts/TTF/IosevkaTermNerdFontPropo-MediumItalic.ttf", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 5 <0.000098>
17:19:25.688918 fcntl(5, F_SETFD, FD_CLOEXEC) = 0 <0.000031>
17:19:25.689018 fstat(5, {st_mode=S_IFREG|0644, st_size=13644960, ...}) = 0 <0.000044>
17:19:25.689151 mmap(NULL, 13644960, PROT_READ, MAP_SHARED, 5, 0) = 0x7fb2c1000000 <0.000043>
17:19:25.727664 munmap(0x7fb2c1000000, 13644960) = 0 <0.000194>
17:19:25.727973 close(5)                = 0 <0.000029>
17:19:25.728156 open("/usr/share/fonts/TTF/IosevkaTermNerdFontPropo-MediumOblique.ttf", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 5 <0.000115>
17:19:25.728467 fcntl(5, F_SETFD, FD_CLOEXEC) = 0 <0.000086>
17:19:25.728656 fstat(5, {st_mode=S_IFREG|0644, st_size=13560360, ...}) = 0 <0.000036>
17:19:25.728771 mmap(NULL, 13560360, PROT_READ, MAP_SHARED, 5, 0) = 0x7fb2c1000000 <0.000044>
17:19:25.768636 munmap(0x7fb2c1000000, 13560360) = 0 <0.000095>
17:19:25.768825 close(5)                = 0 <0.000042>
...

Using --ignore-system-fonts fixes this. So you know, I have a lot of fonts installed. Here are all installed packages with ttf in the name:

ttf-0xproto-nerd
ttf-3270-nerd
ttf-agave-nerd
ttf-anonymouspro-nerd
ttf-arimo-nerd
ttf-bigblueterminal-nerd
ttf-bitstream-vera-mono-nerd
ttf-cascadia-code-nerd
ttf-cascadia-mono-nerd
ttf-cousine-nerd
ttf-d2coding-nerd
ttf-daddytime-mono-nerd
ttf-dejavu
ttf-dejavu-nerd
ttf-envycoder-nerd
ttf-fantasque-nerd
ttf-firacode-nerd
ttf-go-nerd
ttf-gohu-nerd
ttf-hack-nerd
ttf-heavydata-nerd
ttf-iawriter-nerd
ttf-ibmplex-mono-nerd
ttf-inconsolata-go-nerd
ttf-inconsolata-lgc-nerd
ttf-inconsolata-nerd
ttf-intone-nerd
ttf-iosevka-nerd
ttf-iosevkaterm-nerd
ttf-iosevkatermslab-nerd
ttf-jetbrains-mono-nerd
ttf-lekton-nerd
ttf-liberation
ttf-liberation-mono-nerd
ttf-lilex-nerd
ttf-martian-mono-nerd
ttf-meslo-nerd
ttf-monofur-nerd
ttf-monoid-nerd
ttf-mononoki-nerd
ttf-mplus-nerd
ttf-nerd-fonts-symbols
ttf-nerd-fonts-symbols-common
ttf-nerd-fonts-symbols-mono
ttf-noto-nerd
ttf-opensans
ttf-profont-nerd
ttf-proggyclean-nerd
ttf-recursive-nerd
ttf-roboto-mono-nerd
ttf-sharetech-mono-nerd
ttf-sourcecodepro-nerd
ttf-space-mono-nerd
ttf-terminus-nerd
ttf-tinos-nerd
ttf-ubuntu-mono-nerd
ttf-ubuntu-nerd
ttf-victor-mono-nerd
ttf-zed-mono-nerd

I could give you the output of fc-list, but that is extremely long.

I was surprised to not find any discussion of this problem online, so it's possible that I have done something wrong. If not, my suggestion to fix this is to use https://www.freedesktop.org/wiki/Software/fontconfig/, which to my understanding is made among other things to fix this problem.

Operating system

Linux

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    on Jun 24, 2026
  2. added
    cli
    and removed
    bug
    on Jun 24, 2026
  3. cady-b commented on Jun 24, 2026

    @cady-b
    Contributor

    Fontconfig is a c library and not cross-platform, so that wouldn't solve the issue; there's been a bit of discussion about this in #100, which lead to our current fontdb dependency.

    With everything we need to support (variable font data, extensive metadata for human-readable matching & family bundling, cross-platform & security implications), this unfortunately is not a trivial problem. It has come up a few times though, and generally we'd prefer not doing excessive work of course.


    On Windows, we'd have to interface with DirectWrite (probably IDWriteFactory::GetSystemFontCollection), on macOS with Core Text (i'd guess CTFontCollectionCreateFromAvailableFonts), and on Linux we'd probably need a pure-rust reader of the fontconfig cache. Doing so from rust in a way that doesn't blow up binary size seems like a challenge.

    I've looked a bit into it again today, and servo/font-kit seems to be the most realistic contender. Someone with experience would probably have to a) talk to the team to discuss if concerns of the web app & vision for Typst's future have to be considered, and b) spend some time evaluating it or alternatives (i.e. Fontique has been mentioned by Enivex during our discussion on the Discord, we might also consider managing our own cache though that comes with user interface concerns) for our use case and write up a report detailing where further efforts should be directed. I don't see this happening in the near term however with everything else that's going on unless a contributor decides to take this up.


    As a workaround, you could consider creating a more minimal font directory (ideally on a fast drive and without tons of nerd-fonts, which have a heavy focus on UI and are a much larger than the base fonts; 50s sounds very, very bad even for a long list), and to set the TYPST_IGNORE_SYSTEM_FONTS & TYPST_FONT_PATHS environment variables accordingly (check typst info for more information).

  4. noakodaren commented on Jun 24, 2026

    @noakodaren
    Author

    I know that I can configure my system better, but I'm opening this issue mostly so people can be prevented from thinking that typst is slow (I use --ignore-system-fonts as a workaround). The reason I managed to figure out what was happening and add the --ignore-system-fonts is that I knew about strace, otherwise I would probably still be waiting every time starting typst.

    Assuming there are more people than me installing too many fonts, it would be good to document the issue so people know how to reduce compile times. For example, a good start would be to make typst warn if reading fonts takes a long time, and suggest using --ignore-system-fonts.

    And if it wasn't clear, the reason I am suggesting fontconfig is because of the cache that is often built by linux distributions (or at least arch).

  5. cady-b commented on Jun 24, 2026

    @cady-b
    Contributor

    Having an issue for this now is probably good yes, and I want to thank you for the detailed report. I also want to encourage you and others reading this to reach out to the community upon running into issues though; forfeiting to them or strace skills are not required 😅

    Regarding a hint; I'm not sure what we could recommend here. Depending on the system configuration, more or less time may be expected for this task, explaining the issue in user-friendly terms seems difficult in the space we have, and there also isn't really a universal solution (users may be on different operating systems, may still wish to use all or some of their fonts, may or may not be able to change parts of their system beyond Typst, etc.) So I don't really know what it should say. I guess linking here is an option, but that'll also just be walls of text and might lead to people seeking support here which wouldn't be the right place for that :/ Do you have any ideas what we could do for users?

    No worries on the fontconfig thing, yes that'll be the appropriate component to look into for Linux! I was just explaining further considerations and why it's not enough to resolve this immediately.

  6. added
    text
    on Jun 29, 2026
  7. IgKh commented on Jul 3, 2026

    @IgKh
    SponsorContributor

    Over a year ago I noticed as well that the time it takes to build a FontBook from system fonts is rather long. Although in my case (with a more "usual" set of fonts), it took only around 300-400 msec with an optimized build.

    Profiling it, it was clear that typst_library::text::font::info::FontInfo::from_ttf was dominating the call graph, specifically the part about constructing the Coverage structure for each font face. The implementation there was and still is very naïve, and consists of repeatedly allocating and sorting very large vectors (which is a wasteful, as the codepoints already come out "mostly sorted").

    Given that the team focus at the time and the fact while noticeable it was not something that couldn't be managed (as it is one time cost per establishing a World that can be done in the background) I did not pursue further. It would be interesting if OP could run a typst fonts call of a debug build of the CLI under a profiler to see if it is the same cause but more pronounced or something else.

    Regarding font-config - as noted its' relevance is that it manages and keeps updated a system-wide binary cache that can accelerate font queries (including fallbacks). It is likely not sufficient for Typst's needs - even beyond it being platform specific - but nothing prevents the Typst CLI from mainlining it's own binary serialization of a complete FontBook.

  8. noakodaren commented on Jul 4, 2026

    @noakodaren
    Author

    I also want to encourage you and others reading this to reach out to the community upon running into issues though; forfeiting to them or strace skills are not required 😅

    Good idea, I would probably have done so eventually...

    Regarding a hint; I'm not sure what we could recommend here. Depending on the system configuration, more or less time may be expected for this task, explaining the issue in user-friendly terms seems difficult in the space we have, and there also isn't really a universal solution (users may be on different operating systems, may still wish to use all or some of their fonts, may or may not be able to change parts of their system beyond Typst, etc.) So I don't really know what it should say. I guess linking here is an option, but that'll also just be walls of text and might lead to people seeking support here which wouldn't be the right place for that :/ Do you have any ideas what we could do for users?

    I believe the best thing to do in general is to document issues properly. This would fit in a troubleshooting-page, and of course where font behavior is documented. Maybe then a diagnostic could link there?

    Profiling it, it was clear that typst_library::text::font::info::FontInfo::from_ttf was dominating the call graph, specifically the part about constructing the Coverage structure for each font face. The implementation there was and still is very naïve, and consists of repeatedly allocating and sorting very large vectors (which is a wasteful, as the codepoints already come out "mostly sorted").

    Given that the team focus at the time and the fact while noticeable it was not something that couldn't be managed (as it is one time cost per establishing a World that can be done in the background) I did not pursue further. It would be interesting if OP could run a typst fonts call of a debug build of the CLI under a profiler to see if it is the same cause but more pronounced or something else.

    Here is the output of RUSTFLAGS='-Clink-arg=-Wl,--no-rosegment' cargo flamegraph --bin=typst --dev --profile=dev-fast -- fonts: https://github.com/user-attachments/assets/6fd73edb-cf8b-4484-836f-fd95c0fcb5fe (Note that it "only" took ~27s this time (probably because I have more empty space on my ssd now)). The cause seems to be the same, but even with an optimal implementation it will take some time, as just reading the files takes ~10s (time fc-list | sed -e 's/^\([^:]*\):.*$/\1/' | xargs cat >/dev/null).

  9. IgKh commented on Jul 4, 2026

    @IgKh
    SponsorContributor

    Thanks!

    The cause seems to be the same, but even with an optimal implementation it will take some time, as just reading the files takes ~10s (time fc-list | sed -e 's/^([^:]):.$/\1/' | xargs cat >/dev/null).

    It is not quite equivalent to what happens in typst-cli, since the fontdb crate memory-maps the font files and the ttf-parser crate parses the font's tables lazily, so in practice only a relatively small part of the font is actually read from disk. But I guess that's beside the point.

  10. changed the title [-]Loading system fonts takes extremely long.[/-] [+]Loading system fonts takes extremely long[/+] on Jul 10, 2026
  11. added
    perf
    on Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cliperftext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions