master: read-line: faster return if #\Newline is in the buffer

stassats via Sbcl-commits <[email protected]>
Newsgroups gmane.lisp.steel-bank.cvs
Message-ID <[email protected]>
The branch "master" has been updated in SBCL:
       via  945dba8de37009ce1548476b99b84f8a49f26ff0 (commit)
      from  0b019f26d463bdc73b63d8cf9f560d62703c6a60 (commit)

- Log -----------------------------------------------------------------
commit 945dba8de37009ce1548476b99b84f8a49f26ff0
Author: Stas Boukarev <[email protected]>
Date:   Wed Aug 19 08:01:34 2026 +0300

    read-line: faster return if #\Newline is in the buffer
---
 src/code/stream.lisp | 35 +++++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/src/code/stream.lisp b/src/code/stream.lisp
index 379069143..75b678daf 100644
--- a/src/code/stream.lisp
+++ b/src/code/stream.lisp
@@ -368,20 +368,27 @@
                      (replace res chunk :start1 start1)))))
         (declare (inline refill-buffer))
         (if (or (< %frc-index% +ansi-stream-in-buffer-length+) (refill-buffer))
-            (loop
-             (let ((pos (position #\Newline %frc-buffer%
-                                  :test #'char= :start %frc-index%)))
-               (when pos
-                 (return (values (build-result pos (- pos %frc-index%)) nil)))
-               (let ((chunk (subseq %frc-buffer% %frc-index%)))
-                 (incf chunks-total-length (length chunk))
-                 (push chunk chunks))
-               (unless (refill-buffer)
-                 (return (values (build-result -1 0) t)))))
-          ;; EOF had been reached before we read anything
-          ;; at all. Return the EOF value or signal the error.
-            (progn (done-with-fast-read-char)
-                   (eof-or-lose stream eof-error-p (values eof-value t))))))))
+            (let ((pos (position #\Newline %frc-buffer%
+                                 :test #'char= :start %frc-index%)))
+              (if pos
+                  (let ((result (subseq %frc-buffer% %frc-index% pos)))
+                    (setf %frc-index% (1+ pos))
+                    (done-with-fast-read-char)
+                    (values result nil))
+                  (loop
+                   (let ((chunk (subseq %frc-buffer% %frc-index%)))
+                     (incf chunks-total-length (length chunk))
+                     (push chunk chunks))
+                   (unless (refill-buffer)
+                     (return (values (build-result -1 0) t)))
+                   (let ((pos (position #\Newline %frc-buffer%
+                                        :test #'char= :start %frc-index%)))
+                     (when pos
+                       (return (values (build-result pos (- pos %frc-index%)) nil)))))))
+            ;; EOF had been reached before we read anything
+            ;; at all. Return the EOF value or signal the error.
+          (progn (done-with-fast-read-char)
+                 (eof-or-lose stream eof-error-p (values eof-value t))))))))
 
 ;; to potentially avoid consing a buffer on successive calls to read-line
 ;; (just consing the result string)

-----------------------------------------------------------------------


hooks/post-receive
-- 
SBCL
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.