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;
|
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 = {
|
||||||
id: fields.VARIABLE.id,
|
VARIABLE: {
|
||||||
name: fields.VARIABLE.value
|
id: fields.VARIABLE.id,
|
||||||
|
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 = {
|
||||||
id: fields.LIST.id,
|
LIST: {
|
||||||
name: fields.LIST.value
|
id: fields.LIST.id,
|
||||||
|
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 = {
|
||||||
id: fields.BROADCAST_OPTION.id,
|
BROADCAST_OPTION: {
|
||||||
name: fields.BROADCAST_OPTION.value
|
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 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;
|
||||||
|
|
Loading…
Reference in a new issue