unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattiase@acm.org>
To: Noam Postavsky <npostavs@gmail.com>
Cc: Lars Ingebrigtsen <larsi@gnus.org>,
	Stefan Monnier <monnier@iro.umontreal.ca>,
	14769@debbugs.gnu.org, shigeru.fukaya@gmail.com
Subject: bug#14769: [PATCH] optimize `concat's literals
Date: Wed, 19 Jun 2019 14:54:16 +0200	[thread overview]
Message-ID: <43EF1EEA-BE3B-493A-9BDE-C8AE37B9696A@acm.org> (raw)
In-Reply-To: <87r27qwdrv.fsf@gmail.com>

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

19 juni 2019 kl. 03.43 skrev Noam Postavsky <npostavs@gmail.com>:
> 
> Mattias Engdegård <mattiase@acm.org> writes:
> 
>> +    (dolist (arg (cdr form))
> 
>> +                   (let ((val (eval arg)))
> 
>> +                          ;; Constant arg: concat with previous.
>> +                          (setq accum (concat accum val)))))
> 
> Hmm, I think the OP's patch is careful not to concat in a loop like
> this: it's O(n^2).  I guess for most human written code n is small
> enough that it doesn't matter, but I could imagine this slowing
> compilation of a very big rx macro.

Yes, I thought it wouldn't matter but you are right. Updated patch attached.

For that matter, ry performs this optimisation itself, but it's also nice to have it in the compiler, since concat is frequently used to split long string constants in elisp source.

[-- Attachment #2: 0001-Merge-consecutive-constant-concat-args-bug-14769.patch --]
[-- Type: application/octet-stream, Size: 2408 bytes --]

From d4c3bbce9472618b5e762a18af5ba1a2b1b79698 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Sun, 16 Jun 2019 13:13:47 +0200
Subject: [PATCH] Merge consecutive constant `concat' args (bug#14769)

Suggested by Shigeru Fukaya <shigeru.fukaya@gmail.com>

* lisp/emacs-lisp/byte-opt.el (byte-optimize-concat): New.
(concat): Add byte-optimizer.
---
 lisp/emacs-lisp/byte-opt.el | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/lisp/emacs-lisp/byte-opt.el b/lisp/emacs-lisp/byte-opt.el
index b0aa407c8b..2e09601639 100644
--- a/lisp/emacs-lisp/byte-opt.el
+++ b/lisp/emacs-lisp/byte-opt.el
@@ -850,6 +850,33 @@ byte-optimize-memq
                           ',list)))))
     (byte-optimize-predicate form)))
 
+(defun byte-optimize-concat (form)
+  "Merge adjacent constant arguments to `concat'."
+  (let ((args (cdr form))
+        (newargs nil))
+    (while args
+      (let ((strings nil)
+            val)
+        (while (and args (macroexp-const-p (car args))
+                    (progn
+                      (setq val (eval (car args)))
+                      (and (or (stringp val)
+                               (and (or (listp val) (vectorp val))
+                                    (not (memq nil
+                                               (mapcar #'characterp val))))))))
+          (push val strings)
+          (setq args (cdr args)))
+        (when strings
+          (let ((s (apply #'concat (nreverse strings))))
+            (when (not (zerop (length s)))
+              (push s newargs)))))
+      (when args
+        (push (car args) newargs)
+        (setq args (cdr args))))
+    (if (= (length newargs) (length (cdr form)))
+        form          ; No improvement.
+      (cons 'concat (nreverse newargs)))))
+
 (put 'identity 'byte-optimizer 'byte-optimize-identity)
 (put 'memq 'byte-optimizer 'byte-optimize-memq)
 
@@ -892,6 +919,8 @@ byte-optimize-memq
 (put 'car-safe 'byte-optimizer 'byte-optimize-predicate)
 (put 'cdr-safe 'byte-optimizer 'byte-optimize-predicate)
 
+(put 'concat 'byte-optimizer 'byte-optimize-concat)
+
 ;; I'm not convinced that this is necessary.  Doesn't the optimizer loop
 ;; take care of this? - Jamie
 ;; I think this may some times be necessary to reduce ie (quote 5) to 5,
-- 
2.20.1 (Apple Git-117)


  reply	other threads:[~2019-06-19 12:54 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-02 17:04 bug#14769: 24.3.50; [PATCH] optimize `concat's literals Shigeru Fukaya
2016-02-24  4:51 ` Lars Ingebrigtsen
2019-06-16 11:57 ` bug#14769: " Mattias Engdegård
2019-06-19  1:43   ` Noam Postavsky
2019-06-19 12:54     ` Mattias Engdegård [this message]
2019-06-20  0:38       ` Noam Postavsky
2019-06-21  9:07         ` Mattias Engdegård
2019-06-22 22:17           ` Noam Postavsky
2019-06-26  9:44             ` Mattias Engdegård

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=43EF1EEA-BE3B-493A-9BDE-C8AE37B9696A@acm.org \
    --to=mattiase@acm.org \
    --cc=14769@debbugs.gnu.org \
    --cc=larsi@gnus.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=npostavs@gmail.com \
    --cc=shigeru.fukaya@gmail.com \
    /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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).