Open Bug 865196 Opened 11 years ago Updated 4 months ago

Avoid return value object allocations when caller destructures

Categories

(Core :: JavaScript Engine, defect, P3)

x86
Linux
defect

Tracking

()

People

(Reporter: bruant.d, Unassigned)

References

(Blocks 1 open bug)

Details

The context is a discussion started on es-discuss [1]
The basic idea can be summed up in this example:

  function f(){
    return { a: 1, b: 2, c: 3 };
  }

  var {a, c} = f();

In this case, the intermediary object returned by f doesn't need to be allocated.
The dual of this is when destructuring is used in arguments:

  function g({b, d}){
    return b + d;
  }

  g({a: 1, b: 2, d: 4})

The gain here would be passing values directly instead of allocate+initialize+extract from object via property access+free (though since these objects are meant to be short-lived, once Generaional GC is here, there is not much to gain from the free'ing phase)

I couldn't find of a better summary wording. Feel free to rephrase as appropriate.

[1] https://mail.mozilla.org/pipermail/es-discuss/2013-April/029930.html
Possibly related bugs:
Bug 856533
Bug 661374
Kevin: no escape analysis here. This is easy in the case of an object or array literal return value.

/be
Summary: Less allocations when destructuring is used → Avoid return value object allocations when caller destructures
Ah, so there's no intent to support:

var result = {a: 1, b: 2, c: 3};
return result;

or

var result = {};
result.a = 1;
result.b = 2;
return result;

? That makes sense, I suppose. The inline return of a literal is probably the most common.
The former might well fall out of refactorings.  But as people aren't likely to write moderately obtuse code like that, its performance shouldn't matter for a first cut.

Note that recognizing the latter would require some fun to ensure those are property *additions* (non-conflicting ones, at that) and not property sets, even if this optimization would be to not perform either.  It could be done.  It might involve more work than recognizing only the simple literal case, or that instance plus that desugar to it via SSA/escape analysis, so returning the literal would be preferred as the general rule.
(In reply to Kevin Gadd (:kael) from comment #3)
> var result = {};
> result.a = 1;
> result.b = 2;
> return result;

which is similar to what is done by "new Fun()", and the type monitoring can even tell us the shape of it.
Also see bug 601662, which is specifically for arrays.
Assignee: general → nobody
Severity: normal → S3
Blocks: sm-js-perf
Priority: -- → P3
You need to log in before you can comment on or make changes to this bug.