1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

optimizing comprehensions source references

This commit is contained in:
Jeremy Ashkenas 2010-10-06 21:13:11 -04:00
parent 5605ba32e3
commit d4dac214ab
11 changed files with 70 additions and 86 deletions

View file

@ -2,6 +2,6 @@
lunch = eat food for food in ['toast', 'cheese', 'wine']
# Naive collision detection.
for roid in asteroids
for roid, index in asteroids
for roid2 in asteroids when roid isnt roid2
roid.explode() if roid.overlaps roid2

View file

@ -1,4 +1,4 @@
var _i, _j, _len, _len2, _ref, _ref2, _result, food, lunch, roid, roid2;
var _i, _j, _len, _len2, _len3, _ref, _result, food, index, lunch, roid, roid2;
lunch = (function() {
_result = []; _ref = ['toast', 'cheese', 'wine'];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@ -7,12 +7,10 @@ lunch = (function() {
}
return _result;
})();
_ref = asteroids;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
roid = _ref[_i];
_ref2 = asteroids;
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
roid2 = _ref2[_j];
for (index = 0, _len2 = asteroids.length; index < _len2; index++) {
roid = asteroids[index];
for (_j = 0, _len3 = asteroids.length; _j < _len3; _j++) {
roid2 = asteroids[_j];
if (roid !== roid2) {
if (roid.overlaps(roid2)) {
roid.explode();

View file

@ -1,12 +1,12 @@
var Animal, Horse, Snake, sam, tom;
var __extends = function(child, parent) {
var ctor = function(){};
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.prototype.constructor = child;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
};
var ctor = function() {};
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.prototype.constructor = child;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
};
Animal = (function() {
return function Animal(_arg) {
this.name = _arg;

View file

@ -1,9 +1,9 @@
var _ref, _result, globals, name;
var _result, globals, name;
var __hasProp = Object.prototype.hasOwnProperty;
globals = (function() {
_result = []; _ref = window;
for (name in _ref) {
if (!__hasProp.call(_ref, name)) continue;
_result = [];
for (name in window) {
if (!__hasProp.call(window, name)) continue;
_result.push(name);
}
return _result;

View file

@ -1,7 +1,7 @@
var Account;
var __bind = function(func, context) {
return function(){ return func.apply(context, arguments); };
};
return function() { return func.apply(context, arguments); };
};
Account = function(customer, cart) {
this.customer = customer;
this.cart = cart;

View file

@ -829,10 +829,10 @@ lyrics = (function() {
lunch <span class="Keyword">=</span> eat food <span class="Keyword">for</span> food <span class="Keyword">in</span> [<span class="String"><span class="String">'</span>toast<span class="String">'</span></span>, <span class="String"><span class="String">'</span>cheese<span class="String">'</span></span>, <span class="String"><span class="String">'</span>wine<span class="String">'</span></span>]
<span class="Comment"><span class="Comment">#</span> Naive collision detection.</span>
<span class="Keyword">for</span> roid <span class="Keyword">in</span> asteroids
<span class="Keyword">for</span> roid, index <span class="Keyword">in</span> asteroids
<span class="Keyword">for</span> roid2 <span class="Keyword">in</span> asteroids <span class="Keyword">when</span> roid <span class="Keyword">isnt</span> roid2
roid.explode() <span class="Keyword">if</span> roid.overlaps roid2
</pre><pre class="idle"><span class="Storage">var</span> _i, _j, _len, _len2, _ref, _ref2, _result, food, lunch, roid, roid2;
</pre><pre class="idle"><span class="Storage">var</span> _i, _j, _len, _len2, _len3, _ref, _result, food, index, lunch, roid, roid2;
lunch <span class="Keyword">=</span> (<span class="Storage">function</span>() {
_result <span class="Keyword">=</span> []; _ref <span class="Keyword">=</span> [<span class="String"><span class="String">'</span>toast<span class="String">'</span></span>, <span class="String"><span class="String">'</span>cheese<span class="String">'</span></span>, <span class="String"><span class="String">'</span>wine<span class="String">'</span></span>];
<span class="Keyword">for</span> (_i <span class="Keyword">=</span> <span class="Number">0</span>, _len <span class="Keyword">=</span> _ref.<span class="LibraryConstant">length</span>; _i <span class="Keyword">&lt;</span> _len; _i<span class="Keyword">++</span>) {
@ -841,12 +841,10 @@ lunch <span class="Keyword">=</span> (<span class="Storage">function</span>() {
}
<span class="Keyword">return</span> _result;
})();
_ref <span class="Keyword">=</span> asteroids;
<span class="Keyword">for</span> (_i <span class="Keyword">=</span> <span class="Number">0</span>, _len <span class="Keyword">=</span> _ref.<span class="LibraryConstant">length</span>; _i <span class="Keyword">&lt;</span> _len; _i<span class="Keyword">++</span>) {
roid <span class="Keyword">=</span> _ref[_i];
_ref2 <span class="Keyword">=</span> asteroids;
<span class="Keyword">for</span> (_j <span class="Keyword">=</span> <span class="Number">0</span>, _len2 <span class="Keyword">=</span> _ref2.<span class="LibraryConstant">length</span>; _j <span class="Keyword">&lt;</span> _len2; _j<span class="Keyword">++</span>) {
roid2 <span class="Keyword">=</span> _ref2[_j];
<span class="Keyword">for</span> (index <span class="Keyword">=</span> <span class="Number">0</span>, _len2 <span class="Keyword">=</span> asteroids.<span class="LibraryConstant">length</span>; index <span class="Keyword">&lt;</span> _len2; index<span class="Keyword">++</span>) {
roid <span class="Keyword">=</span> asteroids[index];
<span class="Keyword">for</span> (_j <span class="Keyword">=</span> <span class="Number">0</span>, _len3 <span class="Keyword">=</span> asteroids.<span class="LibraryConstant">length</span>; _j <span class="Keyword">&lt;</span> _len3; _j<span class="Keyword">++</span>) {
roid2 <span class="Keyword">=</span> asteroids[_j];
<span class="Keyword">if</span> (roid <span class="Keyword">!</span><span class="Keyword">==</span> roid2) {
<span class="Keyword">if</span> (roid.overlaps(roid2)) {
roid.explode();
@ -1045,22 +1043,22 @@ six = (one = 1) + (two = 2) + (three = 3);;alert(six);'>run: six</button><br cla
<div class='code'><pre class="idle"><span class="Comment"><span class="Comment">#</span> The first ten global properties.</span>
globals <span class="Keyword">=</span> (name <span class="Keyword">for</span> name <span class="Keyword">of</span> window)[<span class="Number">0</span>...<span class="Number">10</span>]
</pre><pre class="idle"><span class="Storage">var</span> _ref, _result, globals, name;
</pre><pre class="idle"><span class="Storage">var</span> _result, globals, name;
<span class="Storage">var</span> __hasProp <span class="Keyword">=</span> <span class="LibraryClassType">Object</span>.<span class="LibraryConstant">prototype</span>.hasOwnProperty;
globals <span class="Keyword">=</span> (<span class="Storage">function</span>() {
_result <span class="Keyword">=</span> []; _ref <span class="Keyword">=</span> <span class="LibraryClassType">window</span>;
<span class="Keyword">for</span> (name <span class="Keyword">in</span> _ref) {
<span class="Keyword">if</span> (<span class="Keyword">!</span>__hasProp.<span class="LibraryFunction">call</span>(_ref, name)) <span class="Keyword">continue</span>;
_result <span class="Keyword">=</span> [];
<span class="Keyword">for</span> (name <span class="Keyword">in</span> <span class="LibraryClassType">window</span>) {
<span class="Keyword">if</span> (<span class="Keyword">!</span>__hasProp.<span class="LibraryFunction">call</span>(<span class="LibraryClassType">window</span>, name)) <span class="Keyword">continue</span>;
_result.<span class="LibraryFunction">push</span>(name);
}
<span class="Keyword">return</span> _result;
})().<span class="LibraryFunction">slice</span>(<span class="Number">0</span>, <span class="Number">10</span>);
</pre><button onclick='javascript: var _ref, _result, globals, name;
</pre><button onclick='javascript: var _result, globals, name;
var __hasProp = Object.prototype.hasOwnProperty;
globals = (function() {
_result = []; _ref = window;
for (name in _ref) {
if (!__hasProp.call(_ref, name)) continue;
_result = [];
for (name in window) {
if (!__hasProp.call(window, name)) continue;
_result.push(name);
}
return _result;
@ -1194,13 +1192,13 @@ tom.move()
</pre><pre class="idle"><span class="Storage">var</span> Animal, Horse, Snake, sam, tom;
<span class="Storage">var</span> <span class="FunctionName">__extends</span> = <span class="Storage">function</span>(<span class="FunctionArgument">child, parent</span>) {
<span class="Storage">var</span> <span class="FunctionName">ctor</span> = <span class="Storage">function</span>(){};
<span class="LibraryClassType">ctor</span>.<span class="LibraryConstant">prototype</span> = parent.<span class="LibraryConstant">prototype</span>;
<span class="LibraryClassType">child</span>.<span class="LibraryConstant">prototype</span> = <span class="Keyword">new</span> <span class="TypeName">ctor</span>();
<span class="LibraryClassType">child</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">constructor</span> = child;
<span class="Keyword">if</span> (<span class="Keyword">typeof</span> parent.extended <span class="Keyword">===</span> <span class="String"><span class="String">&quot;</span>function<span class="String">&quot;</span></span>) parent.extended(child);
child.__super__ <span class="Keyword">=</span> parent.<span class="LibraryConstant">prototype</span>;
};
<span class="Storage">var</span> <span class="FunctionName">ctor</span> = <span class="Storage">function</span>() {};
<span class="LibraryClassType">ctor</span>.<span class="LibraryConstant">prototype</span> = parent.<span class="LibraryConstant">prototype</span>;
<span class="LibraryClassType">child</span>.<span class="LibraryConstant">prototype</span> = <span class="Keyword">new</span> <span class="TypeName">ctor</span>();
<span class="LibraryClassType">child</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">constructor</span> = child;
<span class="Keyword">if</span> (<span class="Keyword">typeof</span> parent.extended <span class="Keyword">===</span> <span class="String"><span class="String">&quot;</span>function<span class="String">&quot;</span></span>) parent.extended(child);
child.__super__ <span class="Keyword">=</span> parent.<span class="LibraryConstant">prototype</span>;
};
Animal <span class="Keyword">=</span> (<span class="Storage">function</span>() {
<span class="Keyword">return</span> <span class="Storage">function</span> <span class="FunctionName">Animal</span>(<span class="FunctionArgument">_arg</span>) {
<span class="Variable">this</span>.<span class="LibraryConstant">name</span> <span class="Keyword">=</span> _arg;
@ -1236,13 +1234,13 @@ sam.move();
tom.move();
</pre><button onclick='javascript: var Animal, Horse, Snake, sam, tom;
var __extends = function(child, parent) {
var ctor = function(){};
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.prototype.constructor = child;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
};
var ctor = function() {};
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.prototype.constructor = child;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
};
Animal = (function() {
return function Animal(_arg) {
this.name = _arg;
@ -1418,8 +1416,8 @@ _ref = tag.split(""), open = _ref[0], contents = __slice.call(_ref, 1, _ref.leng
<span class="Variable">@customer</span>.purchase <span class="Variable">@cart</span>
</pre><pre class="idle"><span class="Storage">var</span> Account;
<span class="Storage">var</span> <span class="FunctionName">__bind</span> = <span class="Storage">function</span>(<span class="FunctionArgument">func, context</span>) {
<span class="Keyword">return</span> <span class="Storage">function</span>(){ <span class="Keyword">return</span> func.<span class="LibraryFunction">apply</span>(context, arguments); };
};
<span class="Keyword">return</span> <span class="Storage">function</span>() { <span class="Keyword">return</span> func.<span class="LibraryFunction">apply</span>(context, arguments); };
};
<span class="FunctionName">Account</span> = <span class="Storage">function</span>(<span class="FunctionArgument">customer, cart</span>) {
<span class="Variable">this</span>.customer <span class="Keyword">=</span> customer;
<span class="Variable">this</span>.cart <span class="Keyword">=</span> cart;

View file

@ -55,11 +55,10 @@
});
};
printTasks = function() {
var _ref, desc, name, spaces, task;
var desc, name, spaces, task;
puts('');
_ref = tasks;
for (name in _ref) {
task = _ref[name];
for (name in tasks) {
task = tasks[name];
spaces = 20 - name.length;
spaces = spaces > 0 ? Array(spaces + 1).join(' ') : '';
desc = task.description ? ("# " + (task.description)) : '';

View file

@ -49,12 +49,12 @@
return compileScripts();
};
compileScripts = function() {
var _i, _len, _ref2, _result;
_result = []; _ref2 = sources;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
var _i, _len, _result;
_result = [];
for (_i = 0, _len = sources.length; _i < _len; _i++) {
(function() {
var base, compile;
var source = _ref2[_i];
var source = sources[_i];
return _result.push((function() {
base = source;
compile = function(source, topLevel) {

View file

@ -1639,7 +1639,7 @@
return '';
};
ForNode.prototype.compileNode = function(o) {
var body, codeInBody, forPart, guardPart, idt1, index, ivar, lvar, name, namePart, range, ref, returnResult, rvar, scope, source, sourcePart, stepPart, svar, topLevel, varPart, vars;
var _ref2, body, codeInBody, forPart, guardPart, idt1, index, ivar, lvar, name, namePart, range, returnResult, rvar, scope, source, sourcePart, stepPart, svar, topLevel, varPart, vars;
topLevel = del(o, 'top') && !this.returns;
range = this.source instanceof ValueNode && this.source.base instanceof RangeNode && !this.source.properties.length;
source = range ? this.source.base : this.source;
@ -1677,16 +1677,12 @@
step: this.step
}));
} else {
svar = this.source.compile(o);
if (IDENTIFIER.test(svar) && scope.check(svar, {
immediate: true
})) {
sourcePart = '';
} else {
ref = scope.freeVariable('ref');
sourcePart = ("" + ref + " = " + svar + ";");
svar = ref;
}
_ref2 = this.source.compileReference(merge(o, {
top: true
}), {
precompile: true
}), sourcePart = _ref2[0], svar = _ref2[1];
sourcePart = sourcePart === svar ? '' : ("" + sourcePart + ";");
namePart = this.pattern ? new AssignNode(this.name, literal("" + svar + "[" + ivar + "]")).compile(merge(o, {
top: true
})) : (name ? ("" + name + " = " + svar + "[" + ivar + "]") : undefined);

View file

@ -281,11 +281,10 @@
levels = {};
openLine = {};
this.scanTokens(function(token, i) {
var _i, _len, _ref, _ref2, close, open, tag;
var _i, _len, _ref, close, open, tag;
tag = token[0];
_ref = pairs;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
_ref2 = _ref[_i], open = _ref2[0], close = _ref2[1];
for (_i = 0, _len = pairs.length; _i < _len; _i++) {
_ref = pairs[_i], open = _ref[0], close = _ref[1];
levels[open] |= 0;
if (tag === open) {
if (levels[open] === 0) {
@ -316,11 +315,10 @@
}
};
exports.Rewriter.prototype.rewriteClosingParens = function() {
var _ref, debt, key, stack;
var debt, key, stack;
stack = [];
debt = {};
_ref = INVERSES;
for (key in _ref) {
for (key in INVERSES) {
(debt[key] = 0);
}
return this.scanTokens(function(token, i, tokens) {

View file

@ -1429,13 +1429,8 @@ exports.ForNode = class ForNode extends BaseNode
sourcePart = source.compileVariables(o)
forPart = source.compile merge o, index: ivar, step: @step
else
svar = @source.compile o
if IDENTIFIER.test(svar) and scope.check(svar, immediate: on)
sourcePart = ''
else
ref = scope.freeVariable 'ref'
sourcePart = "#{ref} = #{svar};"
svar = ref
[sourcePart, svar] = @source.compileReference merge(o, top: yes), precompile: yes
sourcePart = if sourcePart is svar then '' else "#{sourcePart};"
namePart = if @pattern
new AssignNode(@name, literal "#{svar}[#{ivar}]").compile merge o, top: on
else if name