From 0e57ddd061abf20c91518b5e7e3611966483b0ba Mon Sep 17 00:00:00 2001 From: James Livesey Date: Sat, 14 Sep 2024 14:53:26 +0100 Subject: [PATCH] Simplify position references to improve performance of `if` statements --- dist/libvoxel.h | 11 +++++++++-- src/executors.h | 11 +++++++++-- tools/vxbuild-js/statements.js | 27 +++++++++------------------ tools/vxbuild/tokeniser.vxl | 2 +- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/dist/libvoxel.h b/dist/libvoxel.h index 3761620..1c8a048 100644 --- a/dist/libvoxel.h +++ b/dist/libvoxel.h @@ -6154,9 +6154,16 @@ VOXEL_ERRORABLE voxel_stepExecutor(voxel_Executor* executor) { voxel_Thing* function = voxel_newFunctionPosRef(executor->context, referencedPosition); - VOXEL_MUST(voxel_setScopeItem(executor->scope, (voxel_Thing*)posRefKey.value, function)); + voxel_Thing* posRefKeyValue = (voxel_Thing*)posRefKey.value; + + if (posRefKeyValue->type != VOXEL_TYPE_NULL) { + VOXEL_MUST(voxel_setScopeItem(executor->scope, (voxel_Thing*)posRefKey.value, function)); + VOXEL_MUST(voxel_unreferenceThing(executor->context, function)); + } else { + VOXEL_MUST(voxel_pushOntoList(executor->context, executor->valueStack, function)); + } + VOXEL_MUST(voxel_unreferenceThing(executor->context, (voxel_Thing*)posRefKey.value)); - VOXEL_MUST(voxel_unreferenceThing(executor->context, function)); break; } diff --git a/src/executors.h b/src/executors.h index 7916009..679a6f4 100644 --- a/src/executors.h +++ b/src/executors.h @@ -404,9 +404,16 @@ VOXEL_ERRORABLE voxel_stepExecutor(voxel_Executor* executor) { voxel_Thing* function = voxel_newFunctionPosRef(executor->context, referencedPosition); - VOXEL_MUST(voxel_setScopeItem(executor->scope, (voxel_Thing*)posRefKey.value, function)); + voxel_Thing* posRefKeyValue = (voxel_Thing*)posRefKey.value; + + if (posRefKeyValue->type != VOXEL_TYPE_NULL) { + VOXEL_MUST(voxel_setScopeItem(executor->scope, (voxel_Thing*)posRefKey.value, function)); + VOXEL_MUST(voxel_unreferenceThing(executor->context, function)); + } else { + VOXEL_MUST(voxel_pushOntoList(executor->context, executor->valueStack, function)); + } + VOXEL_MUST(voxel_unreferenceThing(executor->context, (voxel_Thing*)posRefKey.value)); - VOXEL_MUST(voxel_unreferenceThing(executor->context, function)); break; } diff --git a/tools/vxbuild-js/statements.js b/tools/vxbuild-js/statements.js index 8c44349..7cbe697 100644 --- a/tools/vxbuild-js/statements.js +++ b/tools/vxbuild-js/statements.js @@ -248,30 +248,21 @@ export class IfStatementNode extends ast.AstNode { ) : codeGen.bytes(); var skipFalseCode = this.skipFalseSymbol ? codeGen.join( - this.skipFalseSymbol.generateCode(options), - codeGen.bytes(codeGen.vxcTokens.GET, codeGen.vxcTokens.JUMP) + codeGen.bytes(codeGen.vxcTokens.NULL, codeGen.vxcTokens.POS_REF_FORWARD), + codeGen.int32(isFalseCode.length + 1), + codeGen.bytes(codeGen.vxcTokens.JUMP) + ) : codeGen.bytes(); // Not necessary if we do not need to skip true statement var skipTrueCode = codeGen.join( - this.skipTrueSymbol.generateCode(options), - codeGen.bytes(codeGen.vxcTokens.GET, codeGen.vxcTokens.JUMP_IF_TRUTHY) - ); - - var skipFalseDefinitionCode = this.skipFalseSymbol ? codeGen.join( - this.skipFalseSymbol.generateCode(options), - codeGen.bytes(codeGen.vxcTokens.POS_REF_FORWARD), - codeGen.int32(notConditionCode.length + skipTrueCode.length + isTrueCode.length + skipFalseCode.length + isFalseCode.length) - ) : codeGen.bytes(); - - var skipTrueDefinitionCode = codeGen.join( - this.skipTrueSymbol.generateCode(options), - codeGen.bytes(codeGen.vxcTokens.POS_REF_FORWARD), - codeGen.int32(skipFalseDefinitionCode.length + notConditionCode.length + skipTrueCode.length + isTrueCode.length + skipFalseCode.length) + codeGen.bytes(codeGen.vxcTokens.NULL, codeGen.vxcTokens.POS_REF_FORWARD), + codeGen.int32(isTrueCode.length + skipFalseCode.length + isFalseCode.length + 1), + codeGen.bytes(codeGen.vxcTokens.JUMP_IF_TRUTHY) ); return codeGen.join( - skipTrueDefinitionCode, - skipFalseDefinitionCode, + // skipTrueDefinitionCode, + // skipFalseDefinitionCode, notConditionCode, skipTrueCode, isTrueCode, diff --git a/tools/vxbuild/tokeniser.vxl b/tools/vxbuild/tokeniser.vxl index 11f4536..1767075 100644 --- a/tools/vxbuild/tokeniser.vxl +++ b/tools/vxbuild/tokeniser.vxl @@ -413,7 +413,7 @@ export function tokenise(sourceContainer) { ( (currentByte >= Byte("a") &&& currentByte <= Byte("z")) ||| (currentByte >= Byte("A") &&& currentByte <= Byte("Z")) ||| - currentByte == Byte("_") || currentByte == Byte("$") + currentByte == Byte("_") ||| currentByte == Byte("$") ) && matchIdentifier() ) { if (KEYWORDS.contains(matchedString)) {