On Thu, Dec 15, 2011 at 5:30 PM, Eric Wong <normalperson / yhbt.net> wrote:
> Chuck Remes <cremes.devlist / mac.com> wrote:
>> 6. In hot code when looking up a value in an array, it's a tad faster
>> to use #at instead of #[]. The reason is that #[] can take different
>> arguments other than a simple index (like a Range). Those extra
>> argument checks can add up.
>
>> These are all micro-optimizations and may not even be valid on your
>> runtime of choice.
>
> Exactly.
>
> Array#at shouldn't be faster than Array#[] on MRI 1.9+ with a single arg
> (ref: insns.def).

Indeed. Array#[] is slower on JRuby because String#[] uses backref
($~), and since we can't tell at compile time whether we're calling
Array#[] or String#[], we always prepare space for backref. There's no
#at method that uses backref, so we can elide the allocation of space
for backref.

I plan to reduce the cost of backref allocation, but it's not there
yet. In any case, JRuby's quite fast either way.

- Charlie