# Why does Array#first not use rb\_ary\_subseq?

**URL:** <https://www.ruby-forum.com/t/why-does-array-first-not-use-rb-ary-subseq/104476>\
**Category:** Ruby\
**Created:** [May 18, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/why-does-array-first-not-use-rb-ary-subseq/104476 "2007-05-18T14:36:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![apeiros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@apeiros](https://www.ruby-forum.com/u/apeiros)\
**Post date:** [May 18, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/why-does-array-first-not-use-rb-ary-subseq/104476/1 "2007-05-18T14:36:17Z")

</div>

Hi there

When I run a small bench I noticed that Array#first(n) with an argument  
is vastly slower than Array#[0,n].  
Poking a bit in the source revealed that Array#[] uses rb\_ary\_subseq,  
which seems to create a COW Array while Array#first creates a new Array  
on its own and fills it with values. Is there any rationale as to why  
Array#first doesn’t just use rb\_ary\_subseq too?  
I searched the ML for an answer to this and found none. Forgive me  
please if I overlooked the answer.

Regards  
Stefan R.

---

<div class="post-metadata">

**Author:** ![apeiros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@apeiros](https://www.ruby-forum.com/u/apeiros)\
**Post date:** [May 18, 2007, 2:47pm UTC](https://www.ruby-forum.com/t/why-does-array-first-not-use-rb-ary-subseq/104476/2 "2007-05-18T14:47:16Z")

</div>

Hi,

In message “Re: Why does Array#first not use rb\_ary\_subseq?”  
on Fri, 18 May 2007 21:36:19 +0900, Stefan R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|When I run a small bench I noticed that Array#first(n) with an argument  
|is vastly slower than Array#[0,n].

The difference is due to Array#first is optimized for memory  
consumption, I guess. We’d like profile on the use-case, so could you  
show us “vastly slower” example code?

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![apeiros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@apeiros](https://www.ruby-forum.com/u/apeiros)\
**Post date:** [May 18, 2007, 3:22pm UTC](https://www.ruby-forum.com/t/why-does-array-first-not-use-rb-ary-subseq/104476/3 "2007-05-18T15:22:30Z")

</div>

Yukihiro M. wrote:

> Hi,
> 
> In message “Re: Why does Array#first not use rb\_ary\_subseq?”  
> on Fri, 18 May 2007 21:36:19 +0900, Stefan R.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> |When I run a small bench I noticed that Array#first(n) with an argument  
> |is vastly slower than Array#[0,n].
> 
> The difference is due to Array#first is optimized for memory  
> consumption, I guess. We’d like profile on the use-case, so could you  
> show us “vastly slower” example code?
> 
> ```
> matz.
> 
> ```

Thank you for your reply.

Memory consumption: could you please elaborate that a bit for me?  
I somehow expected the COW Array created by Array#[] to be more memory  
efficient as I understood that to be the reason why that is used at all.  
I’m new to C, so please forgive me if I’m a bit slow ☹

I posted a benchmark with results and code on  
[Parked at Loopia](http://pastie.caboo.se/62638). I don’t have a specific use-case but I’d  
say a lot of array operations base on .each (all Enumerable methods)  
which is why I added a bench running an each loop over the elements.  
It’s intriguing that the difference only seems to be huge for the  
creation. In my initial bench I only run the [] and first method, I  
didn’t add an each-loop. I didn’t expect this as the returned array is  
not modified. I think I have to delve a bit more into the whys and whats  
with that COW array.

But what I forgot to ask in my initial question: if those two  
implementations have different characteristics, why isn’t that  
documented? (from the docs I’d just expect first to have nicer semantics  
for its use-case and nothing else)

regards  
Stefan R.

---

<div class="post-metadata">

**Author:** ![apeiros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@apeiros](https://www.ruby-forum.com/u/apeiros)\
**Post date:** [May 18, 2007, 3:36pm UTC](https://www.ruby-forum.com/t/why-does-array-first-not-use-rb-ary-subseq/104476/4 "2007-05-18T15:36:25Z")

</div>

Hi,

In message “Re: Why does Array#first not use rb\_ary\_subseq?”  
on Fri, 18 May 2007 22:22:31 +0900, Stefan R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|But what I forgot to ask in my initial question: if those two  
|implementations have different characteristics, why isn’t that  
|documented? (from the docs I’d just expect first to have nicer semantics  
|for its use-case and nothing else)

Just because it is just an implementation matter. Anyway, I will  
examine your code example, and check what we can do.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![apeiros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@apeiros](https://www.ruby-forum.com/u/apeiros)\
**Post date:** [May 18, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/why-does-array-first-not-use-rb-ary-subseq/104476/5 "2007-05-18T16:11:29Z")

</div>

Hi,

In message “Re: Why does Array#first not use rb\_ary\_subseq?”  
on Fri, 18 May 2007 22:35:08 +0900, Yukihiro M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|Just because it is just an implementation matter. Anyway, I will  
|examine your code example, and check what we can do.

I am sorry, I was bit confused with 1.9 (trunk) and 1.8 which you are  
talking about. You are right. Array#first should utilize  
copy-on-write as much as it can. I will fix.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![apeiros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@apeiros](https://www.ruby-forum.com/u/apeiros)\
**Post date:** [May 18, 2007, 4:22pm UTC](https://www.ruby-forum.com/t/why-does-array-first-not-use-rb-ary-subseq/104476/6 "2007-05-18T16:22:09Z")

</div>

Yukihiro M. wrote:

> Hi,
> 
> In message “Re: Why does Array#first not use rb\_ary\_subseq?”  
> on Fri, 18 May 2007 22:35:08 +0900, Yukihiro M.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> |Just because it is just an implementation matter. Anyway, I will  
> |examine your code example, and check what we can do.
> 
> I am sorry, I was bit confused with 1.9 (trunk) and 1.8 which you are  
> talking about. You are right. Array#first should utilize  
> copy-on-write as much as it can. I will fix.
> 
> ```
> matz.
> 
> ```

Oh, I should have supplied the version. Sorry about that.  
Heh, actually I’m surprised that my curiosity results in a change in  
code, but happy to be of help 🙂

Regards  
Stefan
