unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Leo <sdl.web@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: 7089@debbugs.gnu.org
Subject: bug#7089: 23.2; slow ansi-color-apply
Date: Thu, 28 Oct 2010 12:03:18 +0800	[thread overview]
Message-ID: <m1bp6farqx.fsf@cam.ac.uk> (raw)
In-Reply-To: <jwvd3qvjb65.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Wed, 27 Oct 2010 22:38:17 -0400")

On 2010-10-28 10:38 +0800, Stefan Monnier wrote:
>> The following version fixed some glitches in setting ansi-color-context.
>> Also I have received an email from Alex that welcomes the improvement.
>> Let me know if I should send a patch in.
>
> Yes, a patch would be nice.  Also a ChangeLog explaining the change
> (which should hopefully explain why the new code is faster) would
> be welcome.
>
>
>         Stefan

Attached to the end of this message. I basically rewrite
ansi-color-apply using re-search-forward (as in
ansi-color-apply-on-region) which seems to be an order more efficient
than string-match.

I have been using the new version in eshell and it is almost as
efficient as ansi-color-apply-on-region. It is very painful to use the
original ansi-color-apply.

Do you know for sure string-match is slower (more CPU intensive) than
re-search-forward?

Thanks.
Leo

--------------------------------

From 724a620dd2d5301c32ecf4fbe6ce539db0c9bc8d Mon Sep 17 00:00:00 2001
Date: Thu, 28 Oct 2010 11:48:13 +0800
Subject: [PATCH] Rewrite ansi-color-apply using re-search-forward

to improve efficiency. `string-match' uses a lot of CPU.
---
 lisp/ChangeLog     |    5 ++++
 lisp/ansi-color.el |   66 ++++++++++++++++++++++++++-------------------------
 2 files changed, 39 insertions(+), 32 deletions(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 146c6c9..7088f28 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,8 @@
+2010-10-28  Leo <sdl.web@gmail.com>
+
+	* ansi-color.el (ansi-color-apply): Rewrite using
+	re-search-forward for speed.
+
 2010-10-03  Chong Yidong  <cyd@stupidchicken.com>
 
 	* minibuffer.el (completion--some, completion--do-completion)
diff --git a/lisp/ansi-color.el b/lisp/ansi-color.el
index 00162c9..6ef55e1 100644
--- a/lisp/ansi-color.el
+++ b/lisp/ansi-color.el
@@ -314,43 +314,45 @@ This function can be added to `comint-preoutput-filter-functions'.
 You cannot insert the strings returned into buffers using font-lock.
 See `ansi-color-unfontify-region' for a way around this."
   (let ((face (car ansi-color-context))
-	(start 0) end escape-sequence result
-	colorized-substring)
+        start end fragment escape-sequence)
     ;; If context was saved and is a string, prepend it.
     (if (cadr ansi-color-context)
         (setq string (concat (cadr ansi-color-context) string)
               ansi-color-context nil))
-    ;; Find the next escape sequence.
-    (while (setq end (string-match ansi-color-regexp string start))
-      (setq escape-sequence (match-string 1 string))
-      ;; Colorize the old block from start to end using old face.
-      (when face
-	(put-text-property start end 'ansi-color t string)
-	(put-text-property start end 'face face string))
-      (setq colorized-substring (substring string start end)
-	    start (match-end 0))
-      ;; Eliminate unrecognized ANSI sequences.
-      (while (string-match ansi-color-drop-regexp colorized-substring)
-	(setq colorized-substring
-	      (replace-match "" nil nil colorized-substring)))
-      (push colorized-substring result)
-      ;; Create new face, by applying escape sequence parameters.
-      (setq face (ansi-color-apply-sequence escape-sequence face)))
-    ;; if the rest of the string should have a face, put it there
-    (when face
-      (put-text-property start (length string) 'ansi-color t string)
-      (put-text-property start (length string) 'face face string))
-    ;; save context, add the remainder of the string to the result
-    (let (fragment)
-      (if (string-match "\033" string start)
-	  (let ((pos (match-beginning 0)))
-	    (setq fragment (substring string pos))
-	    (push (substring string start pos) result))
-	(push (substring string start) result))
+    (prog1
+        (with-temp-buffer
+          (insert string)
+          (setq start (point-min-marker))
+          (goto-char start)
+          (while (re-search-forward ansi-color-drop-regexp nil t)
+            (replace-match ""))
+          (goto-char start)
+          ;; Find the next escape sequence.
+          (while (re-search-forward ansi-color-regexp nil t)
+            (setq end (match-beginning 0))
+            (when face
+              (put-text-property start end 'ansi-color t)
+              (put-text-property start end 'face face))
+            (setq start (copy-marker (match-end 0)))
+            (setq escape-sequence (match-string 1))
+            (replace-match "")
+            (setq face (ansi-color-apply-sequence escape-sequence face)))
+          ;; Search for the possible start of a new escape sequence
+          (if (re-search-forward "\033" nil t)
+              (setq fragment
+                    (delete-and-extract-region (match-beginning 0)
+                                               (point-max))))
+          ;; If the rest of the string should have a face, put it there
+          (when face
+            (put-text-property start (point-max) 'ansi-color t)
+            (put-text-property start (point-max) 'face face))
+          ;; Return the string
+          (buffer-string))
+      ;; Save context; NB: ansi-color-context is buffer-local so set it after
+      ;; exit the temp buffer.
       (if (or face fragment)
-	  (setq ansi-color-context (list face fragment))
-	(setq ansi-color-context nil)))
-    (apply 'concat (nreverse result))))
+          (setq ansi-color-context (list face fragment))
+        (setq ansi-color-context nil)))))
 
 ;; Working with regions
 
-- 
1.7.3






  reply	other threads:[~2010-10-28  4:03 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-23  9:15 bug#7089: 23.2; slow ansi-color-apply Leo
2010-09-23 10:38 ` Leo
2010-10-28  2:38   ` Stefan Monnier
2010-10-28  4:03     ` Leo [this message]
2010-10-31 19:10       ` Stefan Monnier
2010-11-01 16:23         ` Leo
2010-11-25 14:30         ` Leo

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=m1bp6farqx.fsf@cam.ac.uk \
    --to=sdl.web@gmail.com \
    --cc=7089@debbugs.gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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).