- (let* ((length (string-output-stream-index stream))
- (result (make-string length)))
- (replace result (string-output-stream-string stream))
- (setf (string-output-stream-index stream) 0)
+ (let* ((length (string-output-stream-last-index stream))
+ (element-type (string-output-stream-element-type stream))
+ (result
+ (case element-type
+ ;; Overwhelmingly common case; can be inlined.
+ ((character) (make-string length))
+ (t (make-string length :element-type element-type)))))
+ ;; For the benefit of the REPLACE transform, let's do this, so
+ ;; that the common case isn't ludicrously expensive.
+ (etypecase result
+ ((simple-array character (*))
+ (replace result (string-output-stream-string stream)))
+ ((simple-array nil (*))
+ (replace result (string-output-stream-string stream))))
+ (setf (string-output-stream-index stream) 0
+ (string-output-stream-index-cache stream) 0)