emacs-lsp / lsp-mode

Emacs client/library for the Language Server Protocol
https://emacs-lsp.github.io/lsp-mode
GNU General Public License v3.0
4.8k stars 892 forks source link

lsp--fontlock-with-mode is slow with treesit #4358

Open scturtle opened 8 months ago

scturtle commented 8 months ago

Thank you for the bug report

Bug description

I've noticed that the lsp--fontlock-with-mode function seems to be slow when the built-in tree-sitter support (treesit) is automatically enabled. I suspect this is because the .so library is being loaded on every call. For instance, profiling shows it takes around 0.16 seconds to execute:

lsp--fontlock-with-mode 0.163621 s
lsp--fontlock-with-mode 0.162615 s
lsp--fontlock-with-mode 0.163193 s
lsp--fontlock-with-mode 0.163004 s
lsp--fontlock-with-mode 0.162281 s

But with treesit turned off, it only takes 0.002 seconds:

lsp--fontlock-with-mode 0.002005 s
lsp--fontlock-with-mode 0.002601 s
lsp--fontlock-with-mode 0.002055 s
lsp--fontlock-with-mode 0.002191 s
lsp--fontlock-with-mode 0.002485 s

Steps to reproduce

(let ((t1 (current-time)))
  (with-temp-buffer
    (c++-ts-mode)
    (font-lock-ensure))
  (float-time (time-since t1)))

Expected behavior

Fast hover as before with treesit.

Which Language Server did you use?

C++

OS

Linux

Error callstack

No response

Anything else?

My workaround:

;; HACK: `lsp--fontlock-with-mode' is SLOW with treesit
(advice-add #'lsp--render-string :filter-args
            (lambda (args) (if (string= (cadr args) "markdown") args
                             (list (concat "```" (cadr args) "\n" (car args) "\n" "```") "markdown"))))