Why does Array#first not use rb_ary_subseq?

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.

Hi,

In message “Re: Why does Array#first not use rb_ary_subseq?”
on Fri, 18 May 2007 21:36:19 +0900, Stefan R.
[email protected] 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.

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.
[email protected] 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 :frowning:

I posted a benchmark with results and code on
Parked at Loopia. 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.

Hi,

In message “Re: Why does Array#first not use rb_ary_subseq?”
on Fri, 18 May 2007 22:22:31 +0900, Stefan R.
[email protected] 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.

Hi,

In message “Re: Why does Array#first not use rb_ary_subseq?”
on Fri, 18 May 2007 22:35:08 +0900, Yukihiro M.
[email protected] 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.

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.
[email protected] 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 :slight_smile:

Regards
Stefan