# IO#write\_nonblock(string, offset) useful?

**URL:** <https://www.ruby-forum.com/t/io-write-nonblock-string-offset-useful/147526>\
**Category:** Ruby\
**Created:** [September 2, 2008, 8:51pm UTC](https://www.ruby-forum.com/t/io-write-nonblock-string-offset-useful/147526 "2008-09-02T20:51:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [September 2, 2008, 8:51pm UTC](https://www.ruby-forum.com/t/io-write-nonblock-string-offset-useful/147526/1 "2008-09-02T20:51:34Z")

</div>

Was asked to repost this under a separate thread, so here it is!

Currently we have  
IO#write\_nonblock(string) [writes as much of the string as currently  
possible]

What I was thinking would be useful would be  
IO#write\_nonblock(string, offset) [starts the write at offset bytes into  
the string].

Would anybody else find this useful?  
Thanks!  
-=R

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [September 2, 2008, 9:12pm UTC](https://www.ruby-forum.com/t/io-write-nonblock-string-offset-useful/147526/2 "2008-09-02T21:12:52Z")

</div>

Roger P. wrote:

> Would anybody else find this useful?  
> Thanks!

With COW strings, is this really needed?

s = (0…9).to\_a.to\_s \* 1000000  
sa = (1…10000).map {|i| s[i…-1]}  
puts `cat /proc/#{$$}/status`[/VmRSS:\s\*(.\*)/,1] # 13292 kB

Is there a reason other than saving memory and avoiding a #slice call?

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [September 2, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/io-write-nonblock-string-offset-useful/147526/3 "2008-09-02T22:12:54Z")

</div>

Roger P. wrote:

> > puts `cat /proc/#{$$}/status`[/VmRSS:\s\*(.\*)/,1] # 13292 kB  
> > Is there a reason other than saving memory and avoiding a #slice call?
> 
> Those would be the reasons I’d consider it useful–to save on CPU and  
> RAM [since it would avoid having to create all those extra objects]. I  
> think that’s what you were pointing out?

It’s just that those extra objects are fairly small, due to  
copy-on-write… just the 20 byte ruby RString object for each slice  
rather than a copy of the whole 1Mb string you are trying to send. They  
share the 1Mb string data.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [September 2, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/io-write-nonblock-string-offset-useful/147526/4 "2008-09-02T22:05:54Z")

</div>

> puts `cat /proc/#{$$}/status`[/VmRSS:\s\*(.\*)/,1] # 13292 kB  
> Is there a reason other than saving memory and avoiding a #slice call?

Those would be the reasons I’d consider it useful–to save on CPU and  
RAM [since it would avoid having to create all those extra objects]. I  
think that’s what you were pointing out?

Thanks!  
-=R

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [September 3, 2008, 2:37pm UTC](https://www.ruby-forum.com/t/io-write-nonblock-string-offset-useful/147526/5 "2008-09-03T14:37:35Z")

</div>

Rob B. wrote:

Rob, are you aware that your message body is empty, at least on  
ruby-talk. Are you posting from the forum or newsgroup?

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [September 3, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/io-write-nonblock-string-offset-useful/147526/6 "2008-09-03T15:05:09Z")

</div>

On Tue, Sep 2, 2008 at 2:29 PM, Joel VanderWerf  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rob B. wrote:
> 
> Rob, are you aware that your message body is empty, at least on ruby-talk.  
> Are you posting from the forum or newsgroup?

Hi Rob,

i’ve noticed loss of the message with some of you other posts, too,  
the most recent group starting from the Chris P. tutorial thread.

Todd

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [September 3, 2008, 12:35am UTC](https://www.ruby-forum.com/t/io-write-nonblock-string-offset-useful/147526/7 "2008-09-03T00:35:30Z")

</div>

> It’s just that those extra objects are fairly small, due to  
> copy-on-write… just the 20 byte ruby RString object for each slice  
> rather than a copy of the whole 1Mb string you are trying to send. They  
> share the 1Mb string data.

Fascinating. Thanks for pointing that out. I guess that being the case  
then it’d be only a small improvement–10,000 slices on my machine takes  
0.02s and calls the GC twice, so…there’s some room for improvement,  
but it’s probably not a bottleneck.]  
Thanks!  
-=R

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [September 3, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/io-write-nonblock-string-offset-useful/147526/8 "2008-09-03T15:05:45Z")

</div>

On Sep 2, 2008, at 3:29 PM, Joel VanderWerf wrote:

> Rob B. wrote:
> 
> Rob, are you aware that your message body is empty, at least on ruby-  
> talk. Are you posting from the forum or newsgroup?
> 
> –  
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

From email (therefore the newsgroup). This happens when I forget to  
uncheck the “digitally signed” option. What I meant to say was:

I presume that the IO#write\_nonblock returns the number of bytes  
written (which the write(2) man page says, but is only implied by the  
rdoc).

Assuming this is the case (and the method will become  
IO#write\_nonblock(string, offset=0)), I’d think that this was a good  
idea. (Even though I’ve never had to use it.)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
