[vim/vim] Decrease regex-based syntax-highlighting time (PR #21166)

5 views
Skip to first unread message

Julien Voisin

unread,
Aug 27, 2026, 11:28:25 AM (5 days ago) Aug 27
to vim/vim, Subscribed

Problem: While advancing the syntax highlighting, store_current_state()
calls syn_stack_find_entry() about once per parsed line, and
that function rescans the state-cache list from its head every
time. With the list capped at 1000 entries this linear rescan
dominates the cost of highlighting a large file.
Solution: Keep a finger to the entry last located in the list and resume
the scan from it when it is at or before the wanted line,
advancing the finger as new states are stored.

The list is kept sorted by line number, so when the finger sits at or before the wanted line the answer can only follow it, never precede it; resuming from the finger therefore returns the very same entry as a scan from the head. The "at or before" guard makes the shortcut safe for any lookup order: a backward or random-access lookup whose finger is past the wanted line simply falls back to a full scan. The finger is cleared whenever an entry is freed, the array is reallocated, or the block is freed, so it can never dangle.

Parsing the syntax of a 20000-line C file is about a third faster a 5000-line file about a quarter faster, benchmarked via something like this:

call synID(1, 1, 1)              " warm-up
let s = reltime()
for l in range(1, line('$')) | call synID(l, 1, 1) | endfor
call writefile([reltimefloat(reltime(s))], $T)

You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/21166

Commit Summary

  • 316f729 Decrease regex-based syntax-highlighting time

File Changes

(3 files)

Patch Links:


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/21166@github.com>

Reply all
Reply to author
Forward
0 new messages