[plt-scheme] re-open a bytes/string port?

From: YC (yinso.chen at gmail.com)
Date: Fri Jan 16 15:44:26 EST 2009

On Fri, Jan 16, 2009 at 12:06 PM, Matthew Flatt <mflatt at cs.utah.edu> wrote:

>
> > (define-struct byte-port (bytes port) #:property prop:input-port 1)
> >
> > Are there any issues (cost, synchronization, etc) with using port structs
> > this way that I need to think about?
>
> No --- that should work well. There is an overhead in using the wrapped
> port, but it's small compared to most any operation on the port.
>

Thanks for verifying, Matthew.  I did a quick test and found that the
overhead is minimal.  I also compared against `substring` as reference, and
found that regular string port still has a bit of overhead above substring.
Is the overhead due to synchronization primitives?

Thanks,
yc

(define s "this is a string that we are going to read and peek from")
(define-struct wport (inner port) #:property prop:input-port 1)
(define (open-input-wport s)
  (make-wport s ((cond ((string? s) open-input-string)
                       ((bytes? s) open-input-bytes)
                       (else (error 'open-input-wport "Unknown type: ~a"
s))) s)))

(define (test-port in count)
  (for ((i (in-range 0 count)))
       (read-bytes 50 in)
       (file-position in 0)))

(define (test-string s count)
  (for ((i (in-range 0 count)))
       (substring s 0 50)))

(define (test count)
  (let ((in1 (open-input-string s))
        (in2 (open-input-wport s)))
    (time (test-port in1 count))
    (time (test-port in2 count))
    (time (test-string s count))
    ))

(test 500)
;; cpu time: 0 real time: 0 gc time: 0 ;; string port
;; cpu time: 1 real time: 1 gc time: 0 ;; wrapped port
;; cpu time: 0 real time: 0 gc time: 0 ;; substring

(test 5000)
;; cpu time: 3 real time: 4 gc time: 0
;; cpu time: 36 real time: 35 gc time: 31
;; cpu time: 2 real time: 2 gc time: 0

(test 50000)
;; cpu time: 30 real time: 30 gc time: 0
;; cpu time: 43 real time: 43 gc time: 1
;; cpu time: 14 real time: 14 gc time: 0

(test 500000)
;; cpu time: 195 real time: 195 gc time: 4
;; cpu time: 291 real time: 291 gc time: 4
;; cpu time: 134 real time: 134 gc time: 7

(test 5000000)
;; cpu time: 1982 real time: 1982 gc time: 64
;; cpu time: 3081 real time: 3080 gc time: 69
;; cpu time: 1464 real time: 1465 gc time: 205
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090116/b8016ff2/attachment.html>

Posted on the users mailing list.