Skip to content

Fix UI lag: revert markdown lexer to 0208d7a (Issue #1314) - #1329

Open
4d30 wants to merge 1 commit into
martanne:masterfrom
4d30:fix-ui-lag-in-md
Open

Fix UI lag: revert markdown lexer to 0208d7a (Issue #1314)#1329
4d30 wants to merge 1 commit into
martanne:masterfrom
4d30:fix-ui-lag-in-md

Conversation

@4d30

@4d30 4d30 commented Apr 3, 2026

Copy link
Copy Markdown

Title

Fix UI lag in markdown lexer by reverting to 0208d7a (Fixes #1314)

Description

Upon investigation, it was determined that changes made to the markdown lexer /lua/lexers/markdown.lua after commit 0208d7a introduced nested loops during the highlighting process. This increased lexing complexity, causing the UI thread to hang or stutter when scrolling through or editing markdown files.

Proposed Change

  • Revert lua/lexers/markdown.lua to 0208d7a

@4d30
4d30 marked this pull request as ready for review April 3, 2026 11:44

@rnpnr rnpnr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are certainly other things we could be doing so that this is not necessary. I will discuss the code line change with upstream though.

Interestingly when testing I couldn't recreate the issue because my installed vis uses a statically linked LPEG. It seems that embedding LPEG directly in vis provides a much more significant performance boost than I would normally expect. It may be worth making that the default when lua is enabled.

Comment thread lua/lexers/markdown.lua
Comment on lines +43 to +47
lex:add_rule('blockquote',
lex:tag(lexer.STRING, lpeg.Cmt(lexer.starts_line('>', true), function(input, index)
local _, e = input:find('\n[ \t]*\r?\n', index) -- the next blank line (possibly with indentation)
return (e or #input) + 1
end)))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can only perform worse. It does more than the existing line.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disagree, see benchmark in #1314. Reverting decreases runtime by 96%.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You didn't run the benchmark with only this change. The difference from this hunk alone is insignificant.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Working from a functional state like 0208d7a and then benchmarking and improving from there would be a productive path.

Comment thread lua/lexers/markdown.lua

local FOLD_HEADER, FOLD_BASE = lexer.FOLD_HEADER, lexer.FOLD_BASE
-- Fold '#' headers.
function lex:fold(text, start_line, start_level)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't support folding so there is no reason to diverge from upstream here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, love to cut out what's not required.

Comment thread lua/lexers/markdown.lua
'\n' * hspace^0 * '```' * hspace^0 * ('\n' + P(-1))) +
lexer.range(lexer.starts_line('~~~', true), '\n' * hspace^0 * '~~~' * hspace^0 * ('\n' + P(-1)))

local code_line = lexer.starts_line((B(' ') + B('\t')) * lexer.to_eol(), true)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be the only relevant change.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not sure what this means, would you please clarify? Do you mean only the removal of 68-70 and the addion of 32?

When I diff the files, I see a significant addition added in 16e31ce lines 32-70.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't mean to be unclear. What I meant was that changing code_line rule back to its old form was the only part that was meaningfully changing performance. That is why I pointed out to upstream that it shouldn't have been running on every line and it was fixed. But as shown by your benchmark that was not the only issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

100% cpu spikes and slow response with md syntax on

2 participants