GNU bug report logs - #66816
[PATCH] Delete redundant lambdas around unary functions

Previous Next

Package: emacs;

Reported by: Stefan Kangas <stefankangas <at> gmail.com>

Date: Sun, 29 Oct 2023 13:04:02 UTC

Severity: wishlist

Tags: patch

To reply to this bug, email your comments to 66816 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#66816; Package emacs. (Sun, 29 Oct 2023 13:04:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Stefan Kangas <stefankangas <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sun, 29 Oct 2023 13:04:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefankangas <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: [PATCH] Delete redundant lambdas around unary functions
Date: Sun, 29 Oct 2023 06:02:38 -0700
[Message part 1 (text/plain, inline)]
Severity: wishlist

Please see the attached patch.

I've been able to measure around 10% better performance in the few cases
I looked into.  Here's an example:

    (benchmark-run 1000
      (seq-filter (lambda (n) (bufferp n))
                  (number-sequence 0 100000)))
    => (69.007736 333 47.018879999999996)

    (benchmark-run 1000
      (seq-filter #'bufferp
                  (number-sequence 0 100000)))
    => (61.541616 333 47.673137)
[0001-Delete-redundant-lambdas-around-unary-functions.patch (text/x-patch, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#66816; Package emacs. (Sun, 29 Oct 2023 19:05:01 GMT) Full text and rfc822 format available.

Message #8 received at 66816 <at> debbugs.gnu.org (full text, mbox):

From: Mattias Engdegård <mattias.engdegard <at> gmail.com>
To: Stefan Kangas <stefankangas <at> gmail.com>
Cc: Stefan Monnier <monnier <at> iro.umontreal.ca>, 66816 <at> debbugs.gnu.org
Subject: bug#66816: [PATCH] Delete redundant lambdas around unary functions
Date: Sun, 29 Oct 2023 20:03:16 +0100
> I've been able to measure around 10% better performance in the few cases
> I looked into.  Here's an example:
> 
>     (benchmark-run 1000
>       (seq-filter (lambda (n) (bufferp n))
>                   (number-sequence 0 100000)))

(This would be less of a problem if we could inline functions on the Lisp level...)

We could also have the compiler η-reduce automatically. I investigated this very briefly some time ago. Maybe I can dig up the patch.

One problem is that the arity isn't necessarily preserved. Most of the time this isn't a problem but it could be something to watch out for if doing the η-reduction by hand.






Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#66816; Package emacs. (Sun, 29 Oct 2023 21:57:01 GMT) Full text and rfc822 format available.

Message #11 received at 66816 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Mattias Engdegård <mattias.engdegard <at> gmail.com>
Cc: Stefan Kangas <stefankangas <at> gmail.com>, 66816 <at> debbugs.gnu.org
Subject: Re: bug#66816: [PATCH] Delete redundant lambdas around unary functions
Date: Sun, 29 Oct 2023 17:55:27 -0400
The patch looks good to me.

> We could also have the compiler η-reduce automatically. I investigated this
> very briefly some time ago. Maybe I can dig up the patch.
> One problem is that the arity isn't necessarily preserved.

And even if we check the arity before performing the rewrite, the arity
at compile-time may be different from the arity at run-time, so it's not
a safe optimization.

OTOH we could emit a warning :-)

> Most of the time this isn't a problem but it could be something to
> watch out for if doing the η-reduction by hand.

I've been bitten a few times, indeed.


        Stefan





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#66816; Package emacs. (Mon, 30 Oct 2023 08:57:02 GMT) Full text and rfc822 format available.

Message #14 received at 66816 <at> debbugs.gnu.org (full text, mbox):

From: Mattias Engdegård <mattias.engdegard <at> gmail.com>
To: Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: Stefan Kangas <stefankangas <at> gmail.com>, 66816 <at> debbugs.gnu.org
Subject: Re: bug#66816: [PATCH] Delete redundant lambdas around unary functions
Date: Mon, 30 Oct 2023 09:56:01 +0100
29 okt. 2023 kl. 22.55 skrev Stefan Monnier <monnier <at> iro.umontreal.ca>:

> The patch looks good to me.

To me, too -- good work.

One version that the patch doesn't seem to include is

  (lambda (X...) (funcall F X...)) -> F

which is less safe in general (since the value of the expression F could change between the closure creation and application) but sometimes obviously fine, as when the function is a parameter to `mapcar`.

The patch also appears limited to unary functions.





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#66816; Package emacs. (Mon, 30 Oct 2023 15:02:02 GMT) Full text and rfc822 format available.

Message #17 received at 66816 <at> debbugs.gnu.org (full text, mbox):

From: Mattias Engdegård <mattias.engdegard <at> gmail.com>
To: Stefan Kangas <stefankangas <at> gmail.com>
Cc: Stefan Monnier <monnier <at> iro.umontreal.ca>, 66816 <at> debbugs.gnu.org
Subject: Re: bug#66816: [PATCH] Delete redundant lambdas around unary functions
Date: Mon, 30 Oct 2023 16:00:34 +0100
[Message part 1 (text/plain, inline)]
Since I had a tool for it handy, I scanned the Emacs tree. Output attached, in case it might be of use.
Caution: it has not been vetted in any way and definitely contains plenty of false positives.

[eta-opportunities.log (application/octet-stream, attachment)]

This bug report was last modified 185 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.