Subject: Re: fast io
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 1997/01/21
Newsgroups: comp.lang.lisp
Message-ID: <5c1ohi$6ol@tokyo.engr.sgi.com>

Bjorn Borud  <borud@lucifer.guardian.no> wrote:
+---------------
| I am looking for ways to read and write blocks of data fast in Scheme
| or Common Lisp, but I can only find per-character IO-routines or, at
| best, per-line.  what I would like to do is to read entire files using
| read() or equivalent...
+---------------

Jaffer's "SCM" implementation of Scheme provides a non-standard Scheme
datatype called "uniform vector" (much like CL's specialized arrays),
allocated with:

	(make-uniform-vector length prototype)

The type/value of "prototype" determines the specialization of the
vector. Such vectors may be shortened or lengthened with:

	(vector-set-length! uve length)

The procedures:

	(uniform-vector-read! uve)
	(uniform-vector-read! uve port)
	(uniform-vector-write uve)
	(uniform-vector-write uve port)

read or write (uniform-vector-length uve) binary objects from/to the
(possibly default) input or output port, and return the actual number
of objects read/written [e.g., if you get EOF on input, the number
read may be less than (uniform-vector-length uve)]. When running on
Unix, these procedures use the underlying Unix read() and write() calls
directly. Code such as this:

	(define BUFSIZ 16384)
	(define buf (make-uniform-vector BUFSIZ #\a ))

	(define (cat-all-bytes port)
	  (do ((n (uniform-vector-read! buf port)
		  (uniform-vector-read! buf port)))
	      ((<= n 0))
	    (vector-set-length! buf n)
	    (uniform-vector-write buf)
	    (vector-set-length! buf BUFSIZ)))

will run at approximately disk speed (the same as when written in C).


-Rob

-----
Rob Warnock, 7L-551		rpw3@sgi.com
Silicon Graphics, Inc.		http://reality.sgi.com/rpw3/
2011 N. Shoreline Blvd.		Phone: 415-933-1673  FAX: 415-933-0979
Mountain View, CA  94043	PP-ASEL-IA