Skip to content

Commit

Permalink
Simplify position references to improve performance of if statements
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Livesey committed Sep 14, 2024
1 parent d4ee19b commit 0e57ddd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
11 changes: 9 additions & 2 deletions dist/libvoxel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 9 additions & 2 deletions src/executors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
27 changes: 9 additions & 18 deletions tools/vxbuild-js/statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tools/vxbuild/tokeniser.vxl
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 0e57ddd

Please sign in to comment.