From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <tarantool-patches-bounce@freelists.org>
Received: from localhost (localhost [127.0.0.1])
	by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 89BBF2B608
	for <tarantool-patches@freelists.org>; Sun, 28 Apr 2019 12:56:31 -0400 (EDT)
Received: from turing.freelists.org ([127.0.0.1])
	by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id D-Ak_1V5QZ2L for <tarantool-patches@freelists.org>;
	Sun, 28 Apr 2019 12:56:31 -0400 (EDT)
Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [94.100.181.251])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 41B262B814
	for <tarantool-patches@freelists.org>; Sun, 28 Apr 2019 12:56:31 -0400 (EDT)
From: Vladislav Shpilevoy <v.shpilevoy@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
Message-Id: <01f8b95e78623d7f56cded96324184b8d96cec13.1556470563.git.v.shpilevoy@tarantool.org>
In-Reply-To: <cover.1556470563.git.v.shpilevoy@tarantool.org>
References: <cover.1556470563.git.v.shpilevoy@tarantool.org>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Sender: tarantool-patches-bounce@freelists.org
Errors-to: tarantool-patches-bounce@freelists.org
Reply-To: tarantool-patches@freelists.org
List-Help: <mailto:ecartis@freelists.org?Subject=help>
List-Unsubscribe: <mailto:tarantool-patches-request@freelists.org?Subject=unsubscribe>
List-software: Ecartis version 1.0.0
List-Id: tarantool-patches <tarantool-patches.freelists.org>
List-Subscribe: <mailto:tarantool-patches-request@freelists.org?Subject=subscribe>
List-Owner: <mailto:>
List-post: <mailto:tarantool-patches@freelists.org>
List-Archive: <http://www.freelists.org/archives/tarantool-patches>
To: tarantool-patches@freelists.org
Cc: kostja@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)