Skip to content

LaTeXML speedups and profiling #480

Description

@dginev

There is a new TeX to XML formula conversion paper that makes it a primary argument that the math conversion via LaTeXML is decidedly too slow, as compared to their MathJaX-based approach.

While there is a good reason why LaTeXML shouldn't be as fast (it does more!), I think we have not hit the best of our game in terms of speed (indeed our optimization efforts have been hitting only the obvious nails so far), so I am making a meta ticket to try and plan ahead some strategies for making things faster, while still remaining within the bounds of reason.

Here is a list that I wrote up while profiling a daemonized math conversion job today:

  • Think on moving core components into native C code, via XS wrappers, for a real performance impact. The philosophy that is starting to form in my mind is:

    high performance core with highly expressive periphery

    In other words, think of porting the most important core classes to C, but keep the less frequently invoked pieces in Perl (such as bindings, KeyVals, and other specialized code).

  • Perl's LibXML wrapper had some inefficiencies:

    • We should use _getAttribute() with an underscore when requesting attributes, as over 50% of the runtime of the user-facing getAttribute() is spent on figuring out if we are asking for an actual attribute or a namespace (vlaues that start with xmlns:). That overhead piles up since that method is called extremely often.
    • It is somewhat heart-breaking to see that the DESTROY method for XML::LibXML::Element takes a factor of ten longer to delete the Perl tiecache used for the wrapper, as it takes to free the C datastructure. No suggestion how to avoid that, but it's worth remarking (it's in the top subroutines reported by NYTProf).
      • In truth, it makes me wonder if it isn't sensible to rewrite LaTeXML::Core::Document and possibly LaTeXML::Common::XML in C and avoid using XML::LibXML altogether.
    • This is a good place to start getting scared of my suggestions :-)
  • The math profile can be slimmed down, e.g. by turning off the scan and crossref post-processors (they're currently enabled by default).

  • The vast majority of time for processing a single formula is naturally spent in the grammar, so we can consider switching to a XS-wrapped interface to a Marpa grammar of the same coverage. That grammar needs to be properly evaluated too, of course, so that we know a switch is sensible.

So, in the particular focus of making single formula conversion fast, we need to polish the performance of the main actors involved. Currently, profiling shows two of them are the main bottlenecks - the grammar and the document construction and manipulation. I will look into a Tikz conversion next, to contrast what would be vital there. Will also possibly profile an arXiv document. When I approach individual optimization tasks, I intend to open individual tickets (but not sooner).

And as always - discussion is more than welcome!

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions