[前][次][番号順一覧][スレッド一覧]

ruby-changes:38922

From: normal <ko1@a...>
Date: Wed, 24 Jun 2015 08:41:14 +0900 (JST)
Subject: [ruby-changes:38922] normal:r51003 (trunk): ext/socket/ancdata.c (bsock_recvmsg_internal): reduce stack use

normal	2015-06-24 08:40:53 +0900 (Wed, 24 Jun 2015)

  New Revision: 51003

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51003

  Log:
    ext/socket/ancdata.c (bsock_recvmsg_internal): reduce stack use
    
    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.
    
    [ruby-core:69595] [Feature #11263]

  Modified files:
    trunk/ChangeLog
    trunk/ext/socket/ancdata.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 51002)
+++ ChangeLog	(revision 51003)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Jun 24 08:28:15 2015  Eric Wong  <e@8...>
+
+	* ext/socket/ancdata.c (bsock_recvmsg_internal): reduce stack use
+	  [ruby-core:69595] [Feature #11263]
+
 Tue Jun 23 14:32:42 2015  Nobuyoshi Nakada  <nobu@r...>
 
 	* error.c (name_err_receiver): raise ArgumentError if no receiver
Index: ext/socket/ancdata.c
===================================================================
--- ext/socket/ancdata.c	(revision 51002)
+++ ext/socket/ancdata.c	(revision 51003)
@@ -1498,7 +1498,7 @@ bsock_recvmsg_internal(int argc, VALUE * https://github.com/ruby/ruby/blob/trunk/ext/socket/ancdata.c#L1498
     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 * https://github.com/ruby/ruby/blob/trunk/ext/socket/ancdata.c#L1506
 #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 * https://github.com/ruby/ruby/blob/trunk/ext/socket/ancdata.c#L1515
 
     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 * https://github.com/ruby/ruby/blob/trunk/ext/socket/ancdata.c#L1557
 #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));

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]