chickadee » specialized-io

Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

specialized-io

Description

Fast I/O procedures specialized for different types. Instead of using the Scheme procedure read, specialized procedures can be used, so:

The table below is a simple comaprison done in one system. (This is not a real benchmark; it is just meant to give an approximate idea of what the speedup could be, and even then, the results on a different machine could be diferent!)

                scheme    specialized-io   speedup
-------------------------------------------------
write fixnum     6.516      4.396          1.4823
read  fixnum     27.98      6.304          4.4385

write flonum     9.596      6.076          1.5793
read  flonum    99.396      5.776          17.208

write complex  155.092     20.776          7.4650
read  complex   212.08     22.776          9.3116
-------------------------------------------------

The source for the benchmarks is in the subversion repository.

Author

Jeronimo Pellegrini

Requirements

Example

(use specialized-io)

(let ((v (read-flonum (current-input-port))))
  (write-flonum (fp* 2.5 v) (current-output-port)))

Note that you can use fp* with a value returned from read-flonum (it's guaranteed to be a flonum).

Documentation

read-fixnum PORTprocedure
read-flonum PORTprocedure
read-complex PORTprocedure
read-one-char PORTprocedure

These procedures read one object of a specific type from an input-port. There are no runtime checks in the Scheme way; the procedures will just call error if anything goes wront (EOF, lexical error etc).

write-fixnum VALUE PORTprocedure
write-flonum VALUE PORTprocedure
write-complex VALUE PORTprocedure
write-one-char VALUE PORTprocedure

These procedures write one object of a specific type to an output-port. There are no runtime checks in the Scheme way; the procedures will just call error if anything goes wrong (EOF, lexical error etc).

write-one-string STRING PORTprocedure

This procedure writes a string to the output port.

read-string-until CHAR PORTprocedure
read-string-between LEFTCHAR RIGHTCHAR PORTprocedure

These procedures read a string from the input port. read-string-until will read from the current position of the stream until CHAR is found (and CHAR will be included in the resulting string). read-string-between will first search for LEFTCHAR then read until RIGHTCHAR is found.

specialized-io-errorparameter

This variable should be bound to a procedure that will be called when parsing fails. By default it's bound to error. To change it, call (specialized-io some-other-error-procedure).

+specialized-io-string-buffer-size+parameter

This is the size of the buffer in which strings are read. When the buffer becomes full, its size is doubled. This default value is 500. To change it, call (+specialized-io-string-buffer-size+ N), where N is the desired buffer size.

Limitations

The procedures in this egg are not safe. They may crash if called with arguments different form the expected. In particular, the ports that may be passed to these procedures are those bound to C file descriptors: files, standard input/ourput/error, etc. They do not work, for example, with string ports.

More examples

Create a file named "in", with the following content:

10
1.2 1.3 .14 4.2 .5 6.6 7.0 00 0.0 10.0

The code below will read the numbers in the file and put them in a vector.

(use specialized-io)
(use srfi-4)

(define port (open-input-file "in"))

(let ((n (read-fixnum port)))
  (let ((vec (make-f64vector n)))
    (do ((i 0 (fx+ 1 i)))
        ((fx= i n))
      (f64vector-set! vec i (read-flonum port)))
    vec))

==> #f64(1.2 1.3 0.14 4.2 0.5 6.6 7.0 0.0 0.0 10.0)

Changelog

License

 Copyright (c) 2010, 2011, 2012 Jeronimo C. Pellegrini
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
 conditions are met:
 
   Redistributions of source code must retain the above copyright notice, this list of conditions and the following
     disclaimer. 
   Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
     disclaimer in the documentation and/or other materials provided with the distribution. 
   Neither the name of the author nor the names of its contributors may be used to endorse or promote
     products derived from this software without specific prior written permission. 
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.

Contents »