Hi,

On Wed, Jun 22, 2011 at 6:34 PM, Charles Nutter <headius / headius.com> wrote=
:
>
> Perhaps if an optimization is needed, it could just be adding a nil check=
 to Kernel#Array.

Yes, that's a possibility I thought of.

The problem lies both in the "if" (note that the included benchmark
shows a ~35% performance gain) and the fact that the definition of
Array() would have to change slightly.

I also fail to see why Array(nil) should be optimized and not, say, Array(4=
2)

I'm feel there are many more important things to worry about.

> ----------------------------------------
> Feature #4917: NilClass#to_ary
> http://redmine.ruby-lang.org/issues/4917
>
> Author: Jay Feldblum
> Status: Rejected
> Priority: Normal
> Assignee:
> Category:
> Target version:
>
>
> As a performance improvement in certain cases, nil should respond to to_a=
ry.
>
> Kernel#Array, when passed nil, first tries to send to_ary to nil (which a=
ctually ends up calling method_missing on nil) and then tries to send to_a =
to nil which finally succeeds. When Kernel#Array is used often, for example=
 in library/gem code, this can have a noticeable, if relatively small, nega=
tive impact on the overall application performance.
>
> =A0 =A0$ irb
>
> =A0 =A0> RUBY_VERSION
> =A0 =A0 =3D> "1.9.2"
>
> =A0 =A0> require 'benchmark'
> =A0 =A0> def bench(times) Benchmark.bmbm{|x| x.report{times.times(&Proc.n=
ew)}} end
>
> =A0 =A0# Let's zero the scale....
> =A0 =A0> bench(10_000_000) { }
>
> =A0 =A0# The "before" benchmark....
> =A0 =A0> bench(10_000_000) { Array(nil) }
>
> =A0 =A0# An optimization....
> =A0 =A0> NilClass.class_eval { alias to_ary to_a }
>
> =A0 =A0# The "after" benchmark....
> =A0 =A0> bench(10_000_000) { Array(nil) }
> =A0 =A0# Much faster!
>
> =A0 =A0# Let's see how many times method_missing actually gets called....
> =A0 =A0> NilClass.class_eval { undef to_ary }
> =A0 =A0> class NilClass
> =A0 =A0> =A0 alias method_missing_without_hit method_missing
> =A0 =A0> =A0 def method_missing(name, *args, &block)
> =A0 =A0> =A0 =A0 $method_missing_hits +=3D 1
> =A0 =A0> =A0 =A0 method_missing_without_hit(name, *args, &block)
> =A0 =A0> =A0 end
> =A0 =A0> end
>
> =A0 =A0> $method_missing_hits =3D 0
> =A0 =A0> bench(100_000) { Array(nil) }
> =A0 =A0# Very slow!
> =A0 =A0> $method_missing_hits
> =A0 =A0 =3D> 200005
>
> =A0 =A0> NilClass.class_eval { alias to_ary to_a }
> =A0 =A0> $method_missing_hits =3D 0
> =A0 =A0> bench(100_000) { Array(nil) }
> =A0 =A0# Instantaneous!
> =A0 =A0> $method_missing_hits
> =A0 =A0 =3D> 0
>
>
>
> --
> http://redmine.ruby-lang.org
>
>