[gambit-list] newbie question: output from one port as input to another port

Marc Feeley feeley at iro.umontreal.ca
Mon Mar 13 13:42:34 EST 2006


On 13-Mar-06, at 12:20 PM, Samuel Montgomery-Blinn wrote:

> Thank you both for the answers (and the code samples). Just seeing  
> how one is "supposed" to write such functions is a very good head  
> start to understanding.
>
> -Sam

No problem.  By the way, your message got me thinking about how to  
improve the input-to-output latency.  On a slow input device, it is  
probably worthwhile to read only as many octets as are currently  
available and transfer them to the output, in particular when the  
buffers are big.  This can be done by blocking the reading thread  
only for the reading of the first octet, and use a nonblocking read  
for the remaining octets.  Here's the new code (the copy-octet-port- 
to-octet-port-asap procedure).  Give it a try and see if it improves  
performance.

Marc


(define buf-size 4096)

(define (copy-octet-port-to-octet-port in out)
   (let ((buf (make-u8vector buf-size)))
     (let loop ()
       (let ((n (read-subu8vector buf 0 (u8vector-length buf) in)))
         (if (> n 0)
             (begin
               (write-subu8vector buf 0 n out)
               (force-output out)
               (loop)))))))

(define (copy-octet-port-to-octet-port-asap in out)
   (let ((buf (make-u8vector buf-size)))
     (let loop ()
       (input-port-timeout-set! in #f) ; block thread until at
                                       ; least 1 octet available from in
       (let ((n (read-subu8vector buf 0 1 in)))
         (if (> n 0)
             (begin
               (input-port-timeout-set! in 0) ; get whatever is still  
in the
                                              ; buffer without blocking
               (let ((m (read-subu8vector buf n (u8vector-length buf)  
in)))
                 (write-subu8vector buf 0 (+ n m) out)
                 (force-output out)
                 (loop))))))))




More information about the Gambit-list mailing list