[#3006] CVS repository — "Eugene Scripnik" <hoaz@...>

Hello.

21 messages 2004/06/16
[#3008] Re: CVS repository — ts <decoux@...> 2004/06/16

>>>>> "E" == Eugene Scripnik <hoaz@gala.net> writes:

[#3009] Re: CVS repository — Michal Rokos <michal@...> 2004/06/16

Hi!

[#3057] Ruby 1.8.2 to be released. — matz@... (Yukihiro Matsumoto)

Hi,

20 messages 2004/06/23

entries / to_a

From: Johan Holmberg <holmberg@...>
Date: 2004-06-03 16:08:53 UTC
List: ruby-core #2962
Hi !

The Enumerable module defines the two methods "entries" and "to_a"
as synonyms for the same thing. Some classes that mix-in Enumerable
re-implement "to_a" but *not* "entries". Sometimes this is ok, but
in the case of the Array class I think this is a bad thing.
An example:

    require "benchmark"

    arr = [11, 22, 33] * 1_000_000

    Benchmark.bm(10) do |x|
      x.report("to_a")     { arr.to_a.length    }
      x.report("entries")  { arr.entries.length }
    end

This gives the following output on my Linux machine with an
up-to-date version of Ruby from CVS:

    $ ruby test-entries.rb
		    user     system      total        real
    to_a        0.000000   0.000000   0.000000 (  0.000047)
    entries     2.560000   0.020000   2.580000 (  2.587834)


The "entries" method return a newly created array even thou we
started with an array. We could have returned the original
array and saved some time (this is what "to_a" does).

I added the following patch to "array.c":

    --- array.c.orig        2004-06-03 14:26:18.000000000 +0200
    +++ array.c     2004-06-03 14:29:16.000000000 +0200
    @@ -3001,6 +3001,7 @@
         rb_define_method(rb_cArray, "to_s", rb_ary_to_s, 0);
         rb_define_method(rb_cArray, "inspect", rb_ary_inspect, 0);
         rb_define_method(rb_cArray, "to_a", rb_ary_to_a, 0);
    +    rb_define_alias(rb_cArray,  "entries", "to_a");
         rb_define_method(rb_cArray, "to_ary", rb_ary_to_ary_m, 0);
         rb_define_method(rb_cArray, "frozen?",  rb_ary_frozen_p, 0);


Then I re-ran the testsuites I know about. The new faster version
seemed to pass all tests so I assume that Ruby *doesn't require* that
we should get a copy.

Wouldn't it be a good idea to add this new behaviour to Ruby?
I see no point in doing the copying.

/Johan Holmberg


In This Thread

Prev Next