From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Newsgroups: gmane.emacs.bugs Subject: bug#58839: [Patch] Re: bug#58839: 29.0.50; project-kill-buffer fails when Eglot is running Date: Tue, 1 Nov 2022 13:37:58 +0000 Message-ID: References: <87sfj8umwb.fsf@posteo.net> <213f3549-de4e-25a7-5e27-d13893e557bc@yandex.ru> <87zgdfwkle.fsf@gmail.com> <8e31a89d-e35e-6dd0-a8e3-f0b9684c8bfa@yandex.ru> <87v8o3wgq1.fsf@gmail.com> <87ilk2x1si.fsf@gmail.com> <871qqq7l9p.fsf@posteo.net> <87eduqwekz.fsf@gmail.com> <87wn8invbx.fsf@posteo.net> <877d0iw8iq.fsf@gmail.com> <837d0hhlke.fsf@gnu.org> <46ff0065-5645-ef1e-2621-242fb6a73f98@yandex.ru> <87v8o0uxn5.fsf@gmail.com> <787a4362-7ff5-7dbb-9118-16e4bee5f328@yandex.ru> <87edunvhf2.fsf@gmail.com> <6d4d9e72-1bae-4d64-b7c1-c2b9c11e396f@yandex.ru> <87tu3jgdbv.fsf@posteo.net> <87h6zihq3q.fsf@posteo.net> <877d0ehlnb.fsf@posteo.net> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000949d9f05ec68d1db" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="634"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , manuel.uberti@inventati.org, 58839@debbugs.gnu.org, Dmitry Gutov To: Philip Kaludercic Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Nov 01 15:37:43 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1opsOc-000AUA-AX for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 01 Nov 2022 15:37:42 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oprSt-0002da-EL; Tue, 01 Nov 2022 09:38:03 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oprSs-0002dT-L3 for bug-gnu-emacs@gnu.org; Tue, 01 Nov 2022 09:38:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oprSs-00041J-6N for bug-gnu-emacs@gnu.org; Tue, 01 Nov 2022 09:38:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oprSr-0002Ou-QB for bug-gnu-emacs@gnu.org; Tue, 01 Nov 2022 09:38:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 01 Nov 2022 13:38:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 58839 X-GNU-PR-Package: emacs Original-Received: via spool by 58839-submit@debbugs.gnu.org id=B58839.16673098299166 (code B ref 58839); Tue, 01 Nov 2022 13:38:01 +0000 Original-Received: (at 58839) by debbugs.gnu.org; 1 Nov 2022 13:37:09 +0000 Original-Received: from localhost ([127.0.0.1]:42952 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oprS0-0002Nl-QQ for submit@debbugs.gnu.org; Tue, 01 Nov 2022 09:37:09 -0400 Original-Received: from mail-oo1-f54.google.com ([209.85.161.54]:42842) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oprRy-0002NE-Fz for 58839@debbugs.gnu.org; Tue, 01 Nov 2022 09:37:07 -0400 Original-Received: by mail-oo1-f54.google.com with SMTP id r15-20020a4abf0f000000b004761c7e6be1so2059109oop.9 for <58839@debbugs.gnu.org>; Tue, 01 Nov 2022 06:37:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=8HwEpsuoNpnl+CBW7x367l71OL2U0dWXqTtQidV6DdM=; b=eLYCzpky9vSFTl2w2/x1tc/DlXrN9vzL6E4ksMfLIZsj4tyiqYAQyScDCB0vVXRLi5 +foxMIvddvalGQioMQ/yZCJaRMbaoGcMvPBSWJH5zh/FeFMNX2+D53QwtZNHvrRDkPDe otMOZ9Ks+1rP9V+u6dfosO+avuili+3EUi4Ey0kAEzEIuVq69rawR8SyOS9kBi0jd4R2 mfTvL64FuIiPeMhxoVH+0OXeJee5qwDut8dfOl8KykMBVVng6w1R+iDQkv3ujv7YbXTJ sqgIiq4A3MJaqql/C8L/CanXNCohFcCxK/YXE3/ND8xjr4P+PLwpsrrrjLM6iTsDtSTb ErUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8HwEpsuoNpnl+CBW7x367l71OL2U0dWXqTtQidV6DdM=; b=crT0hggrXeDN00ra6POFNwQF9IMgTuff7S+Bn+Qz0IztPXwas713GGyTOKL6/03iGg 4Lj4O2Qpc7z3zdhuW8H5266B0jaL+Pb6g+FGSnOFxubJHqul2dgeGgwyammHpuwRNJvq 2VdMh0YB7Jk9/Hq8Y8ZxpTdZAsOvT2KMPrhPWClFs6jgl4tl4kqiEI7Afc4ch73ej061 9WmFQ+Xe3AQsbZNg54lLfkcLpA3IaVQLjKFm2OAXywe2CyNTYEWj1ERxmkQeILyfPrDW fl+/QO63QxhIUncC1B6znfZLPGNqhFIJtUfIao/7cAGccQRV6TF2y/LVo5c75/n2Gl4S 6wmA== X-Gm-Message-State: ACrzQf1m2U0Dt1nRVYgyfVN6/G1uW6Su7Bclwl7ZS9T4APIBYn5CTqq5 bg62Gu5WIuVSIHbyP7IifXa9N2u7IuCQ0Nfpvj8= X-Google-Smtp-Source: AMsMyM7G1tSUUpctpfnDfj+fKi/5XQ27jExVbXSYJwTs+zZlRtTfAgM7eXAtMNq3xOALXqhdHK14lNX9fxrjOBFIdxE= X-Received: by 2002:a4a:7:0:b0:495:f4a4:4bdc with SMTP id 7-20020a4a0007000000b00495f4a44bdcmr8047669ooh.41.1667309820483; Tue, 01 Nov 2022 06:37:00 -0700 (PDT) In-Reply-To: <877d0ehlnb.fsf@posteo.net> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: "bug-gnu-emacs" Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:246773 Archived-At: --000000000000949d9f05ec68d1db Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I haven't studied your code in depth, but it seems like you're giving `match-buffers/compiled` benchmark 10 times the work you're giving to the other function, which would explain why it's 10x slower. The byte-compiler (or the native compiler) can't really optimize the mini-language more magically. It can only optimize elisp. My idea of using the byte-compiler to do this is different: it entails translating the mini-language to elisp first and then byte-compiling that. But it is a technique that I think your code isn't applying or at least not correctly (though I haven't read all of it: I will soon). You can see eglot's "glob matching" section for the application of such a technique the "glob" minilanguage that is required by LSP (iow it wasn't "invented by me" ;-) ) Jo=C3=A3o On Tue, Nov 1, 2022 at 1:03 PM Philip Kaludercic wrote= : > Jo=C3=A3o T=C3=A1vora writes: > > > On Tue, Nov 1, 2022 at 11:27 AM Philip Kaludercic > > wrote: > > > > > >> E.g. `display-buffer-alist' makes use of it to associate display-buffe= r > >> rules with buffers. Now you can add > >> > >> ((major-mode . help-mode) display-buffer-in-side-window) > >> > >> instead of trying to match being a regular expression to catch all > >> *Help* buffer names of a function along the lines of > >> > >> (lambda (buf _alist) > >> (with-current-buffer buf > >> (derived-mode-p 'help-mode))) > >> > > > > If you really want to save up on this typing, it's better to define > > a reusable helper function, or even a higher order function. > > > > (defun buffer-mode-matcher (mode) > > (lambda (b _alist) > > (with-current-buffer b (derived-mode-p 'help-mode)))) > > > > You can add buffer-mode-matcher to the library if it becomes > > useful enough. Then you add: > > > > `(,(buffer-mode-matcher 'help-mode) display-buffer-in-side-window) > > > > to display-buffer-alist. > > > > But if you really want a new language your language, then I suggest > > a simple adapter buffer-matcher utility that merges the two. That way > one > > doesn't couple existing utilities to the new mini-language and > > simultaneously > > the new mini-language become useful in a much wider setting for those w= ho > > appreciate such things. > > > > (defun buffer-matcher (condition) > > "Return unary predicate of a buffer matching the CONDITION > > mini-language." > > (lambda (buf &rest _whatever) ; make it even more lax > > (buffer-match-p condition))) > > > > Later on, you might even pass an (... &optional compiled) so that the > > return value > > is syntax checked and optimized in some way at compile time. > > > > IOW, (E)Lisp already gives you the tools for these composition without > > needing to invent new languages with the drawbacks I listed. > > I was curious to try this out, and implemented something along the lines > of your suggestion. The bad news is that it is at least 10 times slower > than the current implementation, that isn't even really optimised. > Perhaps I did something native and didn't see what is wrong, but here > are my notes: > > --8<---------------cut here---------------start------------->8--- > (defun translate-buffer-condition (condition) > "Compile a CONDITION into a predicate function." > (pcase-exhaustive condition > ((or 't 'nil) > (lambda (_buffer _arg) > condition)) > ((pred stringp) > (lambda (buffer _arg) > (string-match-p condition (buffer-name buffer)))) > ((pred functionp) > (if (eq 1 (cdr (func-arity condition))) > (lambda (buffer _arg) > (funcall condition buffer)) > condition)) > (`(major-mode . ,mode) > (lambda (buffer _arg) > (eq > (buffer-local-value 'major-mode buffer) > mode))) > (`(derived-mode . ,mode) > (lambda (buffer _arg) > (provided-mode-derived-p > (buffer-local-value 'major-mode buffer) > mode))) > (`(not . ,cond) > (lambda (buffer arg) > (not (funcall (translate-buffer-condition cond) buffer arg)))) > (`(or . ,conds) > (lambda (buffer arg) > (catch 'match > (dolist (cond conds) > (when (funcall (translate-buffer-condition cond) buffer arg) > (throw 'match t)))))) > (`(and . ,conds) > (lambda (buffer arg) > (catch 'match > (dolist (cond conds t) > (when (funcall (translate-buffer-condition cond) buffer arg) > (throw 'match nil)))))))) > > (defvar buffer-match-p-cache (make-hash-table :test 'eq)) > > (defun buffer-match-p/compiled (condition buffer-or-name &optional arg) > "Return non-nil if BUFFER-OR-NAME matches CONDITION. > CONDITION is either: > - the symbol t, to always match, > - the symbol nil, which never matches, > - a regular expression, to match a buffer name, > - a predicate function that takes a buffer object and ARG as > arguments, and returns non-nil if the buffer matches, > - a cons-cell, where the car describes how to interpret the cdr. > The car can be one of the following: > * `derived-mode': the buffer matches if the buffer's major mode > is derived from the major mode in the cons-cell's cdr. > * `major-mode': the buffer matches if the buffer's major mode > is eq to the cons-cell's cdr. Prefer using `derived-mode' > instead when both can work. > * `not': the cdr is interpreted as a negation of a condition. > * `and': the cdr is a list of recursive conditions, that all have > to be met. > * `or': the cdr is a list of recursive condition, of which at > least one has to be met." > (funcall (or (gethash condition buffer-match-p-cache) > (puthash condition > (byte-compile (translate-buffer-condition > condition)) > buffer-match-p-cache)) > (get-buffer buffer-or-name) > arg)) > > (defun match-buffers/compiled (condition &optional buffers arg) > "Return a list of buffers that match CONDITION. > See `buffer-match' for details on CONDITION. By default all > buffers are checked, this can be restricted by passing an > optional argument BUFFERS, set to a list of buffers to check. > ARG is passed to `buffer-match', for predicate conditions in > CONDITION." > (let (bufs) > (dolist (buf (or buffers (buffer-list))) > (when (buffer-match-p/compiled condition (get-buffer buf) arg) > (push buf bufs))) > bufs)) > > ;; Here we will test a moderately complicated condition and time how > ;; long it takes with the current implementation and with the proposed > ;; alternative. > > (defvar sample-condition > '(and (or buffer-file-name > (derived-mode . compilation-mode) > (derived-mode . dired-mode) > (derived-mode . diff-mode) > (derived-mode . comint-mode) > (derived-mode . eshell-mode) > (derived-mode . change-log-mode)) > "\\*.+\\*" > (not . "\\` "))) > > (benchmark-run 100 > (match-buffers sample-condition pr)) > ;; =3D> (1.7045469830000002 20 1.1418286690000023) > > > (benchmark-run 1000 > (match-buffers/compiled project-buffer-conditions pr)) > ;; =3D> (17.646938126000002 219 12.428946030999999) > --8<---------------cut here---------------end--------------->8--- > > I guess this just goes to show that one shouldn't underestimate the cost > of a function call... > > LISP programmers know the value of everything and the cost of nothing= . > -- Alan Perlis > --=20 Jo=C3=A3o T=C3=A1vora --000000000000949d9f05ec68d1db Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I haven't studied your code in depth, but it seem= s like you're giving
`match-buffers/compiled` benchmark 10 ti= mes the work you're giving to
the other function, which = would explain why it's 10x slower.

The byte-co= mpiler (or the native compiler) can't really optimize the
mini-language more magically.=C2=A0 It can only optimize elisp.

My idea of using the byte-compiler to do this is different:= it entails
translating the mini-language to elisp first and= then byte-compiling
that.=C2=A0 But it is a technique that = I think your code isn't applying
or at least not correct= ly (though I haven't read all of it: I will soon).

=
You can see eglot's "glob matching" section for the appl= ication of
such a technique the "glob" minilanguag= e that is required by LSP (iow
it wasn't "invented = by me" ;-) )

Jo=C3=A3o

On Tue, Nov 1, 2022 at 1:03 PM Philip Kaludercic <philipk@posteo.net> wrote:
Jo=C3=A3o T=C3=A1vora <joaotavora@gmail.com> w= rites:

> On Tue, Nov 1, 2022 at 11:27 AM Philip Kaludercic <philipk@posteo.net>
> wrote:
>
>
>> E.g. `display-buffer-alist' makes use of it to associate displ= ay-buffer
>> rules with buffers.=C2=A0 Now you can add
>>
>>=C2=A0 =C2=A0 =C2=A0 =C2=A0((major-mode . help-mode) display-buffer= -in-side-window)
>>
>> instead of trying to match being a regular expression to catch all=
>> *Help* buffer names of a function along the lines of
>>
>>=C2=A0 =C2=A0 =C2=A0 =C2=A0(lambda (buf _alist)
>>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(with-current-buffer buf
>>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(derived-mode-p 'help-= mode)))
>>
>
> If you really want to save up on this typing, it's better to defin= e
> a reusable helper function, or even a higher order function.
>
>=C2=A0 =C2=A0(defun buffer-mode-matcher (mode)
>=C2=A0 =C2=A0 =C2=A0(lambda (b _alist)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0(with-current-buffer b (derived-mode-p '= help-mode))))
>
> You can add buffer-mode-matcher to the library if it becomes
> useful enough.=C2=A0 Then you add:
>
>=C2=A0 =C2=A0`(,(buffer-mode-matcher 'help-mode) display-buffer-in-= side-window)
>
> to display-buffer-alist.
>
> But if you really want a new language your language, then I suggest > a simple adapter buffer-matcher utility that merges the two.=C2=A0 Tha= t way one
> doesn't couple existing utilities to the new mini-language and
> simultaneously
> the new mini-language become useful in a much wider setting for those = who
> appreciate such things.
>
>=C2=A0 =C2=A0(defun buffer-matcher (condition)
>=C2=A0 =C2=A0 =C2=A0 "Return unary predicate of a buffer matching = the CONDITION
> mini-language."
>=C2=A0 =C2=A0 =C2=A0(lambda (buf &rest _whatever) ; make it even mo= re lax
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 (buffer-match-p condition)))
>
> Later on, you might even pass an (... &optional compiled) so that = the
> return value
> is syntax checked and optimized in some way at compile time.
>
> IOW, (E)Lisp already gives you the tools for these composition without=
> needing to invent new languages with the drawbacks I listed.

I was curious to try this out, and implemented something along the lines of your suggestion.=C2=A0 The bad news is that it is at least 10 times slow= er
than the current implementation, that isn't even really optimised.
Perhaps I did something native and didn't see what is wrong, but here are my notes:

--8<---------------cut here---------------start------------->8---
(defun translate-buffer-condition (condition)
=C2=A0 "Compile a CONDITION into a predicate function."
=C2=A0 (pcase-exhaustive condition
=C2=A0 =C2=A0 ((or 't 'nil)
=C2=A0 =C2=A0 =C2=A0(lambda (_buffer _arg)
=C2=A0 =C2=A0 =C2=A0 =C2=A0condition))
=C2=A0 =C2=A0 ((pred stringp)
=C2=A0 =C2=A0 =C2=A0(lambda (buffer _arg)
=C2=A0 =C2=A0 =C2=A0 =C2=A0(string-match-p condition (buffer-name buffer)))= )
=C2=A0 =C2=A0 ((pred functionp)
=C2=A0 =C2=A0 =C2=A0(if (eq 1 (cdr (func-arity condition)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(lambda (buffer _arg)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(funcall condition buffer))
=C2=A0 =C2=A0 =C2=A0 =C2=A0condition))
=C2=A0 =C2=A0 (`(major-mode . ,mode)
=C2=A0 =C2=A0 =C2=A0(lambda (buffer _arg)
=C2=A0 =C2=A0 =C2=A0 =C2=A0(eq
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (buffer-local-value 'major-mode buffer)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 mode)))
=C2=A0 =C2=A0 (`(derived-mode . ,mode)
=C2=A0 =C2=A0 =C2=A0(lambda (buffer _arg)
=C2=A0 =C2=A0 =C2=A0 =C2=A0(provided-mode-derived-p
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (buffer-local-value 'major-mode buffer)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 mode)))
=C2=A0 =C2=A0 (`(not . ,cond)
=C2=A0 =C2=A0 =C2=A0(lambda (buffer arg)
=C2=A0 =C2=A0 =C2=A0 =C2=A0(not (funcall (translate-buffer-condition cond) = buffer arg))))
=C2=A0 =C2=A0 (`(or . ,conds)
=C2=A0 =C2=A0 =C2=A0(lambda (buffer arg)
=C2=A0 =C2=A0 =C2=A0 =C2=A0(catch 'match
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(dolist (cond conds)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(when (funcall (translate-buffer-c= ondition cond) buffer arg)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(throw 'match t)))))) =C2=A0 =C2=A0 (`(and . ,conds)
=C2=A0 =C2=A0 =C2=A0(lambda (buffer arg)
=C2=A0 =C2=A0 =C2=A0 =C2=A0(catch 'match
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(dolist (cond conds t)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(when (funcall (translate-buffer-c= ondition cond) buffer arg)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(throw 'match nil))))))= ))

(defvar buffer-match-p-cache (make-hash-table :test 'eq))

(defun buffer-match-p/compiled (condition buffer-or-name &optional arg)=
=C2=A0 "Return non-nil if BUFFER-OR-NAME matches CONDITION.
CONDITION is either:
- the symbol t, to always match,
- the symbol nil, which never matches,
- a regular expression, to match a buffer name,
- a predicate function that takes a buffer object and ARG as
=C2=A0 arguments, and returns non-nil if the buffer matches,
- a cons-cell, where the car describes how to interpret the cdr.
=C2=A0 The car can be one of the following:
=C2=A0 * `derived-mode': the buffer matches if the buffer's major m= ode
=C2=A0 =C2=A0 is derived from the major mode in the cons-cell's cdr. =C2=A0 * `major-mode': the buffer matches if the buffer's major mod= e
=C2=A0 =C2=A0 is eq to the cons-cell's cdr.=C2=A0 Prefer using `derived= -mode'
=C2=A0 =C2=A0 instead when both can work.
=C2=A0 * `not': the cdr is interpreted as a negation of a condition. =C2=A0 * `and': the cdr is a list of recursive conditions, that all hav= e
=C2=A0 =C2=A0 to be met.
=C2=A0 * `or': the cdr is a list of recursive condition, of which at =C2=A0 =C2=A0 least one has to be met."
=C2=A0 (funcall (or (gethash condition buffer-match-p-cache)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(puthash condition =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 (byte-compile (translate-buffer-condition condition))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 buffer-match-p-cache))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(get-buffer buffer-or-name)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0arg))

(defun match-buffers/compiled (condition &optional buffers arg)
=C2=A0 "Return a list of buffers that match CONDITION.
See `buffer-match' for details on CONDITION.=C2=A0 By default all
buffers are checked, this can be restricted by passing an
optional argument BUFFERS, set to a list of buffers to check.
ARG is passed to `buffer-match', for predicate conditions in
CONDITION."
=C2=A0 (let (bufs)
=C2=A0 =C2=A0 (dolist (buf (or buffers (buffer-list)))
=C2=A0 =C2=A0 =C2=A0 (when (buffer-match-p/compiled condition (get-buffer b= uf) arg)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (push buf bufs)))
=C2=A0 =C2=A0 bufs))

;; Here we will test a moderately complicated condition and time how
;; long it takes with the current implementation and with the proposed
;; alternative.

(defvar sample-condition
=C2=A0 '(and (or buffer-file-name
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (derived-mode . compilation-mode)=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (derived-mode . dired-mode)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (derived-mode . diff-mode)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (derived-mode . comint-mode)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (derived-mode . eshell-mode)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (derived-mode . change-log-mode))=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 "\\*.+\\*"
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (not . "\\` ")))

(benchmark-run 100
=C2=A0 (match-buffers sample-condition pr))
;; =3D> (1.7045469830000002 20 1.1418286690000023)


(benchmark-run 1000
=C2=A0 (match-buffers/compiled project-buffer-conditions pr))
;; =3D> (17.646938126000002 219 12.428946030999999)
--8<---------------cut here---------------end--------------->8---

I guess this just goes to show that one shouldn't underestimate the cos= t
of a function call...

=C2=A0 =C2=A0 LISP programmers know the value of everything and the cost of= nothing.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0--=C2=A0 Alan Perlis


--
Jo=C3=A3o T=C3=A1vora
--000000000000949d9f05ec68d1db--