# [Tex/LaTex] Speeding up minted compilation

compilingminted

I've been looking into speeding up my pdflatex compilation time and it's lighting fast now.

Only one thing's still very slow: compiling source code using the minted package.

I was wondering if there is no way of speeding this up (perhaps like the tikz externalization \tikzexternalize that only needs to run once and from then on includes the result).

I was thinking of making a new environment that puts the minted environment inside a tikz environment, so that the rules of the \tikzexternalize may apply, but I haven't really figured out how to do this exactly (simply putting it inside a tike node doesn't work).

I am the author of the ConTeXt module t-vim which is similar to minted but uses vim rather than pygmentize to generate syntax highlighting. The t-vim module actually delegates the task of running external programs to t-filter module, which is provides the necessary pluming to call external programs on the content of an environment.

By default, the t-filter module behaves in the same manner as minted package: it writes the contents of the environment to an external file, calls the external program, and inputs the result back to TeX. However, to deal with slow external program, the filter module provides a continue=yes option. When this option is enabled, the content of each file is written to a separate file and the md5 sum of each file is calculated. The external filter is run only if the md5 sum is changed.

In MkII, this feature is enabled by calling the external program using

 \doifmode{*first}
{\executeexternalcommand{mtxrun --ifchanged=\inputfile \externalprogram}}


This calls mtxrun, the wrapper script for ConTeXt, which calculates the md5 sum of the file (and stores it as filename.md5) and the the program only if the md5 sum is stored. This is faster than running vim, but still slow as a new process (mtxrun) must be executed. To speed things up, I wrap the entire command in a \doifmode{*first} so that mtxrun is called only during the first run of a multi-run compilation.

To speed up things further, in MkIV, I use the ConTeXt lua function job.files.run, which stores the md5 in the tuc file (similar to aux file in LaTeX). So the call to the external program is roughly equal to

 \ctxlua{job.files.run("\inputfile", "\externalprogram")}


The same method can, in principle, be implemented in minted. In fact, the mtxrun --ifchanged method can be incorporated easily, provided that minted writes each environment in a separate file (currently it does not do that).