<div dir="ltr">Marc,<br><br>just to ensure I got your point. You say that one could create a string-port more than 20 times faster than Gambit's built-in string ports. I'm interested in the reason - is this because:<br>
<br>a) If one writes one in scheme code, Gambit may perform function inlining?<br>b) You presume that the string-port implementation one writes does not need to be thread-safe, within the context of Gambit's threading system, as is Gambit's built in string port?<br>
<br>Any more reasons?<br><br>Also, what further reasons do you see as to why a SSAX-SXML XML to SXML deserialization that takes 3 seconds in Java takes 50 seconds in Gambit? I mean, in general, Gambit code is a lot faster than Java, so this is an exception to the general rule.<br>
<br>Can you further describe how to create a complete port implementation, such as a string port or a gzip port?<br><br>M<br><br><div class="gmail_quote">2008/8/16 Marc Feeley <span dir="ltr"><<a href="mailto:feeley@iro.umontreal.ca">feeley@iro.umontreal.ca</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">If you are really into optimizing the I/O, I suggest you implement<br>
your own special purpose string-port I/O layer.  Basically you<br>
redefine read-char to read from a string.  The code below shows that<br>
this can be over 20 times faster than Gambit's built-in string ports.<br>
The main reasons for the difference are function inlining and locking<br>
(to support multithreading).<br>
<br>
(define (convoluted-string-length1 str) ;; uses standard string ports<br>
   (declare (standard-bindings) (fixnum) (not safe))<br>
   (let ((port (open-input-string str)))<br>
     (let loop ((i 0))<br>
       (let ((c (read-char port)))<br>
         (if (char? c)<br>
             (loop (+ i 1))<br>
             i)))))<br>
<br>
(define-macro (macro-open-input-string str)<br>
   `(let ((str ,str))<br>
      (declare (standard-bindings) (fixnum) (not safe))<br>
      (cons str 0)))<br>
<br>
(define-macro (macro-read-char my-port)<br>
   `(let ((my-port ,my-port))<br>
      (declare (standard-bindings) (fixnum) (not safe))<br>
      (let ((str (car my-port))<br>
            (pos (cdr my-port)))<br>
        (if (< pos (string-length str))<br>
            (let ((c (string-ref str pos)))<br>
              (set-cdr! my-port (+ 1 pos))<br>
              c)<br>
            #!eof))))<br>
<br>
(define-macro (open-input-string str) `(macro-open-input-string ,str))<br>
(define-macro (read-char port) `(macro-read-char ,port))<br>
<br>
;; insert your code after this point, for example:<br>
<br>
(define (convoluted-string-length2 str) ;; uses "fast" string ports<br>
   (declare (standard-bindings) (fixnum) (not safe))<br>
   (let ((port (open-input-string str)))<br>
     (let loop ((i 0))<br>
       (let ((c (read-char port)))<br>
         (if (char? c)<br>
             (loop (+ i 1))<br>
             i)))))<br>
<br>
(define (test)<br>
   (let ((s (make-string 4000000 #\!)))<br>
     (pretty-print (time (convoluted-string-length1 s)))<br>
     (pretty-print (time (convoluted-string-length2 s)))))<br>
<br>
(test)<br>
<br>
;; (time (convoluted-string-length1 s))<br>
;;     704 ms real time<br>
;;     703 ms cpu time (661 user, 42 system)<br>
;;     1 collection accounting for 50 ms real time (32 user, 19 system)<br>
;;     36023288 bytes allocated<br>
;;     no minor faults<br>
;;     no major faults<br>
;; 4000000<br>
;; (time (convoluted-string-length2 s))<br>
;;     26 ms real time<br>
;;     25 ms cpu time (25 user, 0 system)<br>
;;     no collections<br>
;;     24 bytes allocated<br>
;;     no minor faults<br>
;;     no major faults<br>
;; 4000000<br>
<div><div></div><div class="Wj3C7c"><br>
_______________________________________________<br>
Gambit-list mailing list<br>
<a href="mailto:Gambit-list@iro.umontreal.ca">Gambit-list@iro.umontreal.ca</a><br>
<a href="https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list" target="_blank">https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list</a><br>
</div></div></blockquote></div><br></div>