Use a constructor to create the execute cache objects

This commit is contained in:
Michael "Z" Goddard 2018-05-04 12:33:42 -04:00
parent a9a23ef589
commit 9b82530f51
No known key found for this signature in database
GPG key ID: 762CD40DD5349872
2 changed files with 32 additions and 10 deletions

View file

@ -876,21 +876,22 @@ class Blocks {
* @param {string} blockId blockId for the desired execute cache * @param {string} blockId blockId for the desired execute cache
* @return {object} execute cache object * @return {object} execute cache object
*/ */
BlocksExecuteCache.getCached = function (blocks, blockId) { BlocksExecuteCache.getCached = function (blocks, blockId, CacheType = Object) {
const block = blocks.getBlock(blockId);
if (typeof block === 'undefined') return null;
let cached = blocks._cache._executeCached[blockId]; let cached = blocks._cache._executeCached[blockId];
if (typeof cached !== 'undefined') { if (typeof cached !== 'undefined') {
return cached; return cached;
} }
cached = { const block = blocks.getBlock(blockId);
if (typeof block === 'undefined') return null;
cached = new CacheType({
_initialized: false, _initialized: false,
opcode: blocks.getOpcode(block), opcode: blocks.getOpcode(block),
fields: blocks.getFields(block), fields: blocks.getFields(block),
inputs: blocks.getInputs(block), inputs: blocks.getInputs(block),
mutation: blocks.getMutation(block) mutation: blocks.getMutation(block)
}; });
blocks._cache._executeCached[blockId] = cached; blocks._cache._executeCached[blockId] = cached;
return cached; return cached;
}; };

View file

@ -118,6 +118,28 @@ const FieldKind = {
DYNAMIC: 'DYNAMIC' DYNAMIC: 'DYNAMIC'
}; };
const BlockCached = function (cached) {
this._initialized = false;
this.opcode = cached.opcode;
this.fields = cached.fields;
this.inputs = cached.inputs;
this.mutation = cached.mutation;
this._isHat = false;
this._blockFunction = null;
this._definedBlockFunction = false;
this._isShadowBlock = false;
this._shadowValue = null;
this._fields = null;
this._fieldKind = FieldKind.NONE;
this._fieldVariable = null;
this._fieldList = null;
this._fieldBroadcastOption = null;
this._argValues = null;
this._inputs = null;
};
/** /**
* Execute a block. * Execute a block.
* @param {!Sequencer} sequencer Which sequencer is executing. * @param {!Sequencer} sequencer Which sequencer is executing.
@ -132,12 +154,12 @@ const execute = function (sequencer, thread, recursiveCall) {
const currentStackFrame = thread.peekStackFrame(); const currentStackFrame = thread.peekStackFrame();
let blockContainer = thread.blockContainer; let blockContainer = thread.blockContainer;
let block = blockContainer.getBlock(currentBlockId); let blockCached = BlocksExecuteCache.getCached(blockContainer, currentBlockId, BlockCached);;
if (typeof block === 'undefined') { if (blockCached === null) {
blockContainer = runtime.flyoutBlocks; blockContainer = runtime.flyoutBlocks;
block = blockContainer.getBlock(currentBlockId); blockCached = BlocksExecuteCache.getCached(blockContainer, currentBlockId, BlockCached);
// Stop if block or target no longer exists. // Stop if block or target no longer exists.
if (typeof block === 'undefined') { if (blockCached === null) {
// No block found: stop the thread; script no longer exists. // No block found: stop the thread; script no longer exists.
sequencer.retireThread(thread); sequencer.retireThread(thread);
return; return;
@ -158,7 +180,6 @@ const execute = function (sequencer, thread, recursiveCall) {
// Blocks is modified in the editor these cached objects will be cleaned up // Blocks is modified in the editor these cached objects will be cleaned up
// and new cached copies can be created. This lets us optimize this critical // and new cached copies can be created. This lets us optimize this critical
// path while keeping up to date with editor changes to a project. // path while keeping up to date with editor changes to a project.
const blockCached = BlocksExecuteCache.getCached(blockContainer, currentBlockId);
if (blockCached._initialized !== true) { if (blockCached._initialized !== true) {
const {opcode, fields, inputs} = blockCached; const {opcode, fields, inputs} = blockCached;