# How to speed up LuaLaTeX startup with fontspec?

As a novice user of lualatex and fontspec I've found it very slow (>1.3 seconds) to load on my Linux system:

% This is file compact.tex
\documentclass[a4paper]{article}
% This is needed only for old (non-fontspec) fonts.
%\usepackage[utf8]{luainputenc}
\usepackage{fontspec}
\newfontfamily\myverdana{Verdana.ttf}[]
% 1.38s user time up to this point.
% \makeatletter\@@end
\begin{document}
\hrule
{\Huge Helló, Wörld, árvíztűrő}
\hrule
{\Huge Helló, Wörld, árvízt\H{u}r\H{o}}
\hrule
{\myverdana\Huge Helló, Wörld, árvíztűrő}
\hrule
\end{document}
% 1.44s user time up to this point.


My time measurements:

• pdflatex (without fontspec): 0.04 seconds in total to generate the .pdf file
• lualatex without fontspec: 0.25 seconds to reach \begin{document}, then 0.01 seconds extra to generate the .pdf file
• lualatex with fontspec: 1.38 seconds to reach \begin{document}, then 0.06 seconds extra to generate the .pdf file. See example file above.
• lualatex with fontspec, with expl3.sty preloaded by lualatex --ini: 0.50 seconds to reach \begin{document}, then 0.05 seconds extra to generate the .pdf file. See example files below.
• lualatex with fontspec, with mylatexformat.ltx: 0.28 seconds to reach \begin{document}, then 0.07 seconds to generate the .pdf file.

Is there a way to make lualatex startup with fontspec and a font already loaded faster than 1.38 seconds?

I tried calling \dump just before \begin{document}, but when I attempted to load the .fmt file, I got an error stating that some Lua code wasn't available.

(Maybe I can try dumping and restoring the entire Linux process.)

I'm asking because we are implementing a web service which compiles short (1--2 page) LaTeX documents to PDF, and we'd like to have a fast solution (which finishes quickly and uses little CPU on the server). Upgrading from pdflatex to lualatex would make it about 10 times slower for our short documents, which is too slow.

FYI Here is how I used mylatexformat.ltx. I generated luaheader.fmt with (should work on both Linux and Windows):

$luatex -interaction=nonstopmode -ini -jobname=luaheader "&lualatex" mylatexformat.ltx compact.tex  Then I compiled the compact.tex document to .pdf with either of these: $ luatex "&luaheader" compact
$lualatex --fmt luaheader compact  FYI Here are my source files using lualatex --ini manually: % This is half1.tex, compile it with: %$ lualatex --ini half1.tex
\let\OLDdump\dump
\let\dump\relax
\input lualatex.ini
\let\dump\OLDdump
\let\OLDselectfont\selectfont
% \selectfont called by \normalsize defined and called by size10.clo loaded
% by article.cls. If we don't change \selectfont here, we get an error
% message: ! Font \TU/lmr/m/n/10=[lmroman10-regular]:+tlig; at 10pt not
\def\selectfont{\baselineskip12pt }
\documentclass[a4paper]{article}
\let\selectfont\OLDselectfont
\usepackage{expl3}
\usepackage{xparse}
% This fails in initex with: [\directlua]:1: attempt to index global
% 'luatexbase' (a nil value)
%\usepackage{luatexbase}
% This fails in initex with:
% to index global 'luatexbase' (a nil value)
%\usepackage{fontspec}  % Needs luaotfload to work.
% This needs luatexbase.
%\usepackage[utf8]{luainputenc}
\dump


and

% This is half2.tex, compile it with:
% \$ lualatex --fmt half1 half2
\normalsize
% This is needed only for old (non-fontspec) fonts.
%\usepackage[utf8]{luainputenc}
\usepackage{fontspec}
\newfontfamily\myverdana{Verdana.ttf}[]
% 0.50s user time up to this point.
% \makeatletter\@@end
\begin{document}
\hrule
{\Huge Helló, Wörld, árvíztűrő}
\hrule
{\Huge Helló, Wörld, árvízt\H{u}r\H{o}}
\hrule
{\myverdana\Huge Helló, Wörld, árvíztűrő}
\hrule
\end{document}
% 0.55s user time for half2.tex up to this point.

• At least part of the overhead is loading expl3 (there is a lot of dynamic Unicode data): the LaTeX team are having discussions about exactly this. – Joseph Wright Jan 10 at 15:53
• Don't load the inputenc package with lualatex. Beside this: is the time acceptable if you don't load fontspec (and naturally remove the newfontfamily command)? – Ulrike Fischer Jan 10 at 16:06
• @UlrikeFischer: I've updated my time measurements above to include lualatex without fontspec. lualatex without fontspec won't work for us, because it is not able to hyphenate Hungarian words containing the letter \H{o} correctly. pdflatex won't work for us, because the underline package we are using doesn't work correctly with pdflatex. – pts Jan 10 at 16:32
• hyphenation and fontspec are more or less unrelated. The TU-encoding can also be loaded without it. But if the time without fontspec is good enough for you then you could as a first step create a format which contains \usepackage{expl3} so that fontspec no longer has to load it. – Ulrike Fischer Jan 10 at 16:43
• This question is also a duplicate of Using fontspec (which requires exp3-code.tex) makes lualatex slow but it has no answer either. – Henri Menke Jan 11 at 2:56

If your documents use the same preamble, maybe you can save time using a prebuild format.

I produced a prebuild format and tested (texlive 2018, updated) your file:

1. Let's say the filename is myjob.tex. Then produce the format with this command on the command line (Peter, as far as I know you are on Linux, I tested on Windows):

luatex -interaction=nonstopmode -ini -jobname="luaheader" "&lualatex" mylatexformat.ltx ""myjob.tex""

2. Put this text as first line of the file you want to compile: %&luaheader.

OK, probably you have many different file names and I don't have a clue how to adapt this code here to that.

And more important, I'm not sure if you really save much time, because I don't know how to measure compilation times correctly.

By the way: many many thanks for pdfsizeopt, which really has been helpfull for so many years and still is indispensable!

• Thank you for suggesting mylatexformat.ltx! It speeds up initialization from 1.38s to 0.28s for the compact.tex example above. I've updated my question with more details including which commands worked for me. – pts Jan 11 at 15:59
• I'm accepting your answer, because it's unlikely that further speedups are possible with LuaTeX 1.07. – pts Jan 11 at 16:00
• @pts I am so glad that I can give you back something for all your work with pdfsizeopt, really! – Keks Dose Jan 11 at 16:01