IO::Socket seems to do getpeername far too often on a socket and can be "optomized".

A bit of background, I ran some simple IO::Socket code in an NIS/NFS env and noticed that ypserv was leaking memory. While the ypserv env was getting fixed, an strace of the code showed that getpeername was being called over 30k times in a 2 minute window. In an NIS env, getpeername does a yp call. The leak in ypserv was exposed thanks to the 30k yp calls.

While waiting for the IT team to fix ypserv, I took a look at IO::Socket to figure out why it is calling getpeername so often.

On every send, it can call getpeername twice. Once if the peer wasn't passed in as an arguement, and once to see which mode to call the Socket's send. Of course, the getpeername response is cached in the socket if the send was successful.

My thinking is that both peername and send should get "optomized" to use the cached data in the socket object.

The peername sub uses its cache if getpeername fails, but shouldn't it use the cache first, and only look up the name if the cache doesn't exist?

sub peername { @_ == 1 or croak 'usage: $sock->peername()'; my($sock) = @_; getpeername($sock) || ${*$sock}{'io_socket_peername'} || undef; }
The send sub should be modified to use the peername function, which should have the caching mechanism.
sub send { @_ >= 2 && @_ <= 4 or croak 'usage: $sock->send(BUF, [FLAGS, [TO]] +)'; my $sock = $_[0]; my $flags = $_[2] || 0; my $peer = $_[3] || $sock->peername; croak 'send: Cannot determine peer address' unless($peer); my $r = defined(getpeername($sock)) ? send($sock, $_[1], $flags) : send($sock, $_[1], $flags, $peer); # remember who we send to, if it was sucessful ${*$sock}{'io_socket_peername'} = $peer if(@_ == 4 && defined $r); $r; }
While changing the code to use the getpeername cache works for me, before I submit a patch, I wanted to get a few more eyes on the idea, hense this post.

In reply to IO::Socket doing getpeername twice on every send? by cazz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.