dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] ext/socket/ancdata.c (bsock_recvmsg_internal): reduce stack use
@ 2015-06-17 20:55 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2015-06-17 20:55 UTC (permalink / raw)
  To: spew

Using 8K stack is probably too much.  As reference, ALLOCV falls
back to heap allocation at a mere 1K.  Since
bsock_recvmsg_internal is a function which will always allocate
and can trigger GC, it is in our best interest to minimize
stack usage to avoid scanning 8K of stack on GC.
---
 ext/socket/ancdata.c | 38 +++++++++++++-------------------------
 1 file changed, 13 insertions(+), 25 deletions(-)

diff --git a/ext/socket/ancdata.c b/ext/socket/ancdata.c
index 614c8f3..d0290ba 100644
--- a/ext/socket/ancdata.c
+++ b/ext/socket/ancdata.c
@@ -1498,7 +1498,7 @@ bsock_recvmsg_internal(int argc, VALUE *argv, VALUE sock, int nonblock)
     struct msghdr mh;
     struct iovec iov;
     union_sockaddr namebuf;
-    char datbuf0[4096], *datbuf;
+    char *datbuf;
     VALUE dat_str = Qnil;
     VALUE ret;
     ssize_t ss;
@@ -1506,10 +1506,6 @@ bsock_recvmsg_internal(int argc, VALUE *argv, VALUE sock, int nonblock)
 #if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
     struct cmsghdr *cmh;
     size_t maxctllen;
-    union {
-        char bytes[4096];
-        struct cmsghdr align;
-    } ctlbuf0;
     char *ctlbuf;
     VALUE ctl_str = Qnil;
     int family;
@@ -1519,9 +1515,9 @@ bsock_recvmsg_internal(int argc, VALUE *argv, VALUE sock, int nonblock)
 
     rb_scan_args(argc, argv, "03:", &vmaxdatlen, &vflags, &vmaxctllen, &vopts);
 
-    maxdatlen = NIL_P(vmaxdatlen) ? sizeof(datbuf0) : NUM2SIZET(vmaxdatlen);
+    maxdatlen = NIL_P(vmaxdatlen) ? 4096 : NUM2SIZET(vmaxdatlen);
 #if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
-    maxctllen = NIL_P(vmaxctllen) ? sizeof(ctlbuf0) : NUM2SIZET(vmaxctllen);
+    maxctllen = NIL_P(vmaxctllen) ? 4096 : NUM2SIZET(vmaxctllen);
 #else
     if (!NIL_P(vmaxctllen))
         rb_raise(rb_eArgError, "control message not supported");
@@ -1561,26 +1557,18 @@ bsock_recvmsg_internal(int argc, VALUE *argv, VALUE sock, int nonblock)
 #endif
 
   retry:
-    if (maxdatlen <= sizeof(datbuf0))
-        datbuf = datbuf0;
-    else {
-        if (NIL_P(dat_str))
-            dat_str = rb_str_tmp_new(maxdatlen);
-        else
-            rb_str_resize(dat_str, maxdatlen);
-        datbuf = RSTRING_PTR(dat_str);
-    }
+    if (NIL_P(dat_str))
+	dat_str = rb_str_tmp_new(maxdatlen);
+    else
+	rb_str_resize(dat_str, maxdatlen);
+    datbuf = RSTRING_PTR(dat_str);
 
 #if defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)
-    if (maxctllen <= sizeof(ctlbuf0))
-        ctlbuf = ctlbuf0.bytes;
-    else {
-        if (NIL_P(ctl_str))
-            ctl_str = rb_str_tmp_new(maxctllen);
-        else
-            rb_str_resize(ctl_str, maxctllen);
-        ctlbuf = RSTRING_PTR(ctl_str);
-    }
+    if (NIL_P(ctl_str))
+	ctl_str = rb_str_tmp_new(maxctllen);
+    else
+	rb_str_resize(ctl_str, maxctllen);
+    ctlbuf = RSTRING_PTR(ctl_str);
 #endif
 
     memset(&mh, 0, sizeof(mh));
-- 
EW


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2015-06-17 20:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-17 20:55 [PATCH] ext/socket/ancdata.c (bsock_recvmsg_internal): reduce stack use Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).