Read-sequence is slow?
Raymond Toy <[email protected]> Wed, 8 Apr 2026 09:21:37 -0700
| Newsgroups | gmane.lisp.gcl.devel |
|---|---|
| Message-ID | <[email protected]> |
This is a multi-part message in MIME format.
--------------ge0pVuyXayQiK1MmHR5IxIxd
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
I have this routine:
|(defun slurp-bytes (filename) "Return the entire contents of FILENAME
as a (unsigned-byte 8) vector. Results are cached in *bytes-cache* so
each file is read only once." (or (gethash filename *bytes-cache*) (setf
(gethash filename *bytes-cache*) (with-open-file (s filename :direction
:input :element-type '(unsigned-byte 8)) (let* ((len (file-length s))
(buf (make-array len :element-type '(unsigned-byte 8)))) (read-sequence
buf s) buf))))) |
When reading maxima.info-1 (1030671 bytes), it takes a very long time
(several seconds). Is there anyway to speed this up? I would have
expected reading a bunch of bytes from a file via read-sequence would be
very fast.
​
--------------ge0pVuyXayQiK1MmHR5IxIxd
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=3D"content-type" content=3D"text/html; charset=3DUTF=
-8">
<title></title>
<meta charset=3D"utf-8">
</head>
<body>
<div class=3D"markdown-here-wrapper">
<p style=3D"margin-bottom: 19.2px; margin-top: 0px;">I have this
routine:</p>
<pre
style=3D"font-family: Consolas, Inconsolata, Courier, monospace; font-siz=
e: 16px; line-height: 19.2px; margin-bottom: 19.2px; margin-top: 19.2px;"=
><code
style=3D"background-color: rgba(175, 184, 193, 0.2); border-block: 1px so=
lid rgb(204, 204, 204); border-bottom: 1px solid rgb(204, 204, 204); bord=
er-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-end-e=
nd-radius: 3px; border-end-start-radius: 3px; border-inline: 1px solid rg=
b(204, 204, 204); border-left: 1px solid rgb(204, 204, 204); border-right=
: 1px solid rgb(204, 204, 204); border-start-end-radius: 3px; border-star=
t-start-radius: 3px; border-top: 1px solid rgb(204, 204, 204); border-top=
-left-radius: 3px; border-top-right-radius: 3px; display: block; font-fam=
ily: Consolas, Inconsolata, Courier, monospace; font-size: 16px; font-wei=
ght: 550; line-height: 19.2px; margin-left: 2.4px; margin-right: 2.4px; o=
verflow-block: auto; overflow-inline: auto; overflow: auto; padding-block=
: 8px; padding-bottom: 8px; padding-inline: 11.2px; padding-left: 11.2px;=
padding-right: 11.2px; padding-top: 8px;">
(defun slurp-bytes (filename)
"Return the entire contents of FILENAME as a (unsigned-byte 8) vector.
Results are cached in *bytes-cache* so each file is read only once."
(or (gethash filename *bytes-cache*)
(setf (gethash filename *bytes-cache*)
(with-open-file (s filename :direction :input
:element-type '(unsigned-byte 8))=
(let* ((len (file-length s))
(buf (make-array len :element-type '(unsigned-byte 8=
))))
(read-sequence buf s)
buf)))))
</code></pre>
<p style=3D"margin-bottom: 19.2px; margin-top: 0px;">When reading
maxima.info-1 (1030671 bytes), it takes a very long time
(several seconds). Is there anyway to speed this up? I would
have expected reading a bunch of bytes from a file via
read-sequence would be very fast.</p>
</div>
<div class=3D"mdhr-raw"
style=3D"height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-s=
ize:0;padding:0;margin:0;"
aria-hidden=3D"true"
title=3D"MDH:PHA+SSBoYXZlIHRoaXMgcm91dGluZTo8L3A+PHA+YGBgPC9wPjxwPihkZWZ1=
biBzbHVycC1ieXRlcyAoZmlsZW5hbWUpPGJyPiZuYnNwOyAiUmV0dXJuIHRoZSBlbnRpcmUgY=
29udGVudHMgb2YgRklMRU5BTUUgYXMgYSAodW5zaWduZWQtYnl0ZSA4KSB2ZWN0b3IuPGJyPi=
ZuYnNwOyAmbmJzcDtSZXN1bHRzIGFyZSBjYWNoZWQgaW4gKmJ5dGVzLWNhY2hlKiBzbyBlYWN=
oIGZpbGUgaXMgcmVhZCBvbmx5IG9uY2UuIjxicj4mbmJzcDsgKG9yIChnZXRoYXNoIGZpbGVu=
YW1lICpieXRlcy1jYWNoZSopPGJyPiZuYnNwOyAmbmJzcDsgJm5ic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