Socket#recv into a buffer

J

Joel VanderWerf

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.
 
R

Roger Pack

Joel said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,186
Latest member
vinaykumar_nevatia

Latest Threads

Top