[Tex/LaTex] Why is LuaLaTeX so slow and is there anything I can do about it

linuxluatexperformance

A while back I started bumping into issues with LuaLaTex and switched to compiling a project in XeLaTeX. Since then it seems most of my issues have been resolved upstream or are fixable one way on another. It's been suggested that I switch back to avoid some XeLaTeX issues, but speed is one reason that would keep me from doing so. I've gotten almost all the kinks worked out now so my documents compile in either engine, but LuaLaTeX takes over twice as long to do so.

My documents are not long, but they are complicated (example PDF output) involving many rendering passes to fit everything inside of a hard page limit, then doing it all again for other page layouts and content options. A few seconds here and there isn't that big an issue until it adds up to waiting 5 minutes for your documents to be ready for printing instead of 2. I did some testing and it appears this isn't unique to my complicated project. For example this MWE I was using to test ligatures, hyphenation and small-cap font features compiles in 1.29 seconds using XeLaTeX but takes 3.96 seconds in LuaLaTeX!

\documentclass{minimal}
\usepackage{polyglossia}
\usepackage{testhyphens}
\usepackage{libertine}
\setmainlanguage{turkish}
\begin{document}
Uzun---tire. {\scshape Uzun---tire.}
\begin{checkhyphens}{}
İstanbullularınki
İstanbul’lularınki
\end{checkhyphens}
İzmir {\scshape İzmir}
\end{document}

Is this just the way the engine is or am I doing something wrong? If the performance isn't normally so dramatically different, what sort of things might I be looking for to get it setup correctly?

Edit: Based on comments and answers so far it looks like a lot of potentially related issues may be platform specific. I'm particularly interested in problems manifest on and solutions relevant for Linux, although any generally applicable pitfalls or optimizations are welcome.

Best Answer

Running some tests and looking at the output of the process monitor I found that on windows with miktex the "feature" of fontspec to load default settings from a file with the ending .fontspec is slowing down the font search considerably as miktex is looking everywhere for the files. Disabling the feature meant for me that this document needed only around 2.5 seconds instead of 12. In texlive the time gain was 3 seconds instead of 4.6.

\documentclass{article}
\usepackage{fontspec}
\ExplSyntaxOn
\cs_set:Nn \__fontspec_load_external_fontoptions:Nn
 {}
\ExplSyntaxOff
\usepackage{libertine}


\begin{document}
abc
\end{document}

Edit

In the case of miktex there is imho a bug in the file search: unlike pdflatex or xelatex lualatex doesn't rely on the FNDB but starts a real file search in the texmf tree. I made a bug report: https://sourceforge.net/p/miktex/bugs/2356/. In the case of texlive I still have to test if I can improve the compilation time by forcing texlive to use only the ls-R-files.

Edit 2

One can improve the compilation time in texlive by creating more ls-R-files (at least on my system with quite a lot of local roots ...), but the effect is not so dramatic as the large texmf-trees like texmf-dist already uses only the ls-R.

Edit 3

Miktex has added a patch to luatex. With todays update it no longers does a slow disk search if a file is not found. The compilation time of a document which loads e.g. libertine dropped from 12 to 3 seconds on my PC ;-).

Related Question