CTeX-org/ctex-kit

Lualatex + Beamer compile very slow when using Source Han CJK fonts

Firestar-Reimu opened this issue · 7 comments

MWE: https://paste.c-net.org/MongooseHologram
Bib: https://paste.c-net.org/CombinedVirile
Compile: latexmk -lualatex -bibtex -synctex=1 -interaction=nonstopmode -file-line-error --shell-escape mwe.tex
TEX: Texlive 2024 + ctex 2.5.10
OS: Archlinux

ctex fontset biblatex time
+ custom source han + 201s
+ custom source han - 133s
+ adobe - 32s
+ windows - 15s
+ founder - 12s
+ fandol - 7s
+ custom serif windows + 40s
+ fandol + 37s

run lua -> bib -> lua*4 if have biblatex

run lualatex 3 times if no biblatex

Source Han fontset will stuck at:

(/home/firestar/.texlive/2024/texmf-dist/tex/latex/ctex/scheme/ctex-scheme-chin
ese.def
(/home/firestar/.texlive/2024/texmf-dist/tex/latex/ctex/config/ctex-name-utf8.c
fg))) (/home/firestar/.texlive/2024/texmf-dist/tex/latex/ctex/config/ctex.cfg)

for about 30s every lualatex run

another question is why lualatex will run 3 times instead of 2 times?

Looks like related to font caching in LuaLaTeX, which is provided by luaotfload package.

Can you narrow down the example with

  • article instead of beamer document class
  • fontspec instead of ctex package
  • no bibliography stuff

Tested your example on macOS 13.6.6, with Source Han CJK replaced with Noto Serif CJK SC font and two biblatex lines commented out, only the first run took 26s and all following runs took 5--6s. The first run is much slower because luaotfload was saving font caches.

\setCJKmainfont{Noto Serif CJK SC}[BoldFont=Noto Serif CJK SC Medium]

Source Han fontset will stuck at:

(/home/firestar/.texlive/2024/texmf-dist/tex/latex/ctex/scheme/ctex-scheme-chin
ese.def
(/home/firestar/.texlive/2024/texmf-dist/tex/latex/ctex/config/ctex-name-utf8.c
fg))) (/home/firestar/.texlive/2024/texmf-dist/tex/latex/ctex/config/ctex.cfg)

for about 30s every lualatex run

It's actually stuck at \setCJKmainfont line, which you can verify by writing something to terminal right before it:

\immediate\write16{debug: before setCJKmainfont}
\setCJKmainfont{Source Han Serif CN}[BoldFont=Source Han Sans CN Medium]
\immediate\write16{debug: after setCJKmainfont}

So I'm guessing that somehow luaotfload failed to create font caches for at least Source Han CJK on your Archlinux.

By default, the font cache will be written to $(kpsewhich -var-value TEXMFVAR)/luatex-cache/generic/fonts (see last stdout line of luaotfload-tool -h). For example, the caches created for Noto Serif CJK SC font on my side were

$ find $(kpsewhich -var-value TEXMFVAR)/luatex-cache/generic/fonts -name 'notoserifcjksc*'
/Users/username/Library/texlive/2024basic/texmf-var/luatex-cache/generic/fonts/otl/notoserifcjksc-regular.luc
/Users/username/Library/texlive/2024basic/texmf-var/luatex-cache/generic/fonts/otl/notoserifcjksc-medium.luc
/Users/username/Library/texlive/2024basic/texmf-var/luatex-cache/generic/fonts/otl/notoserifcjksc-regular.lua
/Users/username/Library/texlive/2024basic/texmf-var/luatex-cache/generic/fonts/otl/notoserifcjksc-medium.lua

I can see in /home/firestar/.texlive2024/texmf-var/luatex-cache/generic/fonts/otl and there are font-cache of my fonts

The file size of source-hans-cn is not that large comparing to other fonts, all at ~1MB scale (adobe-heiti has 4MB too and even times need 1MB):

(base) [firestar@Archlinux otl]$ ls -l
total 137972
-rw-r--r-- 1 firestar firestar 4251383  4月  3 15:58 adobeheitistd-regular.lua
-rw-r--r-- 1 firestar firestar 2539926  4月  3 15:58 adobeheitistd-regular.luc
-rw-r--r-- 1 firestar firestar 4266488  3月 29 20:28 adobekaitistd-regular.lua
-rw-r--r-- 1 firestar firestar 2582695  3月 29 20:28 adobekaitistd-regular.luc
-rw-r--r-- 1 firestar firestar 4249703  4月  3 15:58 adobesongstd-light.lua
-rw-r--r-- 1 firestar firestar 2534925  4月  3 15:58 adobesongstd-light.luc
-rw-r--r-- 1 firestar firestar 1257952  4月  3 15:31 fandolhei-bold.lua
-rw-r--r-- 1 firestar firestar  844341  4月  3 15:31 fandolhei-bold.luc
-rw-r--r-- 1 firestar firestar 1246451  4月  3 15:31 fandolhei-regular.lua
-rw-r--r-- 1 firestar firestar  827967  4月  3 15:31 fandolhei-regular.luc
-rw-r--r-- 1 firestar firestar 1261424  4月  2 16:06 fandolkai-regular.lua
-rw-r--r-- 1 firestar firestar  844537  4月  2 16:06 fandolkai-regular.luc
-rw-r--r-- 1 firestar firestar 1267434  4月  2 16:06 fandolsong-bold.lua
-rw-r--r-- 1 firestar firestar  849838  4月  2 16:06 fandolsong-bold.luc
-rw-r--r-- 1 firestar firestar 1266861  4月  2 16:06 fandolsong-regular.lua
-rw-r--r-- 1 firestar firestar  847655  4月  2 16:06 fandolsong-regular.luc
-rw-r--r-- 1 firestar firestar 2520675  4月  3 15:54 fzhtk.lua
-rw-r--r-- 1 firestar firestar 1577840  4月  3 15:54 fzhtk.luc
-rw-r--r-- 1 firestar firestar 2591411  3月 26 21:10 fzktk.lua
-rw-r--r-- 1 firestar firestar 1678246  3月 26 21:10 fzktk.luc
-rw-r--r-- 1 firestar firestar 2501694  4月  3 15:54 fzssk.lua
-rw-r--r-- 1 firestar firestar 1555685  4月  3 15:54 fzssk.luc
-rw-r--r-- 1 firestar firestar 2645108  4月  3 15:54 fzxbsk.lua
-rw-r--r-- 1 firestar firestar 1684101  4月  3 15:54 fzxbsk.luc
-rw-r--r-- 1 firestar firestar 2562933  4月  3 15:54 fzxh1k.lua
-rw-r--r-- 1 firestar firestar 1626530  4月  3 15:54 fzxh1k.luc
-rw-r--r-- 1 firestar firestar 1192882  4月  3 15:57 gkai00mp.lua
-rw-r--r-- 1 firestar firestar  660116  4月  3 15:57 gkai00mp.luc
-rw-r--r-- 1 firestar firestar 1044014  3月 27 14:55 latinmodern-math.lua
-rw-r--r-- 1 firestar firestar  637036  3月 27 14:55 latinmodern-math.luc
-rw-r--r-- 1 firestar firestar  420437  3月 27 14:55 linguafranca-bolditalic.lua
-rw-r--r-- 1 firestar firestar  259129  3月 27 14:55 linguafranca-bolditalic.luc
-rw-r--r-- 1 firestar firestar  456169  3月 27 14:55 linguafranca-bold.lua
-rw-r--r-- 1 firestar firestar  276588  3月 27 14:55 linguafranca-bold.luc
-rw-r--r-- 1 firestar firestar  431405  3月 27 14:55 linguafranca-italic.lua
-rw-r--r-- 1 firestar firestar  263967  3月 27 14:55 linguafranca-italic.luc
-rw-r--r-- 1 firestar firestar  550275  3月 27 14:55 linguafranca-regular.lua
-rw-r--r-- 1 firestar firestar  336251  3月 27 14:55 linguafranca-regular.luc
-rw-r--r-- 1 firestar firestar  131566  3月 28 15:04 lmmono10-regular.lua
-rw-r--r-- 1 firestar firestar   81842  3月 28 15:04 lmmono10-regular.luc
-rw-r--r-- 1 firestar firestar  131543  3月 27 14:35 lmmonolt10-bold.lua
-rw-r--r-- 1 firestar firestar   82033  3月 27 14:35 lmmonolt10-bold.luc
-rw-r--r-- 1 firestar firestar  200491  3月 26 21:10 lmroman10-bold.lua
-rw-r--r-- 1 firestar firestar  128367  3月 26 21:10 lmroman10-bold.luc
-rw-r--r-- 1 firestar firestar  200868  3月 25 16:21 lmroman10-regular.lua
-rw-r--r-- 1 firestar firestar  127284  3月 25 16:21 lmroman10-regular.luc
-rw-r--r-- 1 firestar firestar  200996  4月  3 15:00 lmroman6-regular.lua
-rw-r--r-- 1 firestar firestar  128473  4月  3 15:00 lmroman6-regular.luc
-rw-r--r-- 1 firestar firestar  200741  3月 26 21:10 lmroman8-regular.lua
-rw-r--r-- 1 firestar firestar  128103  3月 26 21:10 lmroman8-regular.luc
-rw-r--r-- 1 firestar firestar  200168  3月 25 16:21 lmsans10-bold.lua
-rw-r--r-- 1 firestar firestar  128061  3月 25 16:21 lmsans10-bold.luc
-rw-r--r-- 1 firestar firestar  200055  3月 25 16:21 lmsans10-regular.lua
-rw-r--r-- 1 firestar firestar  127603  3月 25 16:21 lmsans10-regular.luc
-rw-r--r-- 1 firestar firestar  200161  4月  3 15:00 lmsans12-regular.lua
-rw-r--r-- 1 firestar firestar  128122  4月  3 15:00 lmsans12-regular.luc
-rw-r--r-- 1 firestar firestar  200250  3月 27 14:35 lmsans8-regular.lua
-rw-r--r-- 1 firestar firestar  127553  3月 27 14:35 lmsans8-regular.luc
-rw-r--r-- 1 firestar firestar 3673198  4月  3 15:47 msyh-1.lua
-rw-r--r-- 1 firestar firestar 2268462  4月  3 15:47 msyh-1.luc
-rw-r--r-- 1 firestar firestar 3611067  4月  3 15:47 msyhbd-1.lua
-rw-r--r-- 1 firestar firestar 2224636  4月  3 15:47 msyhbd-1.luc
-rw-r--r-- 1 firestar firestar 3125950  4月  3 15:47 simhei.lua
-rw-r--r-- 1 firestar firestar 1763220  4月  3 15:47 simhei.luc
-rw-r--r-- 1 firestar firestar 3357690  4月  3 15:47 simkai.lua
-rw-r--r-- 1 firestar firestar 2057660  4月  3 15:47 simkai.luc
-rw-r--r-- 1 firestar firestar 3717340  3月 27 14:55 simsun-1.lua
-rw-r--r-- 1 firestar firestar 1874756  3月 27 14:55 simsun-1.luc
-rw-r--r-- 1 firestar firestar 4457191  3月 26 21:10 sourcehansanscn-medium.lua
-rw-r--r-- 1 firestar firestar 2608228  3月 26 21:10 sourcehansanscn-medium.luc
-rw-r--r-- 1 firestar firestar 4453593  3月 27 15:10 sourcehansanscn-regular.lua
-rw-r--r-- 1 firestar firestar 2603455  3月 27 15:10 sourcehansanscn-regular.luc
-rw-r--r-- 1 firestar firestar 4665507  4月  3 14:58 sourcehanserifcn-bold.lua
-rw-r--r-- 1 firestar firestar 2734672  4月  3 14:58 sourcehanserifcn-bold.luc
-rw-r--r-- 1 firestar firestar 4620243  4月  4 13:01 sourcehanserifcn-regular.lua
-rw-r--r-- 1 firestar firestar 2713759  4月  4 13:01 sourcehanserifcn-regular.luc
-rw-r--r-- 1 firestar firestar 1513791  3月 26 21:10 stixtwomath-regular.lua
-rw-r--r-- 1 firestar firestar  926134  3月 26 21:10 stixtwomath-regular.luc
-rw-r--r-- 1 firestar firestar 1029591  3月 26 21:10 stixtwotext-bolditalic.lua
-rw-r--r-- 1 firestar firestar  622723  3月 26 21:10 stixtwotext-bolditalic.luc
-rw-r--r-- 1 firestar firestar  986863  3月 26 21:10 stixtwotext-bold.lua
-rw-r--r-- 1 firestar firestar  610435  3月 26 21:10 stixtwotext-bold.luc
-rw-r--r-- 1 firestar firestar 1030997  3月 26 21:10 stixtwotext-italic.lua
-rw-r--r-- 1 firestar firestar  641021  3月 26 21:10 stixtwotext-italic.luc
-rw-r--r-- 1 firestar firestar  987664  3月 26 21:10 stixtwotext-regular.lua
-rw-r--r-- 1 firestar firestar  628594  3月 26 21:10 stixtwotext-regular.luc
-rw-r--r-- 1 firestar firestar  435832  3月 25 16:24 texgyrepagellax-bolditalic.lua
-rw-r--r-- 1 firestar firestar  266144  3月 25 16:24 texgyrepagellax-bolditalic.luc
-rw-r--r-- 1 firestar firestar  474163  3月 25 16:24 texgyrepagellax-bold.lua
-rw-r--r-- 1 firestar firestar  290229  3月 25 16:24 texgyrepagellax-bold.luc
-rw-r--r-- 1 firestar firestar  417848  3月 25 16:24 texgyrepagellax-boldslanted.lua
-rw-r--r-- 1 firestar firestar  259674  3月 25 16:24 texgyrepagellax-boldslanted.luc
-rw-r--r-- 1 firestar firestar  484853  3月 25 16:24 texgyrepagellax-italic.lua
-rw-r--r-- 1 firestar firestar  295950  3月 25 16:24 texgyrepagellax-italic.luc
-rw-r--r-- 1 firestar firestar  494135  3月 25 16:24 texgyrepagellax-regular.lua
-rw-r--r-- 1 firestar firestar  301391  3月 25 16:24 texgyrepagellax-regular.luc
-rw-r--r-- 1 firestar firestar  432942  3月 25 16:24 texgyrepagellax-slanted.lua
-rw-r--r-- 1 firestar firestar  271837  3月 25 16:24 texgyrepagellax-slanted.luc
-rw-r--r-- 1 firestar firestar 1156516  3月 27 14:35 timesbd.lua
-rw-r--r-- 1 firestar firestar  696202  3月 27 14:35 timesbd.luc
-rw-r--r-- 1 firestar firestar  885289  3月 27 14:35 timesbi.lua
-rw-r--r-- 1 firestar firestar  536025  3月 27 14:35 timesbi.luc
-rw-r--r-- 1 firestar firestar  891140  3月 27 14:35 timesi.lua
-rw-r--r-- 1 firestar firestar  538673  3月 27 14:35 timesi.luc
-rw-r--r-- 1 firestar firestar  108159  3月 27 14:47 timesltpro-bolditalic.lua
-rw-r--r-- 1 firestar firestar   68004  3月 27 14:47 timesltpro-bolditalic.luc
-rw-r--r-- 1 firestar firestar  164547  3月 27 14:47 timesltpro-bold.lua
-rw-r--r-- 1 firestar firestar  100732  3月 27 14:47 timesltpro-bold.luc
-rw-r--r-- 1 firestar firestar  107847  3月 27 14:47 timesltpro-italic.lua
-rw-r--r-- 1 firestar firestar   67469  3月 27 14:47 timesltpro-italic.luc
-rw-r--r-- 1 firestar firestar  162506  3月 27 14:47 timesltpro-roman.lua
-rw-r--r-- 1 firestar firestar   99375  3月 27 14:47 timesltpro-roman.luc
-rw-r--r-- 1 firestar firestar 1153483  3月 27 14:35 times.lua
-rw-r--r-- 1 firestar firestar  692943  3月 27 14:35 times.luc
-rw-r--r-- 1 firestar firestar  488682  3月 27 14:48 xitsmath-bold.lua
-rw-r--r-- 1 firestar firestar  291858  3月 27 14:48 xitsmath-bold.luc
-rw-r--r-- 1 firestar firestar  874374  3月 27 14:48 xitsmath-regular.lua
-rw-r--r-- 1 firestar firestar  540671  3月 27 14:48 xitsmath-regular.luc

But when I compile mwe.tex for the 2nd or more time, it still need ~47s with or without biblatex

Looks like related to font caching in LuaLaTeX, which is provided by luaotfload package.

Can you narrow down the example with

* `article` instead of `beamer` document class

* `fontspec` instead of `ctex` package

* no bibliography stuff

1.I am writing a chinese beamer now so I test beamer
2.How to write chinese without ctex?
3.I have some tests without bibliography you can see.

Here is an article example:

\documentclass[a4paper,fontset=none]{ctexart}

\setCJKmainfont{Source Han Serif CN}[BoldFont=Source Han Sans CN Medium,ItalicFont=FZKai-Z03]

\title{\textbf{作业}}
\author{作业}
\date{2024年3月29日}

\begin{document}

\maketitle

正文123

\end{document}

1st compile 44s
2nd compile 44s
3rd compile 43s

also, if you do not change the .tex file and do not clean the generated files, run latexmk the second time will give you:

Rc files read:
  NONE
Latexmk: This is Latexmk, John Collins, 31 Jan. 2024. Version 4.83.
Latexmk: Nothing to do for 'mwe2.tex'.
Latexmk: All targets (mwe2.pdf) are up-to-date

[ble: elapsed 463ms (CPU 97.8%)]

which isn't the true time lualatex compile.

1.I am writing a chinese beamer now so I test beamer

Real working documents can be complex and load a bunch of packages, but to narrow down a problem, a minimal working example is usually recommended, see TeX.Meta.SX question Why does TeX require such elaborate MWE.

A minimal example may also reveal which package is the possible cause. For example if your problem reproduces with fontspec only, then it's not caused by ctex bundle hence should be reported elsewhere.

2.How to write chinese without ctex?

Try loading fontspec only then setting CJK fonts with \setmainfont.

MWE:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Source Han Serif CN}[BoldFont=Source Han Sans CN Medium]

\title{\textbf{作业}}
\author{作业}
\date{\today}

\begin{document}

\maketitle

正文123

\end{document}

log: https://paste.c-net.org/ValidityTanaka

It takes 41s.

Delete the [BoldFont] section does not reduce time

https://paste.c-net.org/WasherStrait

It will stuck at (/home/firestar/.texlive/2024/texmf-dist/tex/latex/fontspec/fontspec.cfg)))

``` (base) [firestar@Archlinux Documents]$ latexmk -lualatex -bibtex -synctex=1 -interaction=nonstopmode -file-line-error --shell-escape mwe2.tex Rc files read: NONE Latexmk: This is Latexmk, John Collins, 31 Jan. 2024. Version 4.83. No existing .aux file, so I'll make a simple one, and require run of *latex. Latexmk: applying rule 'lualatex'... Rule 'lualatex': Reasons for rerun Category 'other': Rerun of 'lualatex' forced or previously required: Reason or flag: 'Initial setup'

Run number 1 of rule 'lualatex'


Running 'lualatex -synctex=1 -interaction=nonstopmode -file-line-error --shell-escape -recorder "mwe2.tex"'

This is LuaHBTeX, Version 1.18.0 (TeX Live 2024)
system commands enabled.
(./mwe2.tex
LaTeX2e <2023-11-01> patch level 1
L3 programming layer <2024-03-14>
(/home/firestar/.texlive/2024/texmf-dist/tex/latex/base/article.cls
Document Class: article 2023/05/17 v1.4n Standard LaTeX document class
(/home/firestar/.texlive/2024/texmf-dist/tex/latex/base/size10.clo))
(/home/firestar/.texlive/2024/texmf-dist/tex/latex/fontspec/fontspec.sty
(/home/firestar/.texlive/2024/texmf-dist/tex/latex/l3packages/xparse/xparse.sty
(/home/firestar/.texlive/2024/texmf-dist/tex/latex/l3kernel/expl3.sty
(/home/firestar/.texlive/2024/texmf-dist/tex/latex/l3backend/l3backend-luatex.d
ef)))
(/home/firestar/.texlive/2024/texmf-dist/tex/latex/fontspec/fontspec-luatex.sty
(/home/firestar/.texlive/2024/texmf-dist/tex/latex/base/fontenc.sty)
(/home/firestar/.texlive/2024/texmf-dist/tex/latex/fontspec/fontspec.cfg)))
(./mwe2.aux) (/home/firestar/.texlive/2024/texmf-dist/tex/latex/base/ts1cmr.fd)
[1{/home/firestar/.texlive/2024/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./mwe2.aux))
406 words of node memory still in use:
3 hlist, 1 vlist, 1 rule, 2 glue, 3 kern, 1 glyph, 4 attribute, 48 glue_spec
, 4 attribute_list, 1 write nodes
avail lists: 2:74,3:16,4:3,5:24,6:8,7:154,8:1,9:44,10:2
</usr/share/fonts/SourceHanSerifCN-Regular.otf></usr/share/fonts/adobe-source-h
an-sans/SourceHanSansCN-Medium.otf>
Output written on mwe2.pdf (1 page, 6968 bytes).
SyncTeX written on mwe2.synctex.gz.
Transcript written on mwe2.log.
Latexmk: Getting log file 'mwe2.log'
Latexmk: Examining 'mwe2.fls'
Latexmk: Examining 'mwe2.log'
Latexmk: Log file says output to 'mwe2.pdf'
Latexmk: All targets (mwe2.pdf) are up-to-date

[ble: elapsed 41.106s (CPU 99.4%)]

</details>

So it's really not ctex's fault. I'm closing this issue.

PS: GitHub flavored markdown requires a blank line between raw HTML <details> and normal markdown. So you may want to use

<details>

normal markdown

</details>

See related section in GitHub Flavored Markdown Specification and the /details slash command.