cl-str icon indicating copy to clipboard operation
cl-str copied to clipboard

REPEAT use APPLY with too many arguments, very sub-optimal

Open Yehouda opened this issue 6 months ago • 2 comments

https://github.com/vindarel/cl-str/blame/f2d52f5490e691dcda132c5f84683cf153090794/str.lisp#L350

The number per chunk should be limited call-argument-list, so the call to apply and concat are with limited number fo arguments. On Lispworks the limit is 20247, so currently you get an error in the test with large count.

Anyway, the code there is very sub-optimal. You really should make the new string in one go, and then copy into it. Something like:

(defun repeat (count string)
  (let ((str-len (length string)))
    (cond  ((= str-len 1)
            (let ((char(char string 0)))
              (make-string count :initial-element char :element-type (type-of char))))
           ((= str-len 0) "")
           (t
            (let* ((full-len (* count str-len))
                   (index 0)
                   (elemet-type (if (typep string 'base-string) ; maybe always make it character
                                    'base-char
                                  'character))
                   (full-string (make-string full-len :element-type elemet-type )))
              (dotimes (x count)
                (replace full-string string :start1 index)
                (incf index str-len))
              full-string)))))

I didn't test this on many cases.

Yehouda avatar Jul 31 '25 11:07 Yehouda

Isn't the apply used with max 10 000 elements?

But thanks, I'll look at your improvement suggestions.

vindarel avatar Aug 28 '25 14:08 vindarel

I mistyped the number. In lispWorks it is 2047 (not 20247).

Yehouda avatar Aug 28 '25 14:08 Yehouda