unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#7089: 23.2; slow ansi-color-apply
@ 2010-09-23  9:15 Leo
  2010-09-23 10:38 ` Leo
  0 siblings, 1 reply; 7+ messages in thread
From: Leo @ 2010-09-23  9:15 UTC (permalink / raw)
  To: 7089; +Cc: Alex Schroeder


I have found ansi-color-apply very expensive and slow.

To test this, do the following

  (remove-hook 'eshell-output-filter-functions 'eshell-handle-ansi-color)
  (add-hook 'eshell-preoutput-filter-functions 'ansi-color-apply)

and do something in eshell that can output a large coloured text such
as:

  cd /usr/include; ack time

where ack is a single file perl script from http://betterthangrep.com/.
The CPU usage should shoot up to very high.

The following version (after brief testing) seems to be noticeably
faster.

(defun ansi-color-apply* (string)
  "A more efficient implementation of `ansi-color-apply' (which see)."
  (let ((face (car ansi-color-context))
        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))
    (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 end (match-beginning 0)
                fragment (buffer-substring end (point-max)))
        (setq end (point-max)))
      ;; if the rest of the string should have a face, put it there
      (when face
        (put-text-property start end 'ansi-color t)
        (put-text-property start end 'face face))
      ;; save context
      (if (or face fragment)
          (setq ansi-color-context (list face fragment))
        (setq ansi-color-context nil))
      (buffer-string))))

Leo





^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2010-11-25 14:30 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2010-10-31 19:10       ` Stefan Monnier
2010-11-01 16:23         ` Leo
2010-11-25 14:30         ` Leo

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).