Schema: data serialization, efficient [and functional]

I am serializing data, which can be an integer, an object (list) with other nested objects, and try to make a choice as to which approach to use. The first two are to recursively create byte vectors and copy them in the calling functions to a larger one byte vector; the second is to use some kind of stream that I could write. In the end, despite the choice, I could use the resulting binary array in any further processing that might happen, for example. d. I would compress the output and send it over the network or just write some parts of it to a file.

I would like to remain functional enough (or fully), but implementing a fast serializer. I use Racket , although any other implementation of the Scheme will also perform.

Thank.

UPDATE:

The following are examples that I added after I found a solution so that users could save time looking for ways to write data:]

write-byteand write-bytesespecially useful when you need to write octets.

> (bytes? (with-output-to-bytes (lambda () (write-byte 42))))
#t
> (bytevector? (with-output-to-bytes (lambda () (write-byte 42))))
#t
> (bytevector->u8-list (with-output-to-bytes (lambda () (write-byte 42))))
{42}
> (bytes->list (with-output-to-bytes (lambda () (write-byte 42) (write-bytes (integer->integer-bytes #x101CA75 4 #f #t)))))
(42 1 1 202 117)
+3
source share
2 answers

write . , , . , , , , print-graph #t . , , open-output-bytes with-output-to-bytes:

(with-output-to-bytes (lambda () (write (list value1 value2 value3))))

, , , .

+4

, open-bytevector-output-port - , :

#lang scheme

(require rnrs/bytevectors-6)
(require rnrs/io/ports-6)

(define-values (oup ext-proc) (open-bytevector-output-port))
(write 4 oup)
(write 2 oup)
(ext-proc)
(make-bytevector 3 1)

:

   DrScheme,  4.2.5 [3m].
: scheme; memory limit: 128 MB.
#"42"
#"\1\1\1"
> 
+1

Source: https://habr.com/ru/post/1756935/


All Articles