Why does Array#first not use rb_ary_subseq?

S

Stefan Rusterholz

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 Rusterholz
 
S

Stefan Rusterholz

Yukihiro said:
Hi,

In message "Re: Why does Array#first not use rb_ary_subseq?"
on Fri, 18 May 2007 21:36:19 +0900, Stefan Rusterholz

|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
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 Rusterholz
 
S

Stefan Rusterholz

Yukihiro said:
Hi,

In message "Re: Why does Array#first not use rb_ary_subseq?"
on Fri, 18 May 2007 22:35:08 +0900, Yukihiro Matsumoto

|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
 

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

Forum statistics

Threads
474,252
Messages
2,571,267
Members
47,908
Latest member
MagdalenaR

Latest Threads

Top