unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattiase@acm.org>
To: 14769@debbugs.gnu.org, shigeru.fukaya@gmail.com,
	Lars Ingebrigtsen <larsi@gnus.org>,
	Noam Postavsky <npostavs@gmail.com>,
	Stefan Monnier <monnier@iro.umontreal.ca>
Subject: bug#14769: [PATCH] optimize `concat's literals
Date: Sun, 16 Jun 2019 13:57:44 +0200	[thread overview]
Message-ID: <6BF130E1-F25B-4E1B-B55D-224F0CCEC133@acm.org> (raw)
In-Reply-To: <C9CE77463644E9shigeru.fukaya@gmail.com>

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

Since this bug was mentioned in  https://debbugs.gnu.org/36237, here is a slightly reworked version that does not special-case `concat' in the general code.


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

From 4a65b351c5f72a7d53904c57137c69bd6dc4d568 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 | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/lisp/emacs-lisp/byte-opt.el b/lisp/emacs-lisp/byte-opt.el
index 44cca6136c..1ebafec3f2 100644
--- a/lisp/emacs-lisp/byte-opt.el
+++ b/lisp/emacs-lisp/byte-opt.el
@@ -850,6 +850,29 @@ byte-optimize-memq
                           ',list)))))
     (byte-optimize-predicate form)))
 
+(defun byte-optimize-concat (form)
+  "Merge adjacent constant arguments to `concat'."
+  (let ((args nil)
+        (accum nil))
+    (dolist (arg (cdr form))
+      (unless (and (macroexp-const-p arg)
+                   (let ((val (eval arg)))
+                     (and (or (stringp val)
+                              (and (or (listp val) (vectorp val))
+                                   (not (memq nil (mapcar #'characterp val)))))
+                          ;; Constant arg: concat with previous.
+                          (setq accum (concat accum val)))))
+        ;; Non-constant arg.
+        (when (and accum (not (zerop (length accum))))
+          (push accum args))
+        (push arg args)
+        (setq accum nil)))
+    (when (and accum (not (zerop (length accum))))
+      (push accum args))
+    (if (= (length args) (length (cdr form)))
+        form          ; No improvement.
+      (cons 'concat (nreverse args)))))
+
 (put 'identity 'byte-optimizer 'byte-optimize-identity)
 (put 'memq 'byte-optimizer 'byte-optimize-memq)
 
@@ -892,6 +915,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)


  parent reply	other threads:[~2019-06-16 11:57 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 ` Mattias Engdegård [this message]
2019-06-19  1:43   ` bug#14769: " Noam Postavsky
2019-06-19 12:54     ` Mattias Engdegård
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=6BF130E1-F25B-4E1B-B55D-224F0CCEC133@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).