mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 06:52:40 -05:00
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:
parent
9b82530f51
commit
5fd749918f
1 changed files with 34 additions and 36 deletions
|
@ -201,21 +201,43 @@ const execute = function (sequencer, thread, recursiveCall) {
|
|||
blockCached._fieldKind = fieldKeys.length > 0 ? FieldKind.DYNAMIC : FieldKind.NONE;
|
||||
if (fieldKeys.length === 1 && fieldKeys.includes('VARIABLE')) {
|
||||
blockCached._fieldKind = FieldKind.VARIABLE;
|
||||
blockCached._fieldVariable = {
|
||||
id: fields.VARIABLE.id,
|
||||
name: fields.VARIABLE.value
|
||||
blockCached._argValues = {
|
||||
VARIABLE: {
|
||||
id: fields.VARIABLE.id,
|
||||
name: fields.VARIABLE.value
|
||||
},
|
||||
mutation: blockCached.mutation
|
||||
};
|
||||
} else if (fieldKeys.length === 1 && fieldKeys.includes('LIST')) {
|
||||
blockCached._fieldKind = FieldKind.LIST;
|
||||
blockCached._fieldList = {
|
||||
id: fields.LIST.id,
|
||||
name: fields.LIST.value
|
||||
blockCached._argValues = {
|
||||
LIST: {
|
||||
id: fields.LIST.id,
|
||||
name: fields.LIST.value
|
||||
},
|
||||
mutation: blockCached.mutation
|
||||
};
|
||||
} else if (fieldKeys.length === 1 && fieldKeys.includes('BROADCAST_OPTION')) {
|
||||
blockCached._fieldKind = FieldKind.BROADCAST_OPTION;
|
||||
blockCached._fieldBroadcastOption = {
|
||||
id: fields.BROADCAST_OPTION.id,
|
||||
name: fields.BROADCAST_OPTION.value
|
||||
blockCached._argValues = {
|
||||
BROADCAST_OPTION: {
|
||||
id: fields.BROADCAST_OPTION.id,
|
||||
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 fields = blockCached._fields;
|
||||
const inputs = blockCached._inputs;
|
||||
const mutation = blockCached.mutation;
|
||||
const blockFunction = blockCached._blockFunction;
|
||||
const isHat = blockCached._isHat;
|
||||
|
||||
|
@ -260,29 +280,10 @@ const execute = function (sequencer, thread, recursiveCall) {
|
|||
return;
|
||||
}
|
||||
|
||||
// Generate values for arguments (inputs).
|
||||
const argValues = {};
|
||||
// Update values for arguments (inputs).
|
||||
let argValues = blockCached._argValues;
|
||||
|
||||
// Add all fields on this block to the argValues. Some known fields may
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Fields are set during blockCached initialization.
|
||||
|
||||
// Recursively evaluate input blocks.
|
||||
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;
|
||||
blockUtility.sequencer = sequencer;
|
||||
blockUtility.thread = thread;
|
||||
|
|
Loading…
Reference in a new issue