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

ruby-changes:38831

From: normal <ko1@a...>
Date: Tue, 16 Jun 2015 05:03:18 +0900 (JST)
Subject: [ruby-changes:38831] normal:r50912 (trunk): socket: allow explicit buffer for recv and recv_nonblock

normal	2015-06-16 05:02:43 +0900 (Tue, 16 Jun 2015)

  New Revision: 50912

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

  Log:
    socket: allow explicit buffer for recv and recv_nonblock
    
    This reduces GC overhead and makes the API more consistent
    with IO#read and IO#read_nonblock.
    
    * ext/socket/basicsocket.c (bsock_recv): document outbuf
    * ext/socket/unixsocket.c (unix_recvfrom): ditto
    * ext/socket/init.c (rsock_strbuf, recvfrom_locktmp): new functions
      (rsock_s_recvfrom): support destination buffer as 3rd arg
      (rsock_s_recvfrom_nonblock): ditto
    * string.c (rb_str_locktmp_ensure): export for internal ext
    * test/socket/test_nonblock.rb: test recv_nonblock
    * test/socket/test_unix.rb: test recv
      [ruby-core:69543] [Feature #11242]
    
    Benchmark results:
    
                 user     system      total        real
    alloc    0.130000   0.280000   0.410000 (  0.420656)
    extbuf   0.100000   0.220000   0.320000 (  0.318708)
    
    -------------------8<--------------------
    require 'socket'
    require 'benchmark'
    nr = 100000
    msg = ' ' * 16384
    size = msg.bytesize
    buf = ' ' * size
    UNIXSocket.pair(:DGRAM) do |a, b|
      Benchmark.bmbm do |x|
        x.report('alloc') do
          nr.times do
            b.send(msg, 0)
            a.recv(size, 0)
          end
        end
    
        x.report('extbuf') do
          nr.times do
            b.send(msg, 0)
            a.recv(size, 0, buf)
          end
        end
      end
    end

  Modified files:
    trunk/ChangeLog
    trunk/ext/socket/basicsocket.c
    trunk/ext/socket/init.c
    trunk/ext/socket/unixsocket.c
    trunk/string.c
    trunk/test/socket/test_nonblock.rb
    trunk/test/socket/test_unix.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50911)
+++ ChangeLog	(revision 50912)
@@ -1,3 +1,15 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Jun 16 04:50:44 2015  Eric Wong  <e@8...>
+
+	* ext/socket/basicsocket.c (bsock_recv): document outbuf
+	* ext/socket/unixsocket.c (unix_recvfrom): ditto
+	* ext/socket/init.c (rsock_strbuf, recvfrom_locktmp): new functions
+	  (rsock_s_recvfrom): support destination buffer as 3rd arg
+	  (rsock_s_recvfrom_nonblock): ditto
+	* string.c (rb_str_locktmp_ensure): export for internal ext
+	* test/socket/test_nonblock.rb: test recv_nonblock
+	* test/socket/test_unix.rb: test recv
+	  [ruby-core:69543] [Feature #11242]
+
 Tue Jun 16 04:38:02 2015  Eric Wong  <e@8...>
 
 	* ext/socket/ancdata.c (bsock_sendmsg_internal,
Index: string.c
===================================================================
--- string.c	(revision 50911)
+++ string.c	(revision 50912)
@@ -2107,7 +2107,7 @@ rb_str_unlocktmp(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L2107
     return str;
 }
 
-VALUE
+RUBY_FUNC_EXPORTED VALUE
 rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg)
 {
     rb_str_locktmp(str);
Index: ext/socket/init.c
===================================================================
--- ext/socket/init.c	(revision 50911)
+++ ext/socket/init.c	(revision 50912)
@@ -108,21 +108,48 @@ recvfrom_blocking(void *data) https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L108
     return (VALUE)ret;
 }
 
+static VALUE
+rsock_strbuf(VALUE str, long buflen)
+{
+    long len;
+
+    if (NIL_P(str)) return rb_tainted_str_new(0, buflen);
+
+    StringValue(str);
+    len = RSTRING_LEN(str);
+    if (len >= buflen) {
+	rb_str_modify(str);
+    } else {
+	rb_str_modify_expand(str, buflen - len);
+    }
+    rb_str_set_len(str, buflen);
+    return str;
+}
+
+static VALUE
+recvfrom_locktmp(VALUE v)
+{
+    struct recvfrom_arg *arg = (struct recvfrom_arg *)v;
+
+    return rb_thread_io_blocking_region(recvfrom_blocking, arg, arg->fd);
+}
+
 VALUE
 rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from)
 {
     rb_io_t *fptr;
-    VALUE str, klass;
+    VALUE str;
     struct recvfrom_arg arg;
     VALUE len, flg;
     long buflen;
     long slen;
 
-    rb_scan_args(argc, argv, "11", &len, &flg);
+    rb_scan_args(argc, argv, "12", &len, &flg, &str);
 
     if (flg == Qnil) arg.flags = 0;
     else             arg.flags = NUM2INT(flg);
     buflen = NUM2INT(len);
+    str = rsock_strbuf(str, buflen);
 
     GetOpenFile(sock, fptr);
     if (rb_io_read_pending(fptr)) {
@@ -130,24 +157,18 @@ rsock_s_recvfrom(VALUE sock, int argc, V https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L157
     }
     arg.fd = fptr->fd;
     arg.alen = (socklen_t)sizeof(arg.buf);
-
-    arg.str = str = rb_tainted_str_new(0, buflen);
-    klass = RBASIC(str)->klass;
-    rb_obj_hide(str);
+    arg.str = str;
 
     while (rb_io_check_closed(fptr),
 	   rsock_maybe_wait_fd(arg.fd),
-	   (slen = BLOCKING_REGION_FD(recvfrom_blocking, &arg)) < 0) {
+	   (slen = (long)rb_str_locktmp_ensure(str, recvfrom_locktmp,
+	                                       (VALUE)&arg)) < 0) {
         if (!rb_io_wait_readable(fptr->fd)) {
             rb_sys_fail("recvfrom(2)");
         }
-	if (RBASIC(str)->klass || RSTRING_LEN(str) != buflen) {
-	    rb_raise(rb_eRuntimeError, "buffer string modified");
-	}
     }
 
-    rb_obj_reveal(str, klass);
-    if (slen < RSTRING_LEN(str)) {
+    if (slen != RSTRING_LEN(str)) {
 	rb_str_set_len(str, slen);
     }
     rb_obj_taint(str);
@@ -191,11 +212,12 @@ rsock_s_recvfrom_nonblock(VALUE sock, in https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L212
     VALUE opts = Qnil;
     socklen_t len0;
 
-    rb_scan_args(argc, argv, "11:", &len, &flg, &opts);
+    rb_scan_args(argc, argv, "12:", &len, &flg, &str, &opts);
 
     if (flg == Qnil) flags = 0;
     else             flags = NUM2INT(flg);
     buflen = NUM2INT(len);
+    str = rsock_strbuf(str, buflen);
 
 #ifdef MSG_DONTWAIT
     /* MSG_DONTWAIT avoids the race condition between fcntl and recvfrom.
@@ -209,8 +231,6 @@ rsock_s_recvfrom_nonblock(VALUE sock, in https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L231
     }
     fd = fptr->fd;
 
-    str = rb_tainted_str_new(0, buflen);
-
     rb_io_check_closed(fptr);
 
     if (!MSG_DONTWAIT_RELIABLE)
@@ -233,7 +253,7 @@ rsock_s_recvfrom_nonblock(VALUE sock, in https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L253
 	}
 	rb_sys_fail("recvfrom(2)");
     }
-    if (slen < RSTRING_LEN(str)) {
+    if (slen != RSTRING_LEN(str)) {
 	rb_str_set_len(str, slen);
     }
     rb_obj_taint(str);
Index: ext/socket/basicsocket.c
===================================================================
--- ext/socket/basicsocket.c	(revision 50911)
+++ ext/socket/basicsocket.c	(revision 50912)
@@ -615,8 +615,7 @@ bsock_do_not_reverse_lookup_set(VALUE so https://github.com/ruby/ruby/blob/trunk/ext/socket/basicsocket.c#L615
 
 /*
  * call-seq:
- *   basicsocket.recv(maxlen) => mesg
- *   basicsocket.recv(maxlen, flags) => mesg
+ *   basicsocket.recv(maxlen[, flags[, outbuf]]) => mesg
  *
  * Receives a message.
  *
@@ -624,6 +623,9 @@ bsock_do_not_reverse_lookup_set(VALUE so https://github.com/ruby/ruby/blob/trunk/ext/socket/basicsocket.c#L623
  *
  * _flags_ should be a bitwise OR of Socket::MSG_* constants.
  *
+ * _outbuf_ will contain only the received data after the method call
+ * even if it is not empty at the beginning.
+ *
  *   UNIXSocket.pair {|s1, s2|
  *     s1.puts "Hello World"
  *     p s2.recv(4)                     #=> "Hell"
Index: ext/socket/unixsocket.c
===================================================================
--- ext/socket/unixsocket.c	(revision 50911)
+++ ext/socket/unixsocket.c	(revision 50912)
@@ -131,7 +131,7 @@ unix_path(VALUE sock) https://github.com/ruby/ruby/blob/trunk/ext/socket/unixsocket.c#L131
 
 /*
  * call-seq:
- *   unixsocket.recvfrom(maxlen [, flags]) => [mesg, unixaddress]
+ *   unixsocket.recvfrom(maxlen [, flags[, outbuf]) => [mesg, unixaddress]
  *
  * Receives a message via _unixsocket_.
  *
@@ -139,6 +139,9 @@ unix_path(VALUE sock) https://github.com/ruby/ruby/blob/trunk/ext/socket/unixsocket.c#L139
  *
  * _flags_ should be a bitwise OR of Socket::MSG_* constants.
  *
+ * _outbuf_ will contain only the received data after the method call
+ * even if it is not empty at the beginning.
+ *
  *   s1 = Socket.new(:UNIX, :DGRAM, 0)
  *   s1_ai = Addrinfo.unix("/tmp/sock1")
  *   s1.bind(s1_ai)
Index: test/socket/test_unix.rb
===================================================================
--- test/socket/test_unix.rb	(revision 50911)
+++ test/socket/test_unix.rb	(revision 50912)
@@ -385,6 +385,13 @@ class TestSocket_UNIXSocket < Test::Unit https://github.com/ruby/ruby/blob/trunk/test/socket/test_unix.rb#L385
     assert_equal("", s1.recv(10))
     assert_equal("", s1.recv(10))
     assert_raise(IO::EAGAINWaitReadable) { s1.recv_nonblock(10) }
+
+    buf = ""
+    s2.send("BBBBBB", 0)
+    sleep 0.1
+    rv = s1.recv(100, 0, buf)
+    assert_equal buf.object_id, rv.object_id
+    assert_equal "BBBBBB", rv
   ensure
     s1.close if s1
     s2.close if s2
Index: test/socket/test_nonblock.rb
===================================================================
--- test/socket/test_nonblock.rb	(revision 50911)
+++ test/socket/test_nonblock.rb	(revision 50912)
@@ -128,6 +128,16 @@ class TestSocketNonblock < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/test/socket/test_nonblock.rb#L128
     }
     mesg = u1.recv_nonblock(100)
     assert_equal("", mesg)
+
+    buf = "short"
+    out = "hello world" * 4
+    out.freeze
+    u2.send(out, 0, u1.getsockname)
+    IO.select [u1]
+    rv = u1.recv_nonblock(100, 0, buf)
+    assert_equal rv.object_id, buf.object_id
+    assert_equal out, rv
+    assert_equal out, buf
   ensure
     u1.close if u1
     u2.close if u2

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

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