Tweak the knots chunked output port
All checks were successful
/ test (push) Successful in 9s

To try and reduce the number of write syscalls.
This commit is contained in:
Christopher Baines 2025-06-30 15:41:04 +02:00
parent deae518b52
commit 7709ffe1d3

View file

@ -63,6 +63,14 @@
(bind sock family addr port)
sock))
(define crlf-bv
(string->utf8 "\r\n"))
(define (chunked-output-port-overhead-bytes write-size)
(+ (string-length (number->string write-size 16))
(bytevector-length crlf-bv)
(bytevector-length crlf-bv)))
(define* (make-chunked-output-port/knots port #:key (keep-alive? #f)
(buffering 1200))
"Returns a new port which translates non-encoded data into a HTTP
@ -74,10 +82,12 @@ when done, as it will output the remaining data, and encode the final
zero chunk. When the port is closed it will also close PORT, unless
KEEP-ALIVE? is true."
(define (write! bv start count)
(put-string port (number->string count 16))
(put-string port "\r\n")
(let ((len-string
(number->string count 16)))
(put-string port len-string))
(put-bytevector port crlf-bv 0 2)
(put-bytevector port bv start count)
(put-string port "\r\n")
(put-bytevector port crlf-bv 0 2)
(force-output port)
count)
@ -401,7 +411,10 @@ on the procedure being called at any particular time."
(make-chunked-output-port/knots
client
#:keep-alive? #t
#:buffering buffer-size))))
#:buffering
(- buffer-size
(chunked-output-port-overhead-bytes
buffer-size))))))
(set-port-encoding! body-port charset)
(let ((body-written?
(with-exception-handler