macteo / Marklight

Markdown syntax highlighter for iOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

improve performance / energy consumption

DivineDominion opened this issue · comments

I notices that no matter what you type, the energy consumption gets pretty high quickly and stays high. I bet that's because of the quadrillion regular expressions that are executed.

Then I had the following idea, from easy to accurate:

  1. Do not trigger re-styling unless a special character has been typed (from a set of \*_#`), text has been pasted, or something has been deleted
  2. Add a state machine with a state changing according to what has been typed; it can thus have awareness of "I'm inside a code block, no need to check for bold/italic styling"

Any additional ideas?

Another approach: Marklight.processEditing does not take the whole textStorage.string for scanning, but a substring of potentially affected paragraphs. I don't think that'll help a lot since the regex matching uses a sub-range already.

While refactoring, another idea came to mind. There's block styles, and there's inline/span styles. Just like TextMate, we could match the blocks first, and then inside the blocks apply inline styles as needed. For example: apply no inline styles inside code blocks.

Might also save a few cycles to process the current line + surrounding lines as blocks, not the whole document, and then kick of inline styling from there.

Edge case to take note of: opening a code block with ``` should result in the whole rest of the document to be part of the code. So in this case, and probably others, we need to more greedily style the whole document.