Reuse argValues

Block args set by fields are static and never change. Inputs that do
change are always set onto args. With these two assumptions we can
reuse the same objects for each execution of the same block instead of
constantly creating them and letting them be garbage collected.
This commit is contained in:
Michael "Z" Goddard 2018-05-04 12:39:48 -04:00
parent 9b82530f51
commit 5fd749918f
No known key found for this signature in database
GPG key ID: 762CD40DD5349872

View file

@ -201,21 +201,43 @@ const execute = function (sequencer, thread, recursiveCall) {
blockCached._fieldKind = fieldKeys.length > 0 ? FieldKind.DYNAMIC : FieldKind.NONE; blockCached._fieldKind = fieldKeys.length > 0 ? FieldKind.DYNAMIC : FieldKind.NONE;
if (fieldKeys.length === 1 && fieldKeys.includes('VARIABLE')) { if (fieldKeys.length === 1 && fieldKeys.includes('VARIABLE')) {
blockCached._fieldKind = FieldKind.VARIABLE; blockCached._fieldKind = FieldKind.VARIABLE;
blockCached._fieldVariable = { blockCached._argValues = {
VARIABLE: {
id: fields.VARIABLE.id, id: fields.VARIABLE.id,
name: fields.VARIABLE.value name: fields.VARIABLE.value
},
mutation: blockCached.mutation
}; };
} else if (fieldKeys.length === 1 && fieldKeys.includes('LIST')) { } else if (fieldKeys.length === 1 && fieldKeys.includes('LIST')) {
blockCached._fieldKind = FieldKind.LIST; blockCached._fieldKind = FieldKind.LIST;
blockCached._fieldList = { blockCached._argValues = {
LIST: {
id: fields.LIST.id, id: fields.LIST.id,
name: fields.LIST.value name: fields.LIST.value
},
mutation: blockCached.mutation
}; };
} else if (fieldKeys.length === 1 && fieldKeys.includes('BROADCAST_OPTION')) { } else if (fieldKeys.length === 1 && fieldKeys.includes('BROADCAST_OPTION')) {
blockCached._fieldKind = FieldKind.BROADCAST_OPTION; blockCached._fieldKind = FieldKind.BROADCAST_OPTION;
blockCached._fieldBroadcastOption = { blockCached._argValues = {
BROADCAST_OPTION: {
id: fields.BROADCAST_OPTION.id, id: fields.BROADCAST_OPTION.id,
name: fields.BROADCAST_OPTION.value name: fields.BROADCAST_OPTION.value
},
mutation: blockCached.mutation
};
} else if (fieldKeys.length > 0) {
// FieldKind.DYNAMIC
blockCached._argValues = {
mutation: blockCached.mutation
};
for (const fieldName in fields) {
blockCached._argValues[fieldName] = fields[fieldName].value;
}
} else {
// FieldKind.NONE
blockCached._argValues = {
mutation: blockCached.mutation
}; };
} }
@ -228,9 +250,7 @@ const execute = function (sequencer, thread, recursiveCall) {
} }
const opcode = blockCached.opcode; const opcode = blockCached.opcode;
const fields = blockCached._fields;
const inputs = blockCached._inputs; const inputs = blockCached._inputs;
const mutation = blockCached.mutation;
const blockFunction = blockCached._blockFunction; const blockFunction = blockCached._blockFunction;
const isHat = blockCached._isHat; const isHat = blockCached._isHat;
@ -260,29 +280,10 @@ const execute = function (sequencer, thread, recursiveCall) {
return; return;
} }
// Generate values for arguments (inputs). // Update values for arguments (inputs).
const argValues = {}; let argValues = blockCached._argValues;
// Add all fields on this block to the argValues. Some known fields may // Fields are set during blockCached initialization.
// appear by themselves and can be set to argValues quicker by setting them
// explicitly.
if (blockCached._fieldKind !== FieldKind.NONE) {
switch (blockCached._fieldKind) {
case FieldKind.VARIABLE:
argValues.VARIABLE = blockCached._fieldVariable;
break;
case FieldKind.LIST:
argValues.LIST = blockCached._fieldList;
break;
case FieldKind.BROADCAST_OPTION:
argValues.BROADCAST_OPTION = blockCached._fieldBroadcastOption;
break;
default:
for (const fieldName in fields) {
argValues[fieldName] = fields[fieldName].value;
}
}
}
// Recursively evaluate input blocks. // Recursively evaluate input blocks.
for (const inputName in inputs) { for (const inputName in inputs) {
@ -363,9 +364,6 @@ const execute = function (sequencer, thread, recursiveCall) {
} }
} }
// Add any mutation to args (e.g., for procedures).
argValues.mutation = mutation;
let primitiveReportedValue = null; let primitiveReportedValue = null;
blockUtility.sequencer = sequencer; blockUtility.sequencer = sequencer;
blockUtility.thread = thread; blockUtility.thread = thread;