bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#7089: 23.2; slow ansi-color-apply


From: Leo
Subject: bug#7089: 23.2; slow ansi-color-apply
Date: Thu, 28 Oct 2010 12:03:18 +0800
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (Mac OS X 10.6.4)

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 <address@hidden>
+
+       * ansi-color.el (ansi-color-apply): Rewrite using
+       re-search-forward for speed.
+
 2010-10-03  Chong Yidong  <address@hidden>
 
        * 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 via email to

[Prev in Thread] Current Thread [Next in Thread]