[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Socket#recv into a buffer

Joel VanderWerf

5/16/2008 10:49:00 PM


Is there any reason why Socket#recv can't take an optional buffer
argument the way IO#read does?

Otherwise, Socket#recv is always allocating strings, hence burdening GC.

I saw about a 20% improvement by using #read instead of #recv in some
socket code, but of course the unbuffered vs. buffered difference comes
into play there too.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

1 Answer

Roger Pack

5/18/2008 4:19:00 AM

0

Joel VanderWerf wrote:
> Is there any reason why Socket#recv can't take an optional buffer
> argument the way IO#read does?
>
> Otherwise, Socket#recv is always allocating strings, hence burdening GC.
>
> I saw about a 20% improvement by using #read instead of #recv in some
> socket code, but of course the unbuffered vs. buffered difference comes
> into play there too.

Sounds like it would make a good patch. I think the reason it's not is
that [afaik] ruby typically doesn't have default lengths for
strings...i.e. if recv returns less than the desired number of bytes,
ruby 'tends' to return a string of exactly that length [and that much
memory allocated] so...in our hypothetical example above, it
should...leave it still large? [i.e. it would require string itself to
change].
That being said, you CAN have a string with extra nulls in it
"abc\0def" and that's a valid string, so maybe recv_into_string could
just write out something like that...
thoughts?
-R
--
Posted via http://www.ruby-....