Fix #4093: runaway gc-cons-threshold in lsp-mode

gc-cons-threshold was doubled every time an lsp-mode buffer was
opened (i.e. every time +lsp-optimization-mode was activated) causing
heavy freezes.
This commit is contained in:
Henrik Lissner 2020-10-15 02:18:00 -04:00
parent d85c7b857b
commit 1c7fd9c220
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -14,6 +14,7 @@ killing and opening many LSP/eglot-powered buffers.")
(defvar +lsp--default-read-process-output-max nil)
(defvar +lsp--default-gcmh-high-cons-threshold nil)
(defvar +lsp--optimization-init-p nil)
(define-minor-mode +lsp-optimization-mode
"Deploys universal GC and IPC optimizations for `lsp-mode' and `eglot'."
@ -21,21 +22,25 @@ killing and opening many LSP/eglot-powered buffers.")
:init-value nil
(if (not +lsp-optimization-mode)
(setq-default read-process-output-max +lsp--default-read-process-output-max
gcmh-high-cons-threshold +lsp--default-gcmh-high-cons-threshold)
(setq +lsp--default-read-process-output-max
(default-value 'read-process-output-max)
+lsp--default-gcmh-high-cons-threshold
(default-value 'gcmh-high-cons-threshold))
;; `read-process-output-max' is only available on recent development
;; builds of Emacs 27 and above.
(setq-default read-process-output-max (* 1024 1024))
;; REVIEW LSP causes a lot of allocations, with or without Emacs 27+'s
;; native JSON library, so we up the GC threshold to stave off
;; GC-induced slowdowns/freezes. Doom uses `gcmh' to enforce its
;; GC strategy, so we modify its variables rather than
;; `gc-cons-threshold' directly.
(setq-default gcmh-high-cons-threshold (* 2 +lsp--default-gcmh-high-cons-threshold)))
(gcmh-set-high-threshold))
gcmh-high-cons-threshold +lsp--default-gcmh-high-cons-threshold
+lsp--optimization-init-p nil)
;; Only apply these settings once!
(unless +lsp--optimization-init-p
(setq +lsp--default-read-process-output-max
(default-value 'read-process-output-max)
+lsp--default-gcmh-high-cons-threshold
(default-value 'gcmh-high-cons-threshold))
;; `read-process-output-max' is only available on recent development
;; builds of Emacs 27 and above.
(setq-default read-process-output-max (* 1024 1024))
;; REVIEW LSP causes a lot of allocations, with or without Emacs 27+'s
;; native JSON library, so we up the GC threshold to stave off
;; GC-induced slowdowns/freezes. Doom uses `gcmh' to enforce its
;; GC strategy, so we modify its variables rather than
;; `gc-cons-threshold' directly.
(setq-default gcmh-high-cons-threshold (* 2 +lsp--default-gcmh-high-cons-threshold))
(gcmh-set-high-threshold)
(setq +lsp--optimization-init-p t))))
;;