From 6a410b5858ecb8b494eab7eed512ba314545f9e7 Mon Sep 17 00:00:00 2001 From: Kisaragi Hiu Date: Fri, 10 Feb 2023 00:55:27 +0900 Subject: [PATCH] Optimize fontification by avoiding recalculating a constant regexp regexp-opt can be slow because it also tries to optimize the regexp. For example: (regexp-opt (list "aaa" "abb")) ;; -> "\\(?:a\\(?:aa\\|bb\\)\\)" So it shouldn't be run every time a fontification happens. This does mean that when lisp-extra-font-lock-loop-keywords-with-var and lisp-extra-font-lock-loop-keywords are changed, the changes would only take effect after the file is reloaded. This shouldn't be a problem though, as these variables are not meant to be changed, and are unlikely to be changed anyways. --- lisp-extra-font-lock.el | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lisp-extra-font-lock.el b/lisp-extra-font-lock.el index 6561cea..bedba6b 100644 --- a/lisp-extra-font-lock.el +++ b/lisp-extra-font-lock.el @@ -577,6 +577,16 @@ the end of the quoted expression." "List of `cl-loop' named variable binding parameters.") +(defvar lisp-extra-font-lock--loop-keywords-regexp + (concat + "\\_<" + "\\(" + (regexp-opt (append + lisp-extra-font-lock-loop-keywords-with-var + lisp-extra-font-lock-loop-keywords)) + "\\)" + "\\_>")) + ;; Match named loop keywords, and (optionally) any bound variables. ;; ;; Note, does not support "destructuring", i.e. binding several @@ -589,14 +599,7 @@ the end of the quoted expression." (forward-comment (buffer-size)) (and (< (point) limit) (not (looking-at - (concat - "\\_<" - "\\(" - (regexp-opt (append - lisp-extra-font-lock-loop-keywords-with-var - lisp-extra-font-lock-loop-keywords)) - "\\)" - "\\_>"))))) + lisp-extra-font-lock--loop-keywords-regexp)))) (condition-case nil (forward-sexp) (error (goto-char limit))))