mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 23:12:24 -05:00
optimisation/avoid negative index lookups #407
Run-time Optimisation The thread.stackFrames array is accessed all the time to retrieve the 'parent' stack frame. This is done using as index of [this.stack.length - 1]. However, a lot of the time this evaluated to [-1]. Although this results in null, which is fine, to get to this javascript actually defers from a numeric array lookup to an object lookup using the string "-1". This is roughly 100 times slower to compute and so a simple catch for negative indexes is well worth the extra check.
This commit is contained in:
parent
6f8eaaa101
commit
a25e117412
1 changed files with 3 additions and 4 deletions
|
@ -101,7 +101,6 @@ Thread.prototype.pushStack = function (blockId) {
|
||||||
if (this.stack.length > this.stackFrames.length) {
|
if (this.stack.length > this.stackFrames.length) {
|
||||||
// Copy warp mode from any higher level.
|
// Copy warp mode from any higher level.
|
||||||
var warpMode = false;
|
var warpMode = false;
|
||||||
if (this.stackFrames[this.stackFrames.length - 1]) {
|
|
||||||
warpMode = this.stackFrames[this.stackFrames.length - 1].warpMode;
|
warpMode = this.stackFrames[this.stackFrames.length - 1].warpMode;
|
||||||
}
|
}
|
||||||
this.stackFrames.push({
|
this.stackFrames.push({
|
||||||
|
@ -129,7 +128,7 @@ Thread.prototype.popStack = function () {
|
||||||
* @return {?string} Block ID on top of stack.
|
* @return {?string} Block ID on top of stack.
|
||||||
*/
|
*/
|
||||||
Thread.prototype.peekStack = function () {
|
Thread.prototype.peekStack = function () {
|
||||||
return this.stack[this.stack.length - 1];
|
return this.stack.length > 0 ? this.stack[this.stack.length - 1] : null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -138,7 +137,7 @@ Thread.prototype.peekStack = function () {
|
||||||
* @return {?Object} Last stack frame stored on this thread.
|
* @return {?Object} Last stack frame stored on this thread.
|
||||||
*/
|
*/
|
||||||
Thread.prototype.peekStackFrame = function () {
|
Thread.prototype.peekStackFrame = function () {
|
||||||
return this.stackFrames[this.stackFrames.length - 1];
|
return this.stackFrames.length > 0 ? this.stackFrames[this.stackFrames.length - 1] : null;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -146,7 +145,7 @@ Thread.prototype.peekStackFrame = function () {
|
||||||
* @return {?Object} Second to last stack frame stored on this thread.
|
* @return {?Object} Second to last stack frame stored on this thread.
|
||||||
*/
|
*/
|
||||||
Thread.prototype.peekParentStackFrame = function () {
|
Thread.prototype.peekParentStackFrame = function () {
|
||||||
return this.stackFrames[this.stackFrames.length - 2];
|
return this.stackFrames.length > 1 ? this.stackFrames[this.stackFrames.length - 2] : null;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue