Project

General

Profile

Actions

Feature #6668

closed

Multiple assignment should not return an Array object

Added by headius (Charles Nutter) over 12 years ago. Updated about 12 years ago.

Status:
Rejected
Target version:
-
[ruby-core:45958]

Description

Currently, when doing multiple assignment, the entire expression must return the right-hand side as an array.

system ~ $ ruby -e "ret = (a, b, c = 1, 2, 3); p ret"
[1, 2, 3]

This is an artifact of MRI's implementation, since multiple assignment was traditionally implemented by taking the array node on the right-hand side, standing it up as a full Ruby Array, and then peeling elements off for assignment on the left-hand side. It is also a performance issue, since it requires constructing the RHS array even when it is never used (unless you are able to do various compiler tricks). I propose removing it.

Justification:

  • The feature is rarely used; most people don't even know it exists.
  • The impact of creating the RHS array is significant; JRuby can optimize it away in cases where the line is not used as an expression, and the performance difference is huge: https://gist.github.com/3019255
  • It is counter-intuitive to have an automatic performance hit just from grouping assignments. "a,b = 1,2" should have the exact same performance as "a = 1; b = 2"

Note that while JRuby can eliminate the array creation in non-expression cases, those are somewhat rare since many times masgn is used at the end of a method body, as for initializers:

class Foo
def initialize(a, b, c)
@A (A A), @b, @c = a, b, c
end
end

JRuby and other implementations may get smart enough in our optimizers to eliminate the array in all cases where it's not needed, but this is a very large burden on the optimization subsystem. It may also not be possible to do in all cases (or not possible to do in even a majority of cases).

Multiple assignment should not return RHS as an array. I do not care what it returns.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0