« Refactoring | Main | Avoiding Allegro's FORMAT »

A bottleneck

Using Allegro's profiler, I found a bottleneck in one tiny function that I had written a long time ago. Despite its simpleton nature, this function is important because it is invoked once for each line of output in the UMLisp print benchmark. By changing the poorly-implemented function
(defun indent-to-level (n &optional (stream *standard-output*))
  (format stream (format nil "~~~DT" (+ n n))))
to the more sane
(defun indent-to-level (n &optional (stream *standard-output*))
  (declare (fixnum n) (optimize (speed 3) (safety 0) (space 0)))
  (dotimes (i (the fixnum (+ n n)))
    (declare (fixnum i))
    (write-char #\space stream)))
I got a marked speed increase in the print benchmark

ImplOld Time
(user/total)
New Time
(user/total)
AllegroCL 6.211.0/11.18.2/8.3
CMUCL 18e+3.0/3.22.2/2.3
Lispworks 4.29.1/105.8/6
SBCL 0.8alpha.0.223.9/4.22.8/3.0
SBCL-MT 0.8alpha.0.143.7/4.52.7/3.2
SCL 1.1.19.2/9.75.1/5.5

This is a 26-44% overall speed increase by changing this one little function tucked away in an old library file. The point proven once again is that there is no adequate substitute for profiling: a major bottleneck may exist in some tiny, forgotten function.

Comments (1)

And the further rewrite looks like:

(defun indent-to-level (lvl stream)
(print-n-chars #\space (+ lvl lvl) stream))

(defun print-n-chars (char n stream)
(declare (fixnum n) (optimize speed))
(dotimes (i n)
(declare (fixnum i))
(write-char char stream)))

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on May 9, 2003 2:26 AM.

The previous post in this blog was Refactoring.

The next post in this blog is Avoiding Allegro's FORMAT.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.