From mboxrd@z Thu Jan  1 00:00:00 1970
Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail
From: Stefan Monnier <monnier@iro.umontreal.ca>
Newsgroups: gmane.emacs.bugs
Subject: bug#36139: [PATCH] Make better use of the switch op in cond forms
Date: Tue, 18 Jun 2019 15:03:01 -0400
Message-ID: <jwvk1di7m93.fsf-monnier+emacs@gnu.org>
References: <68467ACF-DA49-4EBA-BA3B-7339DB22A456@acm.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226";
	logging-data="255663"; mail-complaints-to="usenet@blaine.gmane.org"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)
Cc: 36139@debbugs.gnu.org
To: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= <mattiase@acm.org>
Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Jun 18 21:04:45 2019
Return-path: <bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org>
Envelope-to: geb-bug-gnu-emacs@m.gmane.org
Original-Received: from lists.gnu.org ([209.51.188.17])
	by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
	(Exim 4.89)
	(envelope-from <bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org>)
	id 1hdJPO-0014MA-Fj
	for geb-bug-gnu-emacs@m.gmane.org; Tue, 18 Jun 2019 21:04:42 +0200
Original-Received: from localhost ([::1]:32954 helo=lists.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.86_2)
	(envelope-from <bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org>)
	id 1hdJPN-00030X-BE
	for geb-bug-gnu-emacs@m.gmane.org; Tue, 18 Jun 2019 15:04:41 -0400
Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36135)
 by lists.gnu.org with esmtp (Exim 4.86_2)
 (envelope-from <Debian-debbugs@debbugs.gnu.org>) id 1hdJOl-0002xb-Vz
 for bug-gnu-emacs@gnu.org; Tue, 18 Jun 2019 15:04:05 -0400
Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
 (envelope-from <Debian-debbugs@debbugs.gnu.org>) id 1hdJOk-00069n-PJ
 for bug-gnu-emacs@gnu.org; Tue, 18 Jun 2019 15:04:03 -0400
Original-Received: from debbugs.gnu.org ([209.51.188.43]:60297)
 by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16)
 (Exim 4.71) (envelope-from <Debian-debbugs@debbugs.gnu.org>)
 id 1hdJOk-00069J-Hc
 for bug-gnu-emacs@gnu.org; Tue, 18 Jun 2019 15:04:02 -0400
Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2)
 (envelope-from <Debian-debbugs@debbugs.gnu.org>) id 1hdJOk-0005kD-9Z
 for bug-gnu-emacs@gnu.org; Tue, 18 Jun 2019 15:04:02 -0400
X-Loop: help-debbugs@gnu.org
Resent-From: Stefan Monnier <monnier@iro.umontreal.ca>
Original-Sender: "Debbugs-submit" <debbugs-submit-bounces@debbugs.gnu.org>
Resent-CC: bug-gnu-emacs@gnu.org
Resent-Date: Tue, 18 Jun 2019 19:04:02 +0000
Resent-Message-ID: <handler.36139.B36139.156088459422010@debbugs.gnu.org>
Resent-Sender: help-debbugs@gnu.org
X-GNU-PR-Message: followup 36139
X-GNU-PR-Package: emacs
X-GNU-PR-Keywords: patch
Original-Received: via spool by 36139-submit@debbugs.gnu.org id=B36139.156088459422010
 (code B ref 36139); Tue, 18 Jun 2019 19:04:02 +0000
Original-Received: (at 36139) by debbugs.gnu.org; 18 Jun 2019 19:03:14 +0000
Original-Received: from localhost ([127.0.0.1]:45608 helo=debbugs.gnu.org)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <debbugs-submit-bounces@debbugs.gnu.org>)
 id 1hdJNy-0005iw-H1
 for submit@debbugs.gnu.org; Tue, 18 Jun 2019 15:03:14 -0400
Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:46764)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <monnier@iro.umontreal.ca>) id 1hdJNw-0005ij-F5
 for 36139@debbugs.gnu.org; Tue, 18 Jun 2019 15:03:12 -0400
Original-Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1])
 by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 930D881171;
 Tue, 18 Jun 2019 15:03:05 -0400 (EDT)
Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1])
 by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 4B43980E0B;
 Tue, 18 Jun 2019 15:03:04 -0400 (EDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca;
 s=mail; t=1560884584;
 bh=6a9pQncjOrlLrf37c4fj1Ic64Md5BXAMUKz4jjvynQ0=;
 h=From:To:Cc:Subject:References:Date:In-Reply-To:From;
 b=gsNTwUWDSvRn73zi2VHT9LkYvDI4fCZoKSgDuLuYAL7Cgw2uygzqGBPrVOZ8zpEkx
 YUxsGR9PjiW6g5lqHMlbP0OCrLml9vCpIOqdW1keW6hULIwEYfNN1qEL+pueTd64Vu
 SUacUfrHrmwqYbkWtXTKvgOefGp9OqQNr0fv8nJaSfD4P3/zSUDVvZJdZBbS+DW2Vq
 SFiLZ/ZTdtlH40Af1r1gkS38DTZHM3hLMM19sE9tVF+KQMvqo9DE70KFtRuZiEe4vG
 An5tcWSbxtqFOCUPBxIxdqTiQ8lZLA+qEIyMCOmOuYS1r/wtzn+W636OIPX6sn+l6V
 hhO25dPZBaBwA==
Original-Received: from alfajor (unknown [157.52.10.58])
 by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 7BDC51203AB;
 Tue, 18 Jun 2019 15:03:03 -0400 (EDT)
In-Reply-To: <68467ACF-DA49-4EBA-BA3B-7339DB22A456@acm.org> ("Mattias
 \=\?windows-1252\?Q\?Engdeg\=E5rd\=22's\?\= message of "Sat,
 8 Jun 2019 16:40:07 +0200")
X-BeenThere: debbugs-submit@debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]
X-Received-From: 209.51.188.43
X-BeenThere: bug-gnu-emacs@gnu.org
List-Id: "Bug reports for GNU Emacs,
 the Swiss army knife of text editors" <bug-gnu-emacs.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/bug-gnu-emacs>,
 <mailto:bug-gnu-emacs-request@gnu.org?subject=unsubscribe>
List-Archive: <https://lists.gnu.org/archive/html/bug-gnu-emacs>
List-Post: <mailto:bug-gnu-emacs@gnu.org>
List-Help: <mailto:bug-gnu-emacs-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/bug-gnu-emacs>,
 <mailto:bug-gnu-emacs-request@gnu.org?subject=subscribe>
Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org
Original-Sender: "bug-gnu-emacs"
 <bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org>
Xref: news.gmane.org gmane.emacs.bugs:160793
Archived-At: <http://permalink.gmane.org/gmane.emacs.bugs/160793>

> * 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