From fbf2c0c34527261e31da910d61b15aaef9fc3b41 Mon Sep 17 00:00:00 2001 From: griffpatch Date: Sat, 28 Jan 2017 17:06:07 +0000 Subject: [PATCH] Optimisation - Recycle Stack Frame for cosecutive blocks This saves popping, destroying, recreating, and pushing the stack frame, and then reassigning the warp mode attribute for every block to block step in the execution. --- src/engine/thread.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/engine/thread.js b/src/engine/thread.js index 765f48b71..452d72360 100644 --- a/src/engine/thread.js +++ b/src/engine/thread.js @@ -115,6 +115,22 @@ Thread.prototype.pushStack = function (blockId) { } }; +/** + * Reset the stack frame for use by the next block. + * (avoids popping and re-pushing a new stack frame - keeps the warpmode the same + * @param {string} blockId Block ID to push to stack. + */ +Thread.prototype.reuseStackForNextBlock = function (blockId) { + this.stack[this.stack.length - 1] = blockId; + var frame = this.stackFrames[this.stackFrames.length - 1]; + frame.isLoop = false; + // frame.warpMode = warpMode; // warp mode stays the same when reusing the stack frame. + frame.reported = {}; + frame.waitingReporter = null; + frame.params = {}; + frame.executionContext = {}; +}; + /** * Pop last block on the stack and its stack frame. * @return {string} Block ID popped from the stack. @@ -203,15 +219,7 @@ Thread.prototype.atStackTop = function () { */ Thread.prototype.goToNextBlock = function () { var nextBlockId = this.target.blocks.getNextBlock(this.peekStack()); - // Copy warp mode to next block. - var warpMode = this.peekStackFrame().warpMode; - // The current block is on the stack - pop it and push the next. - // Note that this could push `null` - that is handled by the sequencer. - this.popStack(); - this.pushStack(nextBlockId); - if (this.peekStackFrame()) { - this.peekStackFrame().warpMode = warpMode; - } + this.reuseStackForNextBlock(nextBlockId); }; /**