git » nmdb » commit acdce09

Put static buffers as static global variables instead of using the stack.

author Alberto Bertogli
2007-06-07 06:21:04 UTC
committer Alberto Bertogli
2007-06-07 06:21:04 UTC
parent 02298183c22ad591a00cbff7859b0803a58f3a54

Put static buffers as static global variables instead of using the stack.

It's much more decent to put them there and not abuse the stack.
TCP already did this (although it used more variables than really needed),
but not TIPC nor UDP.

Signed-off-by: Alberto Bertogli <albertito@gmail.com>

nmdb/tcp.c +1 -4
nmdb/tipc.c +10 -13
nmdb/udp.c +10 -13

diff --git a/nmdb/tcp.c b/nmdb/tcp.c
index 415cabe..c453222 100644
--- a/nmdb/tcp.c
+++ b/nmdb/tcp.c
@@ -323,16 +323,13 @@ static unsigned char static_buf[SBSIZE];
 static void tcp_recv(int fd, short event, void *arg)
 {
 	int rv;
-	size_t bsize;
 	struct tcp_socket *tcpsock;
 
 	tcpsock = (struct tcp_socket *) arg;
 
 	if (tcpsock->buf == NULL) {
 		/* New incoming message */
-		bsize = SBSIZE;
-
-		rv = recv(fd, static_buf, bsize, 0);
+		rv = recv(fd, static_buf, SBSIZE, 0);
 		if (rv < 0 && errno == EAGAIN) {
 			/* We were awoken but have no data to read, so we do
 			 * nothing */
diff --git a/nmdb/tipc.c b/nmdb/tipc.c
index 3b92fbb..eb73bc4 100644
--- a/nmdb/tipc.c
+++ b/nmdb/tipc.c
@@ -188,6 +188,14 @@ void tipc_close(int fd)
 }
 
 
+/* Static common buffer to avoid unnecessary allocations.
+ * Originally, this was malloc()ed, but making it static made it go from 27
+ * usec for each set operation, to 23 usec: it made test1 go from 3.213s to
+ * 2.345s for 37618 operations.
+ * Allocate enough to hold the max msg length of 64kbytes. */
+#define SBSIZE (68 * 1024)
+static unsigned char static_buf[SBSIZE];
+
 /* Called by libevent for each receive event */
 void tipc_recv(int fd, short event, void *arg)
 {
@@ -195,21 +203,10 @@ void tipc_recv(int fd, short event, void *arg)
 	struct req_info req;
 	struct sockaddr_tipc clisa;
 	socklen_t clilen;
-	size_t bsize;
-
-	/* Allocate enough to hold the max msg length of 66000 bytes.
-	 * Originally, this was malloc()ed, but using the stack made it go
-	 * from 27 usec for each set operation, to 23 usec. While it may sound
-	 * worthless, it made test1 go from 3.213s to 2.345s for 37618
-	 * operations.
-	 * TODO: check for negative impacts (beside being ugly, obviously)
-	 */
-	unsigned char buf[68 * 1024];
-	bsize = 68 * 1024;
 
 	clilen = sizeof(clisa);
 
-	rv = recvfrom(fd, buf, bsize, 0, (struct sockaddr *) &clisa,
+	rv = recvfrom(fd, static_buf, SBSIZE, 0, (struct sockaddr *) &clisa,
 			&clilen);
 	if (rv <= 0) {
 		/* rv == 0 means "return of an undeliverable message", which
@@ -234,7 +231,7 @@ void tipc_recv(int fd, short event, void *arg)
 	req.reply_cas = tipc_reply_cas;
 
 	/* parse the message */
-	parse_message(&req, buf, rv);
+	parse_message(&req, static_buf, rv);
 
 exit:
 	return;
diff --git a/nmdb/udp.c b/nmdb/udp.c
index d9140a2..7f69bdb 100644
--- a/nmdb/udp.c
+++ b/nmdb/udp.c
@@ -193,6 +193,14 @@ void udp_close(int fd)
 }
 
 
+/* Static common buffer to avoid unnecessary allocations.
+ * Originally, this was malloc()ed, but making it static made it go from 27
+ * usec for each set operation, to 23 usec: it made test1 go from 3.213s to
+ * 2.345s for 37618 operations.
+ * Allocate enough to hold the max msg length of 64kbytes. */
+#define SBSIZE (68 * 1024)
+static unsigned char static_buf[SBSIZE];
+
 /* Called by libevent for each receive event */
 void udp_recv(int fd, short event, void *arg)
 {
@@ -200,21 +208,10 @@ void udp_recv(int fd, short event, void *arg)
 	struct req_info req;
 	struct sockaddr_in clisa;
 	socklen_t clilen;
-	size_t bsize;
-
-	/* Allocate enough to hold the max msg length of 66000 bytes.
-	 * Originally, this was malloc()ed, but using the stack made it go
-	 * from 27 usec for each set operation, to 23 usec. While it may sound
-	 * worthless, it made test1 go from 3.213s to 2.345s for 37618
-	 * operations.
-	 * TODO: check for negative impacts (beside being ugly, obviously)
-	 */
-	unsigned char buf[68 * 1024];
-	bsize = 68 * 1024;
 
 	clilen = sizeof(clisa);
 
-	rv = recvfrom(fd, buf, bsize, 0, (struct sockaddr *) &clisa,
+	rv = recvfrom(fd, static_buf, SBSIZE, 0, (struct sockaddr *) &clisa,
 			&clilen);
 	if (rv < 0) {
 		goto exit;
@@ -237,7 +234,7 @@ void udp_recv(int fd, short event, void *arg)
 	req.reply_cas = udp_reply_cas;
 
 	/* parse the message */
-	parse_message(&req, buf, rv);
+	parse_message(&req, static_buf, rv);
 
 exit:
 	return;