[tarantool-patches] [PATCH 3/3] sio: optimize sio_strfaddr() for the most common case

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Sun Apr 28 19:56:27 MSK 2019


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)





More information about the Tarantool-patches mailing list