# Implementation of String or Array #slice and similar ops?

**URL:** <https://www.ruby-forum.com/t/implementation-of-string-or-array-slice-and-similar-ops/49304>\
**Category:** Ruby\
**Created:** [December 2, 2005, 11:30am UTC](https://www.ruby-forum.com/t/implementation-of-string-or-array-slice-and-similar-ops/49304 "2005-12-02T11:30:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronjeffries](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@ronjeffries](https://www.ruby-forum.com/u/ronjeffries)\
**Post date:** [December 2, 2005, 11:30am UTC](https://www.ruby-forum.com/t/implementation-of-string-or-array-slice-and-similar-ops/49304/1 "2005-12-02T11:30:41Z")

</div>

As some may recall, I’m fiddling with some string manipulation in a  
recent  
series of articles. I’m wondering about the specific implementation of  
operations like slice, on String (and on Array).

Specifically, does slicing a String copy the bytes, or point into the  
old  
string, and similarly for Array? I see that if I modify the slice, the  
original  
is not modified, and vice versa, but have the bytes already been copied  
when the  
slice is created? Or not until later?

Similar questions for Array, all around, and for operations like \<\<.

Bottom line, are all the methods of String and Array directly allocating  
storage, or are any of them “virtual”?

My guess is that the bytes are copied. If so, is anyone aware of a class  
or  
classes that address substrings and such as virtual copies?

Thanks,

---

<div class="post-metadata">

**Author:** ![ronjeffries](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@ronjeffries](https://www.ruby-forum.com/u/ronjeffries)\
**Post date:** [December 2, 2005, 12:35pm UTC](https://www.ruby-forum.com/t/implementation-of-string-or-array-slice-and-similar-ops/49304/2 "2005-12-02T12:35:48Z")

</div>

On Fri, Dec 02, 2005 at 07:27:32PM +0900, Ron J. wrote:  
[…]

> My guess is that the bytes are copied. If so, is anyone aware of a class or  
> classes that address substrings and such as virtual copies?

Ruby does use COW for arrays and strings, so internal pointers are  
reused and the data they point to duplicated when actually needed.

somearray[start, size] will always reuse the internal array.  
If I’m reading rb\_str\_substr correctly, somestring[start, rsize] will  
copy the bytes unless start’+rsize == somestring.size (start’ being  
the adjusted offset), amongst other conditions.

---

<div class="post-metadata">

**Author:** ![ronjeffries](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@ronjeffries](https://www.ruby-forum.com/u/ronjeffries)\
**Post date:** [December 2, 2005, 3:31pm UTC](https://www.ruby-forum.com/t/implementation-of-string-or-array-slice-and-similar-ops/49304/3 "2005-12-02T15:31:08Z")

</div>

On 12/2/05, Mauricio Fernández [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Fri, Dec 02, 2005 at 07:27:32PM +0900, Ron J. wrote:  
> Ruby does use COW for arrays and strings, so internal pointers are  
> reused and the data they point to duplicated when actually needed.
> 
> somearray[start, size] will always reuse the internal array.  
> If I’m reading rb\_str\_substr correctly, somestring[start, rsize] will  
> copy the bytes unless start’+rsize == somestring.size (start’ being  
> the adjusted offset), amongst other conditions.

What does “COW” mean?

A serious problem occurs when you go modify the original (likely  
large) array after taking a slice. It allocates a new area for the  
original array and leaves the slice (probably small) pointing the the  
original area with the entire array. This can cause serious  
performance issues (memory and run-time). Here is a simple example  
that shows the problem:

time ruby -e ‘n=2\*\*13; a=(1…n).to\_a; n.times { |i| a.push(a[i,1]) };  
IO.readlines(“/proc/#$$/status”).grep(/VmSize/).display’  
VmSize: 593020 kB  
50.44user 0.65system 1:00.24elapsed 84%CPU (0avgtext+0avgdata  
0maxresident)k  
0inputs+0outputs (1957major+121801minor)pagefaults 0swaps

You’ll find that both memory and runtime are O(n\*\*2). vs. the  
equivalent without element sharing (O(n)):

time ruby -e ‘n=2\*\*13; a=(1…n).to\_a; n.times { |i| a.push([a[i]]) };  
IO.readlines(“/proc/#$$/status”).grep(/VmSize/).display’  
VmSize: 3384 kB  
0.01user 0.00system 0:00.22elapsed 7%CPU (0avgtext+0avgdata  
0maxresident)k  
0inputs+0outputs (1major+558minor)pagefaults 0swaps

Back in September, I provided a patch to 1.9 for arrays. It addressed  
this issue plus made operations near the beginning of the array (i.e.  
shift/unshift) just as fast as operations near the end (i.e.  
pop/push). Here was my post:

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/5861](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/5861)

This patch hasn’t been accepted to the ruby head (yet). The guys  
doing the Sydney fork of ruby (1.8 derivative) do plan on integrating  
it. I’m not sure where they are on it.

I haven’t had time to do the same for strings.

---

<div class="post-metadata">

**Author:** ![ronjeffries](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@ronjeffries](https://www.ruby-forum.com/u/ronjeffries)\
**Post date:** [December 2, 2005, 3:51pm UTC](https://www.ruby-forum.com/t/implementation-of-string-or-array-slice-and-similar-ops/49304/4 "2005-12-02T15:51:26Z")

</div>

Eric M. wrote:

> What does “COW” mean?

Copy On Write.

---

<div class="post-metadata">

**Author:** ![ronjeffries](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@ronjeffries](https://www.ruby-forum.com/u/ronjeffries)\
**Post date:** [December 2, 2005, 4:52pm UTC](https://www.ruby-forum.com/t/implementation-of-string-or-array-slice-and-similar-ops/49304/5 "2005-12-02T16:52:30Z")

</div>

On Dec 2, 2005, at 9:28 AM, Eric M. wrote:

> What does “COW” mean?

copy on write

> **[Copy-on-write](https://en.wikipedia.org/wiki/Copy-on-write)**
>
> Copy-on-write (COW), sometimes referred to as implicit sharing or shadowing, is a resource-management technique used in computer programming to efficiently implement a "duplicate" or "copy" operation on modifiable resources. If a resource is duplicated but not modified, it is not necessary to create a new resource; the resource can be shared between the copy and the original. Modifications must still create a copy, hence the technique: the copy operation is deferred until the first write Copy-on-...

Gary W.

---

<div class="post-metadata">

**Author:** ![ronjeffries](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@ronjeffries](https://www.ruby-forum.com/u/ronjeffries)\
**Post date:** [December 4, 2005, 2:51am UTC](https://www.ruby-forum.com/t/implementation-of-string-or-array-slice-and-similar-ops/49304/6 "2005-12-04T02:51:03Z")

</div>

Thanks to all for the info on this. It sounds like with strings I won’t  
get any  
advantage from COW at all on slicing, and that even with arrays I’ll not  
be much  
better off.

For my current purposes, I don’t see how to use arrays anyway, since I’m  
interested primarily in large chunks of contiguous memory.

Any pointers relating to what I’m doing in my Extended Set Theory  
articles will  
be gratefully received. A ping via email will help, as I don’t get back  
here as  
frequently as I probably should.

Thanks,

---

<div class="post-metadata">

**Author:** ![ronjeffries](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@ronjeffries](https://www.ruby-forum.com/u/ronjeffries)\
**Post date:** [December 2, 2005, 4:56pm UTC](https://www.ruby-forum.com/t/implementation-of-string-or-array-slice-and-similar-ops/49304/7 "2005-12-02T16:56:32Z")

</div>

On 12/2/05, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Dec 2, 2005, at 9:28 AM, Eric M. wrote:
> 
> > What does “COW” mean?
> 
> copy on write
> 
> [Copy-on-write - Wikipedia](http://en.wikipedia.org/wiki/Copy-on-write)

Thanks. It looks like the normal COW usage from this wiki is when the  
shared resource starts and ends at the same memory locations for all  
uses. I think the conventional COW as described above would apply to  
Array#replace and Array#clone, but not Array#slice. The way ruby does  
COW is clearly a win for Array#replace and Array#clone, but not for  
Array#slice (and many others) as demonstrated in my example.

---

<div class="post-metadata">

**Author:** ![ronjeffries](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@ronjeffries](https://www.ruby-forum.com/u/ronjeffries)\
**Post date:** [December 5, 2005, 2:45am UTC](https://www.ruby-forum.com/t/implementation-of-string-or-array-slice-and-similar-ops/49304/8 "2005-12-05T02:45:58Z")

</div>

On Sat, 3 Dec 2005 19:12:20 -0700, removed\_email\_address@domain.invalid wrote:

> can you use narray? it stores the elements as continuous memory. i’ve got a  
> patch that allows it’s use with mmap to - so a file can be mapped, manipulated  
> as an narray, and the bytes are persisted to file automagically.

Well, I wouldn’t rule it out. Seems like it might have a lot of stuff  
that I  
wouldn’t need, being numerically oriented. Its base functionality might  
be  
interesting.

Are there docs for it anywhere handy? I didn’t find any with a quick  
goog.

Thanks,

---

<div class="post-metadata">

**Author:** ![ronjeffries](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@ronjeffries](https://www.ruby-forum.com/u/ronjeffries)\
**Post date:** [December 4, 2005, 3:23am UTC](https://www.ruby-forum.com/t/implementation-of-string-or-array-slice-and-similar-ops/49304/9 "2005-12-04T03:23:29Z")

</div>

On Sun, 4 Dec 2005, Ron J. wrote:

> Thanks to all for the info on this. It sounds like with strings I won’t get  
> any advantage from COW at all on slicing, and that even with arrays I’ll not  
> be much better off.
> 
> For my current purposes, I don’t see how to use arrays anyway, since I’m  
> interested primarily in large chunks of contiguous memory.

can you use narray? it stores the elements as continuous memory. i’ve  
got a  
patch that allows it’s use with mmap to - so a file can be mapped,  
manipulated  
as an narray, and the bytes are persisted to file automagically.

-a
