I've had slow text insertion for a while in my emacs setup and I finally traced it back to smartparens. For example, with smartparens-latex loaded, inserting any text is super slow in some documents when smartparens-mode is on
Steps to reproduce the problem
Make sure smartparens-latex is loaded and smartparens-mode is on and simply insert any text almost anywhere in a large latex document.
The reason for the slow behavior is that many latex specific parentheses call sp-in-math-p which calls texmathp which can be slow in certain cases.
A simple solution that I implemented is to change the order in which sp--all-pairs-to-insert checks valid parentheses so that only matching pairs are action-checked. A possible implementation follows:
(defun sp--all-pairs-to-insert (&optional looking-fn action)
"Return all pairs that can be inserted at point.
..."
(setq looking-fn (or looking-fn 'sp--looking-back-p))
(setq action (or action 'insert))
(-if-let (trigs
(--filter (and
(plist-get it :trigger)
(funcall looking-fn (sp--strict-regexp-quote (plist-get it :trigger)))
(sp--do-action-p (plist-get it :open) action))
sp-local-pairs))
(cons :trigger trigs)
(cons :open
(--filter (and
(funcall looking-fn (sp--strict-regexp-quote (plist-get it :open)))
(sp--do-action-p (plist-get it :open) action))
sp-local-pairs))))
This already improves the performance of smartparen considerably (For example texmathp is called only once per insertion) and more performance can be gained by making sp--strict-regexp-quote faster by caching the regular-expression instead of reconstructing on the fly in sp--strict-regexp-quote.
I am happy to do a pull-request if this solution is suitable. Similar changes are also worth doing in sp--pair-to-wrap.
Environment & version information
smartparens version: 8b6a3c3
Active major-mode: emacs-lisp-mode
Smartparens strict mode: nil
Emacs version (M-x emacs-version): GNU Emacs 28.1 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.24.33, cairo version 1.16.0) of 2022-09-20
Actual behavior
I've had slow text insertion for a while in my emacs setup and I finally traced it back to smartparens. For example, with
smartparens-latex
loaded, inserting any text is super slow in some documents whensmartparens-mode
is onSteps to reproduce the problem
Make sure
smartparens-latex
is loaded andsmartparens-mode
is on and simply insert any text almost anywhere in a large latex document.Profiler
Here's a profiler output
Reason and Solution
The reason for the slow behavior is that many latex specific parentheses call
sp-in-math-p
which callstexmathp
which can be slow in certain cases.A simple solution that I implemented is to change the order in which
sp--all-pairs-to-insert
checks valid parentheses so that only matching pairs are action-checked. A possible implementation follows:This already improves the performance of smartparen considerably (For example
texmathp
is called only once per insertion) and more performance can be gained by makingsp--strict-regexp-quote
faster by caching the regular-expression instead of reconstructing on the fly insp--strict-regexp-quote
.I am happy to do a pull-request if this solution is suitable. Similar changes are also worth doing in
sp--pair-to-wrap
.Environment & version information
smartparens
version: 8b6a3c3major-mode
:emacs-lisp-mode
M-x emacs-version
): GNU Emacs 28.1 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.24.33, cairo version 1.16.0) of 2022-09-20