Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@freelists.org
Cc: kostja@tarantool.org
Subject: [tarantool-patches] [PATCH 3/3] sio: optimize sio_strfaddr() for the most common case
Date: Sun, 28 Apr 2019 19:56:27 +0300	[thread overview]
Message-ID: <01f8b95e78623d7f56cded96324184b8d96cec13.1556470563.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1556470563.git.v.shpilevoy@tarantool.org>

SIO library provides a wrapper for getnameinfo able to stringify
Unix socket addresses. But it does not care about limited
Tarantool stack and allocates buffers for getnameinfo() right on
it - ~1Kb. Besides, after successful getnameinfo() the result is
copied onto another static buffer.

This patch optimizes sio_strfaddr() for the most common case -
AF_INET, when 32 bytes is more than enough for any IP:Port pair,
and writes the result into the target buffer directly.

The main motivation behind this commit is that SWIM makes active
use of sio_strfaddr() for logging - for each received/sent
message it writes a couple of addresses into a log. It does it in
verbose mode, but the say() function arguments are still
calculated even when the active mode is lower than verbose.
---
 src/lib/core/sio.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/lib/core/sio.c b/src/lib/core/sio.c
index 996b7faad..28aef1d6c 100644
--- a/src/lib/core/sio.c
+++ b/src/lib/core/sio.c
@@ -323,6 +323,12 @@ sio_strfaddr(const struct sockaddr *addr, socklen_t addrlen)
 				return tt_sprintf("unix/:(socket)");
 			break;
 		}
+		case AF_INET: {
+			struct sockaddr_in *in = (struct sockaddr_in *) addr;
+			return tt_snprintf(SERVICE_NAME_MAXLEN, "%s:%d",
+					   inet_ntoa(in->sin_addr),
+					   ntohs(in->sin_port));
+		}
 		default: {
 			char host[NI_MAXHOST], serv[NI_MAXSERV];
 			if (getnameinfo(addr, addrlen, host, sizeof(host),
@@ -330,9 +336,8 @@ sio_strfaddr(const struct sockaddr *addr, socklen_t addrlen)
 					NI_NUMERICHOST | NI_NUMERICSERV) != 0)
 				return tt_sprintf("(host):(port)");
 
-			return tt_snprintf(NI_MAXHOST + NI_MAXSERV,
-					   addr->sa_family == AF_INET ?
-					   "%s:%s" : "[%s]:%s", host, serv);
+			return tt_snprintf(NI_MAXHOST + NI_MAXSERV, "[%s]:%s",
+					   host, serv);
 		}
 	}
 	unreachable();
-- 
2.20.1 (Apple Git-117)

  parent reply	other threads:[~2019-04-28 16:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-28 16:56 [tarantool-patches] [PATCH 0/3] introduce static allocator Vladislav Shpilevoy
2019-04-28 16:56 ` [tarantool-patches] [PATCH 1/3] small: introduce small/static Vladislav Shpilevoy
2019-04-28 16:56 ` [tarantool-patches] [PATCH 2/3] Use static_alloc() instead of 'static char[]' where possible Vladislav Shpilevoy
2019-04-28 16:56 ` Vladislav Shpilevoy [this message]
2019-05-14  8:09   ` [tarantool-patches] [PATCH 3/3] sio: optimize sio_strfaddr() for the most common case Vladimir Davydov
2019-04-29  4:14 ` [tarantool-patches] Re: [PATCH 0/3] introduce static allocator Konstantin Osipov
2019-04-29  8:35   ` Vladislav Shpilevoy
2019-05-13 22:57 ` Vladislav Shpilevoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=01f8b95e78623d7f56cded96324184b8d96cec13.1556470563.git.v.shpilevoy@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [tarantool-patches] [PATCH 3/3] sio: optimize sio_strfaddr() for the most common case' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox