Skip to content

Commit

Permalink
changed to use an array for string storage and write it with
Browse files Browse the repository at this point in the history
with-output-to-string

This allows me to retrieve the built string mulitple times and seems
to be significantly faster than the previous string-stream
implementation. (for my lisp, it seemed to be about 80% faster than
the wots implementation)
  • Loading branch information
bobbysmith007 committed Dec 21, 2011
1 parent 61b2270 commit e9c3b35
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions collectors.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
printed to the stream
A call to the function with no arguments returns the output string"
(let ((s (make-string-output-stream))
(let ((arr (make-array 100 :element-type 'character :fill-pointer 0))
(*print-pretty* pretty)
(printed? nil)
(print-empty? (null ignore-empty-strings-and-nil)))
Expand All @@ -43,23 +43,18 @@
((or null string) delimiter)
(t (princ-to-string delimiter))))
(flet ((p (item)
(let ((item (typecase item
((or null string) item)
(T (princ-to-string item)))))
(when (or print-empty? (and item (plusp (length item))))
(when (and printed? delimiter) (write-sequence delimiter s))
(write-sequence item s)
(setf printed? t)))))
(with-output-to-string (s arr)
(let ((item (typecase item
((or null string) item)
(T (princ-to-string item)))))
(when (or print-empty? (and item (plusp (length item))))
(when (and printed? delimiter) (write-sequence delimiter s))
(write-sequence item s)
(setf printed? t))))))
(lambda (&rest args)
(if args
(mapc #'p args)
;; todo: this seems pretty hacky, but I would like to maintain the interface
;; of all the other collectors (of being able to access intermediate output
;; repeatedly), so for now I guess this is it. Should test to see if
;; writing / coercing an array is faster or slower
(let ((so-far (get-output-stream-string s)))
(write-string so-far s)
so-far))))))
(coerce arr 'string))))))

(defmacro with-string-builder ((name &key delimiter (ignore-empty-strings-and-nil t))
&body body)
Expand Down

0 comments on commit e9c3b35

Please sign in to comment.