Subject: bug#36139: [PATCH] Make better use of the switch op in cond forms

From: Stefan Monnier <[email-masked]>
Date: Tue, 18 Jun 2019 15:03:01 -0400

> * lisp/emacs-lisp/pcase.el (pcase--u1):
> Use the most specific of `memq', `memql' and `member' in or-patterns
> with constant cases.  This improves performance and may help the byte-code
> compiler generate a switch.
> * test/lisp/emacs-lisp/pcase-tests.el (pcase-tests-member):
> Add mixed-type or-pattern test cases.
> ---
>  lisp/emacs-lisp/pcase.el            | 15 ++++++++-------
>  test/lisp/emacs-lisp/pcase-tests.el |  6 ++++--
>  2 files changed, 12 insertions(+), 9 deletions(-)
>
> diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el
> index a644453a94..ae2cf8eb02 100644
> --- a/lisp/emacs-lisp/pcase.el
> +++ b/lisp/emacs-lisp/pcase.el
> @@ -785,25 +785,26 @@ pcase--u1
>     ((eq 'or (caar matches))
>      (let* ((alts (cdar matches))
>             (var (if (eq (caar alts) 'match) (cadr (car alts))))
> -           (simples '()) (others '()) (memql-ok t))
> +           (simples '()) (others '()) (mem-fun 'memq))
>        (when var
>          (dolist (alt alts)
>            (if (and (eq (car alt) 'match) (eq var (cadr alt))
>                     (let ((upat (cddr alt)))
>                       (eq (car-safe upat) 'quote)))
>                (let ((val (cadr (cddr alt))))
> -                (unless (or (integerp val) (symbolp val))
> -                  (setq memql-ok nil))
> -                (push (cadr (cddr alt)) simples))
> +                (cond ((integerp val)
> +                       (when (eq mem-fun 'memq)
> +                         (setq mem-fun 'memql)))
> +                      ((not (symbolp val))
> +                       (setq mem-fun 'member)))
> +                (push val simples))
>              (push alt others))))
>        (cond
>         ((null alts) (error "Please avoid it") (pcase--u rest))
>         ;; Yes, we can use `memql' (or `member')!
>         ((> (length simples) 1)
>          (pcase--u1 (cons `(match ,var
> -                                 . (pred (pcase--flip
> -                                          ,(if memql-ok #'memql #'member)
> -                                          ',simples)))
> +                                 . (pred (pcase--flip ,mem-fun ',simples)))
>                           (cdr matches))
>                     code vars
>                     (if (null others) rest

LGTM.  The other direction is to just always use `member`
and speed up the implementation of `member` by testing the type of
the first arg and dispatch to memq/memql when possible.

        Stefan