From: Artem Boldariev Date: Mon, 2 Aug 2021 14:15:13 +0000 (+0300) Subject: Optimise TLS stream for small write size (>= 512 bytes) X-Git-Tag: v9.17.18~40^2~2 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=e639957b586a0824e32ef7db7f04cd1708fa8da7;p=thirdparty%2Fbind9.git Optimise TLS stream for small write size (>= 512 bytes) 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. --- diff --git a/lib/isc/netmgr/netmgr-int.h b/lib/isc/netmgr/netmgr-int.h index c75ddddfb8c..23cfe8cbf14 100644 --- a/lib/isc/netmgr/netmgr-int.h +++ b/lib/isc/netmgr/netmgr-int.h @@ -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 { diff --git a/lib/isc/netmgr/tlsstream.c b/lib/isc/netmgr/tlsstream.c index f5e65e930dc..0ccf68b4ebc 100644 --- a/lib/isc/netmgr/tlsstream.c +++ b/lib/isc/netmgr/tlsstream.c @@ -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) {