[Tex/LaTex] XeLaTeX with fontspec is slow every time, not just the first time

compilingfontsfontspecxetex

I am running XeLaTeX (distributed as part of MikTeX) on Windows 7 Service Pack 1 64-bit. The following test file takes a long time to compile:

\documentclass{article}
\usepackage{fontspec}
\message{Hello}
\setmainfont{AlegreyaSC-Regular}
\message{How are you?}
\begin{document}
Test
\end{document}

Every time I compile it, it takes 45-50 seconds between the "Hello" and "How are you?" messages being printed to the screen. (Alegreya is an opentype font I want to use in a document. I downloaded it from a web site. Alegreya-SC Regular means the small caps non-bold non-italic version of the font.)

I thought that perhaps XeLaTeX or fontspec might be trying to write a font cache somewhere but getting prevented from doing so because it was not running with administrator permissions; so I tried running XeLaTeX as administrator and manually entering the commands in the file. However, compilation remained slow after doing this.

This question appears similar on the face of it, but mentions that the author found that using the -no-pdf argument to xelatex caused it to run more quickly; this made no difference for me when I tried it.

XeLaTeX's greeting:

This is XeTeX, Version 3.14159265-2.6-0.99991 (MiKTeX 2.9 64-bit)

Edit: A part of the log file

(C:\LaTeX\tex\latex\fontspec\fontspec.cfg))) Hello
\g__file_internal_ior=\read1
.................................................
. fontspec info: "no-scripts"
. 
. Font AlegreyaSC-Regular does not contain any OpenType `Script' information.
.................................................
\g__fontspec_family_AlegreyaSC-Regular_int=\count120
.................................................
. fontspec info: "defining-font"
. 
. Font family 'AlegreyaSC-Regular(0)' created for font 'AlegreyaSC-Regular'
. with options [Ligatures=TeX].
. 
. This font family consists of the following shapes:
. 
. * 'normal' with NFSS spec.:
. <->"AlegreyaSC-Regular/OT:mapping=tex-text;"
. 
. * 'small caps' with NFSS spec.:
. 
. and font adjustment code:
. 
. 
. * 'bold' with NFSS spec.:
. <->"AlegreyaSC-Regular/B/OT:mapping=tex-text;"
. 
. * 'bold small caps' with NFSS spec.:
. 
. and font adjustment code:
. 
. 
. * 'italic' with NFSS spec.:
. <->"AlegreyaSC-Regular/I/OT:mapping=tex-text;"
. 
. * 'small caps' with NFSS spec.:
. 
. and font adjustment code:
. 
. 
. * 'bold italic' with NFSS spec.:
. <->"AlegreyaSC-Regular/BI/OT:mapping=tex-text;"
. 
. * 'bold small caps' with NFSS spec.:
. 
. and font adjustment code:
. 
.................................................
 How are you? (test.aux)

Best Answer

The first thing to diagnose for "XeTeX running slowly" problems is to compile the document with xelatex -no-pdf, and then run xdvipdfmx manually on the resulting .xdv file. Timing each one separately will indicate whether it's XeTeX running slowly or xdvipdfmx.

The next step is to figure out if it's XeTeX or fontspec at fault. I would be surprised if it were fontspec that were causing this slowdown. (Not because I have high regard for fontspec's efficiency, but rather that others haven't reported such terrible performance.) Try compiling the following example instead:

\documentclass{article}
\message{Hello}
\font\x="AlegreyaSC-Regular" at 10pt
\message{How are you?}
\begin{document}
{\x Test}
\end{document}

My hypothesis is that this is also slow for you. (If not, maybe it is my fault after all.)

This method for loading the font (by "font name") is presumably using the Windows equivalent of fc-cache, which I know very little about. It's possible that you have many many fonts installed in your system, and this is slowing things down. Or that there's something misconfigured that's forcing some sort of database rebuild every time XeTeX queries for a font.

You might be able to get around this problem by loading the font by filename instead:

\setmainfont[Path=...]{AlegreyaSC-Regular.otf}

This will require you to select the bold/italic shapes manually; e.g., something like:

\setmainfont[Path=...,Extension=.otf,
   UprightFont=*-Regular,
   ItalicFont=*-Italic,...]{AlegreyaSC}