jperon / lyluatex

Alternative à lilypond-book pour lualatex
MIT License
58 stars 12 forks source link

lyluatex runs slowly on mac #303

Closed xhhdd closed 1 year ago

xhhdd commented 1 year ago

Hi author, I've been using lyluatex to get lilypond to work perfectly with latex, but I recently switched to an Apple silicon computer and have some new problems.

Background

Computer: apple m2 max 32g Lilypond: 2.24.1 lyluatex and mactex versions are the latest Tests have been performed at the command line as well as under various editors Do a comparison of windows computers: cpu-AMD 5700G

Specific questions

First, my latex minimal code case is:

\documentclass{ctexart}
\usepackage{lyluatex}

\begin{document}

\begin{enumerate}
    \item \lilypond{\clef bass \key c \major \time 2/2 c1}
    \item %When officially run, there will be 1000 lines of the same kind of code here
    \item .....
    \item .....
\end{enumerate}

\end{document} 

Generate results It works fine on Apple computers, but it takes about 16 minutes to run. But on windows it only takes almost half the time. This was very surprising to me because the Apple computer was just bought and I had very high expectations for its performance, while the windows hardware was already 2 years old.

In fact, in other cases, the performance of the Apple computer does far exceed that of my windows.

lilypond-book

So next I tested using lilypond-book to test if lilypond is not very suitable for use on Apple computers. I have made a small change to the code example I just used, in order to make the lilypond-book better recognizable.

\documentclass{ctexart}

\begin{document}

\begin{enumerate}
\item 
        \begin{lilypond}
            {\clef alto \key ees \major \time 2/2 deses'1}
        \end{lilypond}

\item ....
\end{enumerate}

\end{document} 

Then I changed the file suffix to lytex and started lilypond-book to convert the file. Finally run the resulting tex file. The process is very, very fast, taking only about 1 minute in total. Generate results However, as you can see from the image above the generated results are indeed far inferior to lylatex and the typography is very confusing.

Finally

So I would like to ask for help on.

ps: My English is not good, so a lot of the help of translation software, I hope I described the problem clearly.

jperon commented 1 year ago

Thank you for the report; and about you English, don’t worry: mine isn’t really good either!

Unfortunately, I don’t have a m2 at hand to test… But it should be feasible to test some points. First of all, when using lilypond-book, do you compile with lualatex or pdflatex or xelatex? Just to know whether the suboptimal performance comes from the lua interpreter included in lualatex. If it isn’t the case, I’ll have to add "benchmarking points" to lyluatex code to be able to know where the slowness comes from.

Another question: is the second compilation so slow? Normally, the "pre-compiled" scores in tmp-ly should make it far faster. If it isn’t the case, it would confirm the doubt about the lua interpreter.

xhhdd commented 1 year ago

Thank you very much for your reply!

You got to the crux of the matter at once!

lualatex

I tried as you suggested, and after I generated the tex file from lytex using lilypond, then compiled it using lalatex or pdflatex or xelatex respectively. It is really the slowest when choosing lualatex!!!

When using pdflatex or xelatex, the information on the console is too fast to see.

But when using lualatex, the information appears very slowly one by one. When I use lyluatex the situation is very consistent, it all comes up slowly one after the other.

(./bb/lily-90fa39dc-systems.tex) (./08/lily-f266e3b4-systems.tex)
(./8d/lily-01c711a4-systems.tex) (./22/lily-50867d21-systems.tex)
(./58/lily-de0375e5-systems.tex) (./1c/lily-78b34f80-systems.tex)
(./0b/lily-42ac6df0-systems.tex) (./2d/lily-02209b84-systems.tex)
(./08/lily-20fb2485-systems.tex) (./d5/lily-73607df5-systems.tex)
(./50/lily-595e2b43-systems.tex) (./a4/lily-1a9763db-systems.tex)
.......

The above is part of the information I chose, and the rest is pretty much the same.

About "Another question"

I may not have understood too well which time you mean by the ''second compilation''.

If you are referring to the use of lilypond-book, after the test above, it is fast as long as you don't use lualatex.

If you are referring to the case of using lyluatex, the overall speed is slow. Because using lyluatex creates a tmp-ly folder, and when I observe this folder during compilation, it is indeed slower in generating files. Other, I also added the option --draftmode according to the readme,There is no change in speed either, the files in tmp-ly will only be generated slowly.

Finally

PS:Finally, thanks again for your reply!

jperon commented 1 year ago

What I mean by "second compilation" is when using lyluatex: the first time you run it on a new document, it has to compile each score to pdf; but it keeps the result in the tmp-ly folder, with a hashed name, so it doesn’t need to re-compile scores each time (as soon as they don’t get modified).

According to what you say, it seems the problem is specific to lualatex. To ensure it doesn’t come from lyluatex itself, may you please try unzipping the attached archive, and compare compilation of test.tex with xelatex and lualatex? I’m just guessing, but I have the impression the slowness comes from \includegraphics.

test.zip

jperon commented 1 year ago

If it is indeed slow, I think it really comes from the lua interpreter. Not that it is slow in itself (lua is known as one of fastest interpreted languages), but I think MacTeX binaries are universal binaries, not optimized ones for M1 / M2. It could explain why it gets so slow. If your experiments confirm what I think, it should be possible to get support on one of luatex's mailing lists.

xhhdd commented 1 year ago

about test.zip

I unpacked the files and tested them. Since there is only one \includegraphics command in it, using xelatex or lualatex is instant. Then I copied the command 1000 times and ran it again, and I could observe that it took 3.1 seconds with lualatex and 3.4 seconds with xelatex.

about tmp-ly

I did observe that with lyluatex, the file is generated and placed in tmp-ly on the first run. I can also observe that every time a small score is generated, tmp-ly creates 4 file:

But this generation process takes a very long time. Here is the feedback from the console during generation:

(lyluatex)  Compiling score tmp-ly/594a594461ba80bf81f764b4af8e3b81 with LilyPond executable 'lilypond'.
Module lyluatex Warning: gs couldn't be launched; there could be rounding error
s. on input line 81

(lyluatex)  Compiling score tmp-ly/692bc7e81478c56e28afedf8517fe775 with LilyPond executable 'lilypond'.
Module lyluatex Warning: gs couldn't be launched; there could be rounding error
s. on input line 82

(lyluatex)  Compiling score tmp-ly/3908364aeb84f4e43af52999be911f82 with LilyPond executable 'lilypond'.
Module lyluatex Warning: gs couldn't be launched; there could be rounding error
s. on input line 83

(lyluatex)  Compiling score tmp-ly/77c389e6d4ef31c354cc36ca529294dd with LilyPond executable 'lilypond'.
Module lyluatex Warning: gs couldn't be launched; there could be rounding error
s. on input line 84

(lyluatex)  Compiling score tmp-ly/008ef284c228eac89bdedf8b7d076411 with LilyPond executable 'lilypond'.
Module lyluatex Warning: gs couldn't be launched; there could be rounding error
s. on input line 85

The "gs couldn't be launched", which I seem to have seen before under other issues, was said to have no effect on the generated results, so I didn't pay special attention to it.

The whole process of the above took about 12 minutes.

Next I did a second compilation, as expected, lyluatex doesn't need to re-compile scores, and finally it took a little over 2 minutes to finish.

Summary

Finally, thank you for your reply, it helped me a lot.

jperon commented 1 year ago

I hadn’t thought about it, but the gs errors could be a part of the explanation: thanks to @uliska's work, lyluatex is quite fussy about scores' width, and without gs, it sometimes launches useless re-compilations (on first pass). But it doesn’t explain the fact it is slow on 2nd pass. Using lilypond command / environment in "complicated" contexts (like enumerate) shouldn’t have significant impact on performances (if any).

jperon commented 1 year ago

Still compiling your .tex… but the time spent evidently comes from lilypond processing. Just guessing, but I wonder whether lilypond-book would be multi-threaded: it could be, given the way it works, while lyluatex (getting its context directly from lualatex, which explains why it can get superior results) can’t. It wouldn’t explain the difference on 2nd pass, though.

About possible optimizations, "if I were you" (which is always easy to say), I’d ensure that the binaries (from MacTeX and from LilyPond) are compiled natively for m2, not "universal" binaries. Otherwise, the only thing I see is avoiding to many changes on scores, so that the tmp-ly cache shows its benefits…

xhhdd commented 1 year ago

Thank you so much for your patience, I think I've got a good idea and figured out a lot of things ~!

By the way, lylatex is really a very good program for music majors who are using latex!