1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-23 22:20:24 -08:00

pcase.el (\`): Try and handle large patterns better

Large backquote patterns tend to lead to very large and deeply
nested expansions, but they also tend to contain a lot of
"constant" subpatterns that can be compiled to quote patterns.
This patch does just that.  See discussion at
https://lists.gnu.org/archive/html/emacs-devel/2024-05/msg01140.html

* lisp/emacs-lisp/pcase.el (pcase--split-pred):
Improve the handling of pred-vs-quote so it also works with
quoted objects like cons cells, vectors, and strings.
Simplify the `pcase--mutually-exclusive-p` branch accordingly.
(pcase--expand-\`): New function, extracted from the \` pcase macro.
Make it recurse internally, and optimize backquote patterns to `quote`
patterns where possible.
(\`): Use it.

* test/lisp/emacs-lisp/pcase-tests.el (pcase-tests-vectors): Add tests
that were broken by a more naïve version of the optimization.
(pcase-tests-quote-optimization): New test.
This commit is contained in:
Stefan Monnier 2024-06-03 13:26:10 -04:00
parent eb9afd558e
commit 16fc5b6c0c
2 changed files with 41 additions and 23 deletions

View file

@ -73,7 +73,17 @@
(should-not (pcase-tests-grep 'member exp))))
(ert-deftest pcase-tests-vectors ()
(should (equal (pcase [1 2] (`[,x] 1) (`[,x ,y] (+ x y))) 3)))
(should (equal (pcase [1 2] (`[,x] 1) (`[,x ,y] (+ x y))) 3))
(should (pcase [1 2] (`[1 ,'2] t)))
(should (pcase '(1 2) (`(1 ,'2) t))))
(ert-deftest pcase-tests-quote-optimization ()
;; FIXME: We could/should also test that we get a corresponding
;; "shadowed branch" warning.
(should-not (pcase-tests-grep
'FOO (macroexpand '(pcase EXP
(`(,_ . ,_) (BAR))
('(a b) (FOO)))))))
(ert-deftest pcase-tests-bug14773 ()
(let ((f (lambda (x)