TeX daemon
From Mathtran
Contents |
Introduction
MathTran runs TeX as a daemon, to give greatly improved permformance why typesetting individual formulas. Here we explain what this means, and how it brings about this benefit.
Run invariant code only once
Removing invariant code from within a loop is a well-known way of improving the performance of a computer program. For example, instead of calculating every time it is used, one can calculate it once and for all, and then refer to the stored value.
The following shows that starting LaTeX (on a 800 MHz machine) takes about 0.2 seconds.
$ time latex '\stop' This is TeX, Version 3.14159 (Web2C 7.4.5) <snip> Transcript written on texput.log. real 0m0.191s user 0m0.180s sys 0m0.010s
However, on the same machine TeX can typeset a mathematical formula in about 0.003 seconds (3 milliseconds). Therefore, much better performance can be obtained by starting TeX once, and keeping it in memory, waiting for formulas to be typeset.
By today's standards, TeX is not a big program, and for our purposes it occupies about 2 Mbytes of memory.
mod_perl and FastCGI
According to wikipedia, mod_perl allows Perl to serve CGI requests 'without the significant overhead of re-launching the Perl interpreter for each request'. In other works, mod_perl runs Perl as a daemon.
There is an alternative technology, FastCGI, which runs both the server and the 'CGI script' as daemon processes, communicating over sockets. MathTran is similar, in that it runs a MathTran daemon as a separate process. (This daemon in turn runs TeX and dvipng as daemons.)
Use fork() instead of running a daemon?
Thinking somewhat along FastCGI lines, some people have suggested using fork() instead of running a daemon. There's a discussion thread on this on Sourceforge and also on comp.text.tex.
There is a test program that investigates this question. It indicates that, very roughly, forking could give a doubling of speed. Running TeX as a daemon gives a 60-times or so speedup compared to LaTeX. (In both cases, this is for small TeX fragment. None of these methods gives a speedup for books.)
