Syntax highlighting is SLOW
pyrocat101 opened this issue · 3 comments
This tool is pretty slow when I tried piping a large diff to it, taking nearly 4 seconds. So I profiled it with perf
and found that a lot of time is spent in the WASM library (https://github.com/NeekSandhu/onigasm) indirectly used by https://github.com/shikijs/shiki/.
If I comment out the usage of highlightSyntaxInLine
, then it takes less than 500ms to display the previous diff. This is the perf report:
Have you considered:
- Provide CLI flag to turn off the syntax highlighter (right now it seems to be tied to the theme setting).
- Use a more performant syntax highlighter implementation. Anecdotally, WASM is much slower than the native, and the call between JS and WASM is also extremely expensive. This worsens when you only call the highlighter one line at a time.
For comparison, https://github.com/dandavison/delta uses https://github.com/trishume/syntect and is very fast.
Hey linjie! Yeah I went down the same path in shikijs/shiki#151, which should speed it up a little (~30% for me). Also see https://github.com/banga/git-split-diffs#performance.
Provide CLI flag to turn off the syntax highlighter (right now it seems to be tied to the theme setting).
You can override the theme setting to disable it, see https://github.com/banga/git-split-diffs#syntax-highlighting
Use a more performant syntax highlighter implementation. Anecdotally, WASM is much slower than the native, and the call between JS and WASM is also extremely expensive. This worsens when you only call the highlighter one line at a time.
I didn't do a lot of research into this, but onigasm
claims to be only 2x slower than the native version, which didn't seem like a lot. I also didn't go with syntect etc. because I wanted something that uses the same textmate grammars as vscode so I could use themes that looked consistent with that. https://github.com/shikijs/shiki (which wraps vscode-textmate & onigasm) made this trivially easy, so I didn't spend much more time beyond that. There's also more low hanging fruit there to speed up before trying to cut out WASM – a substantial amount of time is spent in parsing the textmate grammar/theme files.
I didn't do a lot of research into this, but onigasm claims to be only 2x slower than the native version, which didn't seem like a lot.
As I mentioned here that the call into WASM can be pretty expensive if each of them is only doing a little work. Have you experimented with batching more content for the highlighter?
Ohh got it. I haven’t, will look into it. It’s a little tricky because buffering will delay the initial render which is when the delay is most noticeable, but it’s probably not that much compared to the wasm overhead