diff options
author | Alon Zakai <alonzakai@gmail.com> | 2011-11-30 17:14:57 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2011-11-30 17:14:57 -0800 |
commit | 8794769222e617f5b35fb77973114d3d4a70dd14 (patch) | |
tree | 897f34a3a122db1de05a519d191db92a8bb9fa8b | |
parent | 13a141ce63875596a9bac36d71577b25e6f1988e (diff) |
minor optimization to main loop in framework
-rw-r--r-- | src/framework.js | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/src/framework.js b/src/framework.js index d830599d..e83250f3 100644 --- a/src/framework.js +++ b/src/framework.js @@ -164,25 +164,25 @@ Substrate.prototype = { var finalResult = null; this.results = []; var finished = false; - var that = this; + var onResult = this.onResult; var actors = values(this.actors); // Assumes actors are not constantly added while (!finished) { dprint('framework', "Cycle start, items: ");// + values(this.actors).map(function(actor) actor.items).reduce(function(x,y) x+y, 0)); var hadProcessing = false; - actors.forEach(function(actor) { - midComment(); + for (var i = 0; i < actors.length; i++) { + var actor = actors[i]; + if (actor.inbox.length == 0 && actor.items.length == 0) continue; - that.checkInbox(actor); - if (actor.items.length == 0) return; + midComment(); - var inputs = actor.items; + this.checkInbox(actor); var outputs; - var currResultCount = that.results.length; - dprint('framework', 'Processing using ' + actor.name_ + ': ' + inputs.length); + var currResultCount = this.results.length; + dprint('framework', 'Processing using ' + actor.name_ + ': ' + actor.items.length); + outputs = actor.process(actor.items); actor.items = []; - outputs = actor.process(inputs); if (DEBUG_MEMORY) MemoryDebugger.tick('actor ' + actor.name_); - dprint('framework', 'New results: ' + (outputs.length + that.results.length - currResultCount) + ' out of ' + (that.results.length + outputs.length)); + dprint('framework', 'New results: ' + (outputs.length + this.results.length - currResultCount) + ' out of ' + (this.results.length + outputs.length)); hadProcessing = true; if (outputs) { @@ -195,16 +195,16 @@ Substrate.prototype = { finalResult = outputs[0]; } else { outputs.forEach(function(output) { delete output.tokens }); // clean up tokens to save memory - that.results = that.results.concat(outputs); + this.results = this.results.concat(outputs); } } - }); + } if (!hadProcessing) { if (DEBUG) print("Solving complete: no remaining items"); finalComment(); this.results.forEach(function(result) { delete result.__uid__; // Might recycle these - if (that.onResult) that.onResult(result); + if (onResult) onResult(result); }); ret = this.results; break; |