From 5fd749918f0d9a3e894124ba0a41bc4ac3ec9f90 Mon Sep 17 00:00:00 2001 From: "Michael \"Z\" Goddard" Date: Fri, 4 May 2018 12:39:48 -0400 Subject: [PATCH] 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. --- src/engine/execute.js | 70 +++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/src/engine/execute.js b/src/engine/execute.js index 69468688e..e01908f58 100644 --- a/src/engine/execute.js +++ b/src/engine/execute.js @@ -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;