Re: fast serialization of huge bit vectors?
Jens Thiele <[email protected]> Tue, 05 May 2026 11:14:06 +0200
| Newsgroups | gmane.lisp.scheme.gauche |
|---|---|
| Message-ID | <[email protected]> |
just in case somebody wants a quick hack for now. Something like this
should work:
(define-module bitvector-io
(use gauche.bitvector)
(use binary.io)
(use runtime-compile)
(export read-bitvector
write-bitvector))
(select-module bitvector-io)
(compile-and-load
`((inline-stub
(define-cproc read-bitvector-bits (v::<bitvector> p::<input-port>)
(let* ((bytes::ScmSize (* (+ (/ (SCM_BITVECTOR_SIZE v) SCM_WORD_BITS)
(!= (% (SCM_BITVECTOR_SIZE v) SCM_WORD_BITS) 0))
(/ SCM_WORD_BITS 8)))
(r::ScmSize (Scm_Getz (cast (char *) (SCM_BITVECTOR_BITS v)) bytes p)))
(return (SCM_MAKE_BOOL (== bytes r)))))
(define-cproc write-bitvector-bits (v::<bitvector> p::<output-port>)
(Scm_Putz (cast (char *) (SCM_BITVECTOR_BITS v))
(* (+ (/ (SCM_BITVECTOR_SIZE v) SCM_WORD_BITS)
(!= (% (SCM_BITVECTOR_SIZE v) SCM_WORD_BITS) 0))
(/ SCM_WORD_BITS 8))
p)
(return SCM_UNDEFINED))))
'(read-bitvector-bits write-bitvector-bits))
(define (read-bitvector)
(let1 len (read-u64)
(rlet1 r (make-bitvector len)
(when (not (read-bitvector-bits r (current-input-port)))
(error "read-bitvector: read failure")))))
(define (write-bitvector v)
(write-u64 (bitvector-length v))
(write-bitvector-bits v (current-output-port)))
;; (let1 v #*11111111111111111111111111111111111111111111111111111111111111111 (with-output-to-file "/tmp/v" (lambda() (write-bitvector v))) (equal? v (with-input-from-file "/tmp/v" (lambda() (read-bitvector)))))