This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch

[PATCH racket-rfc8949] Make read-n-bytes performant (tm)

Message ID
DKIM signature
Download raw message
Patch: +12 -21
From: Alwinfy <20421383+Alwinfy@users.noreply.github.com>

 main.rkt | 33 ++++++++++++---------------------
 1 file changed, 12 insertions(+), 21 deletions(-)

diff --git a/main.rkt b/main.rkt
index 73366e5..430069d 100644
--- a/main.rkt
+++ b/main.rkt
@@ -8,15 +8,13 @@
; Parameter that holds the object used to represent undefined
(define cbor-undefined (make-parameter 'undefined #f 'cbor-undefined))

; read n bytes from port into buf. n is inferred from the length of buf
(define (read-n-bytes buf port)
  (define n (bytes-length buf))
  (let loop ([cur 0])
    (if (= cur n)
          (bytes-set! buf cur (read-byte port))
          (loop (add1 cur))))))
; read exactly n bytes, or throw
(define (read-n-bytes n port)
  (let* ([bytes (read-bytes n port)]
         [len   (if (eof-object? bytes) 0 (bytes-length bytes))])
    (if (= len n)
      (error "Unexpected EOF: Expected ~a bytes, got ~a" n len))))

(define (read-arg-as-int port major additional)
  (if (< additional 24)
@@ -58,14 +56,13 @@
(define (cbor-read-byte-string port additional)
  (define payload-len (read-arg-as-int port 2 additional))
  ; todo implement 'indefinite and 'break support
  (let ([buf (make-bytes payload-len)])
    (bytes->immutable-bytes (read-n-bytes buf port))))
  (let ([buf (read-n-bytes payload-len port)])
    (bytes->immutable-bytes buf)))

(define (cbor-read-string port additional)
  (define payload-len (read-arg-as-int port 3 additional))
  ; todo implement 'indefinite and 'break support
  (let ([buf (make-bytes payload-len)])
    (read-n-bytes buf port)
  (let ([buf (read-n-bytes payload-len port)])
    (string->immutable-string (bytes->string/utf-8 buf))))

(define (cbor-read-list port additional)
@@ -135,14 +132,8 @@
            (if (zero? (bitwise-and half #x8000))
                (- val))))]
         (define buf (bytes 0 0 0 0))
         (read-n-bytes buf port)
         (floating-point-bytes->real buf #t)]
         (define buf (bytes 0 0 0 0 0 0 0 0))
         (read-n-bytes buf port)
         (floating-point-bytes->real buf #t)]
        [(26) (floating-point-bytes->real (read-n-bytes 4 port) #t)]
        [(27) (floating-point-bytes->real (read-n-bytes 8 port) #t)]
        [(28 29 30) (error "Additional information ~a is reserved in major type 7" additional)]
        [(31) (error "Unexpected break code")]))) ; We handle break explicitly in deserialization of lists of things, so it should never appear here.

Reply to thread Export thread (mbox)