]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Optimise TLS stream for small write size (>= 512 bytes)
authorArtem Boldariev <artem@boldariev.com>
Mon, 2 Aug 2021 14:15:13 +0000 (17:15 +0300)
committerArtem Boldariev <artem@boldariev.com>
Thu, 12 Aug 2021 11:28:17 +0000 (14:28 +0300)
This commit changes TLS stream behaviour in such a way, that it is now
optimised for small writes. In the case there is a need to write less
or equal to 512 bytes, we could avoid calling the memory allocator at
the expense of possibly slight increase in memory usage. In case of
larger writes, the behviour remains unchanged.

lib/isc/netmgr/netmgr-int.h
lib/isc/netmgr/tlsstream.c

index c75ddddfb8ca646e3f699c2df8b26f60f302a80f..23cfe8cbf14f40267f8990635ee1a7672295b9b6 100644 (file)
@@ -754,6 +754,7 @@ typedef struct isc_nmsocket_tls_send_req {
        void *cbarg;
        isc_nmhandle_t *handle;
        bool finish;
+       uint8_t smallbuf[512];
 } isc_nmsocket_tls_send_req_t;
 
 typedef enum isc_http_request_type {
index f5e65e930dcf747c1d6fe5221adb98dbf48213e2..0ccf68b4ebc7ab3227e6c5e3cf9ecf95ead0d566 100644 (file)
@@ -123,8 +123,15 @@ tls_senddone(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
                }
        }
 
-       isc_mem_put(handle->sock->mgr->mctx, send_req->data.base,
-                   send_req->data.length);
+       /* We are tying to avoid a memory allocation for small write
+        * requests. See the mirroring code in the tls_send_outgoing()
+        * function. */
+       if (ISC_UNLIKELY(send_req->data.length > sizeof(send_req->smallbuf))) {
+               isc_mem_put(handle->sock->mgr->mctx, send_req->data.base,
+                           send_req->data.length);
+       } else {
+               INSIST(&send_req->smallbuf[0] == send_req->data.base);
+       }
        isc_mem_put(handle->sock->mgr->mctx, send_req, sizeof(*send_req));
        tlssock->tlsstream.nsending--;
 
@@ -236,11 +243,15 @@ tls_send_outgoing(isc_nmsocket_t *sock, bool finish, isc_nmhandle_t *tlshandle,
        }
 
        send_req = isc_mem_get(sock->mgr->mctx, sizeof(*send_req));
-       *send_req = (isc_nmsocket_tls_send_req_t){
-               .finish = finish,
-               .data.base = isc_mem_get(sock->mgr->mctx, pending),
-               .data.length = pending
-       };
+       *send_req = (isc_nmsocket_tls_send_req_t){ .finish = finish,
+                                                  .data.length = pending };
+
+       /* Let's try to avoid a memory allocation for small write requests */
+       if (ISC_UNLIKELY((size_t)pending > sizeof(send_req->smallbuf))) {
+               send_req->data.base = isc_mem_get(sock->mgr->mctx, pending);
+       } else {
+               send_req->data.base = &send_req->smallbuf[0];
+       }
 
        isc__nmsocket_attach(sock, &send_req->tlssock);
        if (cb != NULL) {