Making zo-marshal more like C and not with large byte strings

This commit is contained in:
Jay McCarthy 2010-05-24 12:45:42 -06:00
parent f44e3123b5
commit 40e1ba95fc

View File

@ -1,5 +1,6 @@
#lang scheme/base #lang scheme/base
(require compiler/zo-structs (require compiler/zo-structs
unstable/byte-counting-port
scheme/match scheme/match
scheme/contract scheme/contract
scheme/local scheme/local
@ -41,34 +42,37 @@
(when (closure? v) (when (closure? v)
(hash-set! shared v (add1 (hash-count shared)))) (hash-set! shared v (add1 (hash-count shared))))
#t)))) #t))))
(parameterize ([current-wrapped-ht wrapped]) (define (v-skipping v)
(traverse-prefix prefix visit) (define skip? #t)
(traverse-form form visit))
(let* ([s (open-output-bytes)]
[out (make-out s (lambda (v) (hash-ref shared v #f)) wrapped)]
[offsets
(map (lambda (k*v)
(define v (cdr k*v))
(begin0
(file-position s)
(out-anything v (make-out
s
(let ([skip? #t])
(lambda (v2) (lambda (v2)
(if (and skip? (eq? v v2)) (if (and skip? (eq? v v2))
(begin (begin
(set! skip? #f) (set! skip? #f)
#f) #f)
(hash-ref shared v2 #f)))) (hash-ref shared v2 #f))))
wrapped)))) (parameterize ([current-wrapped-ht wrapped])
(traverse-prefix prefix visit)
(traverse-form form visit))
(local [(define in-order-shareds
(sort (hash-map shared (lambda (k v) (cons v k))) (sort (hash-map shared (lambda (k v) (cons v k)))
< <
#:key car))] #:key car))
[post-shared (file-position s)] (define (write-all outp)
[all-short? (post-shared . < . #xFFFF)] (define offsets
[version-bs (string->bytes/latin-1 (version))]) (for/list ([k*v (in-list in-order-shareds)])
(out-data (list* max-let-depth prefix (protect-quote form)) out) (define v (cdr k*v))
(let ([res (get-output-bytes s)]) (begin0
(file-position outp)
(out-anything v (make-out outp (v-skipping v) wrapped)))))
(define post-shared (file-position outp))
(out-data (list* max-let-depth prefix (protect-quote form))
(make-out outp (lambda (v) (hash-ref shared v #f)) wrapped))
(values offsets post-shared (file-position outp)))
(define counting-p (make-byte-counting-port))
(define-values (offsets post-shared all-forms-length)
(write-all counting-p))
(define all-short? (post-shared . < . #xFFFF))
(define version-bs (string->bytes/latin-1 (version)))]
(write-bytes #"#~" outp) (write-bytes #"#~" outp)
(write-bytes (bytes (bytes-length version-bs)) outp) (write-bytes (bytes (bytes-length version-bs)) outp)
(write-bytes version-bs outp) (write-bytes version-bs outp)
@ -77,8 +81,9 @@
(for ([o (in-list offsets)]) (for ([o (in-list offsets)])
(write-bytes (integer->integer-bytes o (if all-short? 2 4) #f #f) outp)) (write-bytes (integer->integer-bytes o (if all-short? 2 4) #f #f) outp))
(write-bytes (int->bytes post-shared) outp) (write-bytes (int->bytes post-shared) outp)
(write-bytes (int->bytes (bytes-length res)) outp) (write-bytes (int->bytes all-forms-length) outp)
(write-bytes res outp)))])) (write-all outp)
(void))]))
;; ---------------------------------------- ;; ----------------------------------------