Mathjax processing time

94 views
Skip to first unread message

ad...@8chan.co

unread,
Jan 31, 2015, 1:03:14 AM1/31/15
to mathja...@googlegroups.com
Hello,

I used to have KaTeX enabled on my imageboard, but I had to disable it due to abuse by users who were inserting very long expressions, like...

\Huge \sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

This causes browsers to crash/freeze.

I've determined that this also freezes MathJax, so it wouldn't work for me either. Is there anything that can be done about this for user-generated content sites? Does Mathjax have a setting for the maximum length of expressions, for example, or a timeout if an expression is overly long?

Best,
Fredrick Brennan
8chan Administrator

fredrick...@gmail.com

unread,
Jan 31, 2015, 1:10:53 AM1/31/15
to mathja...@googlegroups.com, ad...@8chan.co
I just tested this on the Math Stack Exchange and was able to crash my browser there too.


(Sorry, I thought this was a known issue...)

Peter Krautzberger

unread,
Jan 31, 2015, 4:09:32 AM1/31/15
to mathja...@googlegroups.com
Hi Frederick,

I don't think we currently have an option to limit the size of expressions like this (though it seems we should). Perhaps we can come up with a workaround. 

There are a couple of other options that are relevant for user-generated sites.  First, there's the Safe extension which prevents other kinds of abuse. Second, the TeX input processor has two options for limiting macro recursion -- MAXMACROS and MAXBUFFER -- but these don't help with your particular case.

Will think about a workaround for your case.

Best regards,
Peter.


--
You received this message because you are subscribed to the Google Groups "MathJax Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathjax-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Davide P. Cervone

unread,
Feb 1, 2015, 3:27:19 PM2/1/15
to mathja...@googlegroups.com
The issue here is not really the complexity of the mathematics, it is the fact that stretchy delimiters are created from multiple (small) pieces, and that the larger they are, the more pieces are needed. In your case, after the first 5 or so square roots, the remaining ones are formed from multiple characters for the vertical line. It is the fact that the equation uses so many of those lines (and that they are so long) that is the real cause of the long processing time.

MathJax currently has four different output modes: HTML-CSS, SVG, NativeMML, and the new CommonHTML. Of these, only HTML-CSS uses multiple pieces for the delimiters. Switching to SVG output, for example, processed the output much quicker. Of course, the timing will depend on the speed of your computer, the amount of memory available, what else is running on the computer, the operating system used, and other factors, but here are some results from my setup to give an idea of the relative speeds of the various output forms using your example:

In Safari:

HTML-CSS: 7 seconds to process, 10 more to display (17 total)
SVG: .2 seconds
CommonHTML: .05 seconds
NativeMML: .02 seconds

In Firefox:

HTML-CSS 13 seconds to process, 31 more to display (44 total)
SVG: .25 seconds
CommonHTML: .1 seconds
NativeMML: .04 seconds to process, 9 seconds to display

So you can see that the HTML-CSS output (that uses multiple characters for each square root) is substantially slower than the other forms. The results also take a long time to display on screen as the layout is complex and it takes the browser longer to get the display worked out than it takes MathJax to specify the DOM.

The SVG output uses a single stretched joining character between the two end characters (so three in all), while CommonHTML uses only one character stretched (for rather bad results). Safari is in general twice as fast as Firefox, and its NativeMML appears to use a single stretched character, not multiple ones. The long delay in Firefox's NativeMML suggests that it also uses multiple characters for the stretched glyph internally.

MathJax's HTML-CSS mode does have a parameter that controls the maximum number of characters that it will use when creating a stretchy character. By default, it is 1000, but if you set this to 20, for example, then HTML-CSS output becomes

Safari HTML-CSS: 1.7 seconds, 2.3 more to display (4 total)
Firefox HTML-CSS: 2.5 seconds, 2.5 more to display (5 total)

But if you do this, the delimiters don't stretch as far as they should. Moreover, HTML-CSS uses multiple characters for the line over the square root as well, and these become dashed lines if not enough characters are used. So reducing this number too far can damage the output. But you certainly could limit it to something smaller than the default. To do so, use

<script type="tet/x-mathjax-config">
MathJax.Hub.Register.StartupHook("HTML-CSS Jax Ready",function () {
MathJax.OutputJax["HTML-CSS"].maxStretchyParts = 100; // or whatever value you want
});
</script>

before the script that loads MathJax.js initially. Alternatively, you could switch to SVG output for now until the CommonHTML output matures.

Finally, since you brought up KaTeX, I measured that as well on your expression:

Safari KaTeX: 13 seconds
Firefox KaTeX: 56 seconds

Since KaTeX implements the TeX display pipeline, it uses multiple character stretchy delimiters as well, and so gives results comparable to MathJax's HTML-CSS output in this case.


I don't think that limiting the complexity of the mathematics, or the length of the expression as a TeX string, or the nesting level, or solutions of that sort are the right approach, here. The math can be processed relatively quickly, as is shown by the SVG and CommonHTML output. It is building the stretchy delimiters that is causing the trouble, and that is where the solution has to lie.

Davide

Davide P. Cervone

unread,
Feb 1, 2015, 3:29:36 PM2/1/15
to mathja...@googlegroups.com
PS, with my numbers for KaTeX, I could not separate out the processing from the display as I could with MathJax, so those numbers include both the building of the DOM and the browser working to display that DOM (so are comparable to the 17 and 44 totals for MathJax).

Davide

8chan Admin

unread,
Feb 2, 2015, 7:55:00 PM2/2/15
to mathja...@googlegroups.com
Hey Davide,

Thanks a lot for the advice! I wasn't aware that changing the output format could make such a big difference.

I was able to re-enable TeX on my site by creating a custom config called 8chanTeX that only outputs to SVG and that also disables completely \style, \href, \class and \cssId (the next thing they did was \style{padding:100000px 0;background:pink;cursor:help}{\text{thread}=\text{ruined}} but I knew how to fix that).

Imageboard users are pretty intent on breaking things any way they can, so if there are any other problems I'm sure they'll find them eventually.

Best,
Fredrick Brennan
8chan Administrator

> You received this message because you are subscribed to a topic in the Google Groups "MathJax Users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/mathjax-users/S7Fu7ZaEsVg/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to mathjax-user...@googlegroups.com.

Del Wegener

unread,
Feb 3, 2015, 7:50:24 AM2/3/15
to mathja...@googlegroups.com
Davide:
What you say makes sense. Your time trials seem to pinpoint my problem.
The compare favorably with my experience with FF.

I have this line in the <head> of my page

src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
which I presume set the output to HTML-CSS.
Is there a similar line that would switch to SVG?
Where do I find such information?
Thanks.
Del

8chan Admin

unread,
Feb 3, 2015, 6:42:26 PM2/3/15
to mathja...@googlegroups.com
Hello,

I have MathJax installed instead of using the CDN, but the default distribution includes a file called "TeX-AMS-MML_SVG.js". I think you can just change config to "config=TeX-AMS-MML_SVG" for only SVG output. If that's not on the CDN, you can't do it without installing MathJax locally.

Note that this does not insulate you from the \style abuse I mentioned, you have to make a custom config for that. I found this on Math Stackexchange Meta originally (http://www.math.union.edu/locate/Cervone/transfer/mathjax/mathjax-MSE-safe-mode.user.js - also written by Davide Cervone who replied to this thread, small world) and I edited it a bit to make https://ghostbin.com/paste/hhwsj which seems to work for me. I then copied the TeX-AMS-MML_SVG.js to 8chanTeX.js and put that in the file.

I actually think that for my case I don't even need AsciiMath or MML, so I'm going to strip it down even more and just allow TeX2Jax input and SVG output. But, most sites probably don't have to be as cautious as me about receiving user input.

Best,
Fredrick Brennan
8chan Administrator

Peter Krautzberger

unread,
Feb 4, 2015, 8:15:23 AM2/4/15
to mathja...@googlegroups.com
Hi,

You can find more information on the pre-compiled configuration files at https://docs.mathjax.org/en/latest/config-files.html.

For example, there's the TeX-AMS-MML_SVG configuration.


Best regards,
Peter.

You received this message because you are subscribed to the Google Groups "MathJax Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathjax-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages