all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Michael Heerdegen via "Emacs development discussions." <emacs-devel@gnu.org>
To: emacs-devel@gnu.org
Subject: Re: master 4b79c80c999 1/2: New function 'sort-on'
Date: Tue, 05 Mar 2024 11:21:50 +0100	[thread overview]
Message-ID: <87frx5q8qp.fsf@web.de> (raw)
In-Reply-To: 8734t5hzl2.fsf@web.de

[-- Attachment #1: Type: text/plain, Size: 205 bytes --]

Michael Heerdegen <michael_heerdegen@web.de> writes:

> I gave it a try.  The paragraph in the manual describing `on-sort'
> internals would have to be updated in addition.

Version with one thinko less:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Optimize-sort-on.patch --]
[-- Type: text/x-diff, Size: 2142 bytes --]

From cf41d9abacc6d918cf45ded985fb3dcebd8cc5af Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <michael_heerdegen@web.de>
Date: Tue, 5 Mar 2024 08:03:26 +0100
Subject: [PATCH] Optimize sort-on

* lisp/sort.el (sort-on): Manipulate the argument list instead of
creating new lists with `mapcar'.
---
 lisp/sort.el | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/lisp/sort.el b/lisp/sort.el
index 1e82a097047..5054d98913d 100644
--- a/lisp/sort.el
+++ b/lisp/sort.el
@@ -30,6 +30,7 @@
 ;;; Code:
 
 (eval-when-compile (require 'subr-x))
+(eval-when-compile (require 'cl-lib))
 
 (defgroup sort nil
   "Commands to sort text in an Emacs buffer."
@@ -497,15 +498,25 @@ sort-on
 PREDICATE is the function for comparing keys; it is called with two
 arguments, the keys to compare, and should return non-nil if the
 first key should sort before the second key.
-The return value is always a new list.
+SEQUENCE is modified by side effects.
 This function has the performance advantage of evaluating
 ACCESSOR only once for each element in the input SEQUENCE, and is
 therefore appropriate when computing the key by ACCESSOR is an
 expensive operation.  This is known as the \"decorate-sort-undecorate\"
 paradigm, or the Schwartzian transform."
-  (mapcar #'car
-          (sort (mapcar #'(lambda (x) (cons x (funcall accessor x))) sequence)
-                #'(lambda (x y) (funcall predicate (cdr x) (cdr y))))))
+  (cl-macrolet ((transform (list transformer)
+                  (macroexp-let2 macroexp-copyable-p l list
+                    (macroexp-let2 nil ret l
+                      `(progn
+                         (while ,l
+                           (setcar ,l ,(macroexp-let2 nil elt `(car ,l)
+                                         (funcall transformer elt)))
+                           (setq ,l (cdr ,l)))
+                         ,ret)))))
+    (transform
+     (sort (transform sequence (lambda (x) `(cons ,x (funcall accessor ,x))))
+           #'(lambda (x y) (funcall predicate (cdr x) (cdr y))))
+     (lambda (x) `(car ,x)))))
 
 \f
 (defvar sort-columns-subprocess t)
-- 
2.39.2


[-- Attachment #3: Type: text/plain, Size: 10 bytes --]


Michael.

  reply	other threads:[~2024-03-05 10:21 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <170688047526.14693.2994051491358257471@vcs2.savannah.gnu.org>
     [not found] ` <20240202132756.4272CC0EFE7@vcs2.savannah.gnu.org>
2024-02-02 15:00   ` master 4b79c80c999 1/2: New function 'sort-on' Daniel Mendler via Emacs development discussions.
2024-02-02 15:04     ` Eli Zaretskii
2024-02-02 15:26       ` Daniel Mendler via Emacs development discussions.
2024-02-02 15:47         ` Eli Zaretskii
2024-02-02 16:05           ` Daniel Mendler via Emacs development discussions.
2024-02-05 12:14             ` Michael Heerdegen
2024-02-02 15:55         ` Dmitry Gutov
2024-02-02 15:30       ` Michael Heerdegen via Emacs development discussions.
2024-02-02 15:35         ` Daniel Mendler via Emacs development discussions.
2024-02-02 16:08           ` Michael Heerdegen via Emacs development discussions.
2024-02-02 16:23             ` Daniel Mendler via Emacs development discussions.
2024-02-02 16:43               ` Michael Heerdegen via Emacs development discussions.
2024-02-02 15:50         ` Eli Zaretskii
2024-02-02 16:06           ` Eshel Yaron
2024-02-02 16:34             ` Eli Zaretskii
2024-02-02 16:46           ` Michael Heerdegen via Emacs development discussions.
2024-02-02 17:55             ` Emanuel Berg
2024-02-05  0:48           ` Dmitry Gutov
2024-02-05  5:30             ` Yuri Khan
2024-02-05 12:52               ` Eli Zaretskii
2024-02-05 13:25               ` Dmitry Gutov
2024-02-05 14:31                 ` Eli Zaretskii
2024-02-05 14:47                   ` Dmitry Gutov
2024-02-05 15:10                     ` Eli Zaretskii
2024-02-05 15:29                       ` Dmitry Gutov
2024-02-28  7:40                 ` Michael Heerdegen
2024-03-01 23:37                   ` Dmitry Gutov
2024-03-04  6:45                     ` Michael Heerdegen
2024-03-04 16:43                       ` Dmitry Gutov
2024-03-05  8:06                         ` Michael Heerdegen
2024-03-05 10:21                           ` Michael Heerdegen via Emacs development discussions. [this message]
2024-03-05 12:39                             ` Eli Zaretskii
2024-03-06  3:20                               ` Michael Heerdegen
2024-03-06 12:10                                 ` Eli Zaretskii
2024-03-06 18:34                                   ` Dmitry Gutov
2024-03-06 20:12                                     ` John Wiegley
2024-03-07  1:34                                       ` Dmitry Gutov
2024-03-05 12:44                             ` Dmitry Gutov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87frx5q8qp.fsf@web.de \
    --to=emacs-devel@gnu.org \
    --cc=michael_heerdegen@web.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.