Project

General

Profile

Actions

Feature #11242

closed

[PATCH] socket: alloa explicit buffer for recv and recv_nonblock

Added by normalperson (Eric Wong) almost 9 years ago. Updated almost 9 years ago.

Status:
Closed
Target version:
-
[ruby-core:69511]

Description

Note, this relies on [Feature #11229]

This reduces GC overhead and makes the API more consistent
with IO#read and IO#read_nonblock.

Benchmark results:

    |     user  |   system  |    total |       real

--------|----------:|----------:|---------:|-----------:
alloc | 0.130000 | 0.280000 | 0.410000 |( 0.420656)
extbuf | 0.100000 | 0.220000 | 0.320000 |( 0.318708)

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

Files

Updated by nobu (Nobuyoshi Nakada) almost 9 years ago

  • Description updated (diff)

Updated by matz (Yukihiro Matsumoto) almost 9 years ago

Looks good to me. Go ahead.

Matz.

Actions #3

Updated by Anonymous almost 9 years ago

  • Status changed from Open to Closed

Applied in changeset r50912.


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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0