Skip to content

First pass performance #14

@cben

Description

@cben

[migrated from cben/mathdown#19]

The initial render on document load is horribly unacceptably slow (saw 1 minute on a math-heavy page on mathdown.net).
The unrendered text does show very fast but it's not very useful since the editor is paralyzed until all math is rendered.


Verified that loading MathJax from the network is not the blocker; it's the rendering itself.


Pasting same text into http://cben.github.io/CodeMirror-MathJax/demo.html typesets much faster (~10s instead of ~60s).
Sounds like using CM of fixed size helps a lot.
[EDIT: Indeed, switching Mathdown to fixed size helped a lot https://github.com/cben/mathdown/commit/bc6c5c935905cfa012df718666018416a93b9715]


Some rough measurements on a heavy doc:

  • scanning text for the regexp is nearly free (~0.1s)
  • typesetting without markText is not bad - 8s, much of which is due to loading mathjax extensions (=> Funny highlight for header inside quote #21).
    • same with markText without replacedWith! invisible markers are nearly free.
  • everything is at least 2× slower due to logging with an open JS console.
  • full typesetting takes 62s.
    • Chrome profiling suggests a lot of time is spent in visibleLines, (via endOperation←updateDisplay←markText).
      Indeed text gradually shifts vertically as math is typeset. => Should try keeping view/cursor on top instead of bottom.
      Naive attempts to wrap with cm.operation don't help because post-Typeset callbacks come one by one.
  • tried an incremental approach => 61s. no loss, no gain. Still makes editor barely responsive.
    I guess CM's input polling doesn't get much chance to run.

next steps:

  • Queue post-Typeset callbacks and batch via cm.operation.
    • tune batching vs responsiveness on small edits. Being aware of how many have been queued for mathjax typesetting.
  • Learn how to run when CM is idle ("update" event?).
  • On-demand rendering prioritizing math on viewport. (cf. [meta-issue] Stateful parsing #13)?

P.S. Relevant existing bug:

  • Don't mess when text shifts around between asking MJ to typeset and callback doing markText(). Hard to trigger now but changes above will make this important.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions