optimizing the output of compiled splat arguments.

This commit is contained in:
Jeremy Ashkenas
2010-07-24 12:40:36 -07:00
parent dc1288d319
commit 6b6beb588c
3 changed files with 27 additions and 21 deletions

View File

@@ -222,8 +222,7 @@
};
Lexer.prototype.balancedToken = function() {
var delimited;
var _d = arguments.length, _e = _d >= 1;
delimited = __slice.call(arguments, 0, _d - 0);
delimited = __slice.call(arguments, 0);
return this.balancedString(this.chunk, delimited);
};
Lexer.prototype.lineToken = function() {

View File

@@ -1031,20 +1031,24 @@
return (typeof (_b = this.index) !== "undefined" && _b !== null) ? this.compileParam(o) : this.name.compile(o);
};
SplatNode.prototype.compileParam = function(o) {
var _b, _c, idx, len, name, pos, trailing, variadic;
var _b, _c, end, idx, len, name, pos, trailing, variadic;
name = this.name.compile(o);
o.scope.find(name);
len = o.scope.freeVariable();
o.scope.assign(len, "arguments.length");
variadic = o.scope.freeVariable();
o.scope.assign(variadic, ("" + len + " >= " + this.arglength));
_b = this.trailings;
for (idx = 0, _c = _b.length; idx < _c; idx++) {
trailing = _b[idx];
pos = this.trailings.length - idx;
o.scope.assign(trailing.compile(o), ("arguments[" + variadic + " ? " + len + " - " + pos + " : " + (this.index + idx) + "]"));
end = '';
if (this.trailings.length) {
len = o.scope.freeVariable();
o.scope.assign(len, "arguments.length");
variadic = o.scope.freeVariable();
o.scope.assign(variadic, ("" + len + " >= " + this.arglength));
end = this.trailings.length ? (", " + len + " - " + (this.trailings.length)) : null;
_b = this.trailings;
for (idx = 0, _c = _b.length; idx < _c; idx++) {
trailing = _b[idx];
pos = this.trailings.length - idx;
o.scope.assign(trailing.compile(o), ("arguments[" + variadic + " ? " + len + " - " + pos + " : " + (this.index + idx) + "]"));
}
}
return "" + name + " = " + (utility('slice')) + ".call(arguments, " + this.index + ", " + len + " - " + (this.trailings.length) + ")";
return "" + name + " = " + (utility('slice')) + ".call(arguments, " + this.index + end + ")";
};
SplatNode.prototype.compileValue = function(o, name, index, trailings) {
var trail;

View File

@@ -922,14 +922,17 @@ exports.SplatNode: class SplatNode extends BaseNode
compileParam: (o) ->
name: @name.compile(o)
o.scope.find name
len: o.scope.freeVariable()
o.scope.assign len, "arguments.length"
variadic: o.scope.freeVariable()
o.scope.assign variadic, "$len >= $@arglength"
for trailing, idx in @trailings
pos: @trailings.length - idx
o.scope.assign(trailing.compile(o), "arguments[$variadic ? $len - $pos : ${@index + idx}]")
"$name = ${utility('slice')}.call(arguments, $@index, $len - ${@trailings.length})"
end: ''
if @trailings.length
len: o.scope.freeVariable()
o.scope.assign len, "arguments.length"
variadic: o.scope.freeVariable()
o.scope.assign variadic, "$len >= $@arglength"
end: if @trailings.length then ", $len - ${@trailings.length}"
for trailing, idx in @trailings
pos: @trailings.length - idx
o.scope.assign(trailing.compile(o), "arguments[$variadic ? $len - $pos : ${@index + idx}]")
"$name = ${utility('slice')}.call(arguments, $@index$end)"
# A compiling a splat as a destructuring assignment means slicing arguments
# from the right-hand-side's corresponding array.