diff options
author | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-06-15 20:02:43 +0000 |
---|---|---|
committer | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-06-15 20:02:43 +0000 |
commit | 619e8ae84f98516e3418d2a90d6e8b0f366e7bae (patch) | |
tree | f94e7f5a9209c75d7d09f4101930d34d95487d3f /test | |
parent | 835c349aecb6586667ada0ad7f37672a0c290f60 (diff) | |
download | ruby-619e8ae84f98516e3418d2a90d6e8b0f366e7bae.tar.gz |
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
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50912 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r-- | test/socket/test_nonblock.rb | 10 | ||||
-rw-r--r-- | test/socket/test_unix.rb | 7 |
2 files changed, 17 insertions, 0 deletions
diff --git a/test/socket/test_nonblock.rb b/test/socket/test_nonblock.rb index 4e14c9355a..93f463daf9 100644 --- a/test/socket/test_nonblock.rb +++ b/test/socket/test_nonblock.rb @@ -128,6 +128,16 @@ class TestSocketNonblock < Test::Unit::TestCase } 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 diff --git a/test/socket/test_unix.rb b/test/socket/test_unix.rb index a10f2e51eb..344dcc90ad 100644 --- a/test/socket/test_unix.rb +++ b/test/socket/test_unix.rb @@ -385,6 +385,13 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase 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 |