From 41f2f61b94f26f9ce035caa7e72dcfa52345af15 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 1 Apr 2017 10:42:47 +0200 Subject: [PATCH] - minor VM optimization: Precalculate a function's frame size when compiling it instead of doing it each time it gets called. This made up ca. 10% of the 'call' instruction's execution time. --- src/scripting/backend/vmbuilder.cpp | 1 + src/scripting/vm/vm.h | 1 + src/scripting/vm/vmframe.cpp | 4 +--- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/scripting/backend/vmbuilder.cpp b/src/scripting/backend/vmbuilder.cpp index 4ed4f5c4e..e4682432c 100644 --- a/src/scripting/backend/vmbuilder.cpp +++ b/src/scripting/backend/vmbuilder.cpp @@ -139,6 +139,7 @@ void VMFunctionBuilder::MakeFunction(VMScriptFunction *func) func->NumRegA = Registers[REGT_POINTER].MostUsed; func->NumRegS = Registers[REGT_STRING].MostUsed; func->MaxParam = MaxParam; + func->StackSize = VMFrame::FrameSize(func->NumRegD, func->NumRegF, func->NumRegS, func->NumRegA, func->MaxParam, func->ExtraSpace); // Technically, there's no reason why we can't end the function with // entries on the parameter stack, but it means the caller probably diff --git a/src/scripting/vm/vm.h b/src/scripting/vm/vm.h index 42558da09..b95be74e3 100644 --- a/src/scripting/vm/vm.h +++ b/src/scripting/vm/vm.h @@ -716,6 +716,7 @@ public: int ExtraSpace; int CodeSize; // Size of code in instructions (not bytes) unsigned LineInfoCount; + unsigned StackSize; VM_UBYTE NumRegD; VM_UBYTE NumRegF; VM_UBYTE NumRegS; diff --git a/src/scripting/vm/vmframe.cpp b/src/scripting/vm/vmframe.cpp index e2ec3865a..4a0a2538a 100644 --- a/src/scripting/vm/vmframe.cpp +++ b/src/scripting/vm/vmframe.cpp @@ -273,10 +273,8 @@ VMFrameStack::~VMFrameStack() VMFrame *VMFrameStack::AllocFrame(VMScriptFunction *func) { - int size = VMFrame::FrameSize(func->NumRegD, func->NumRegF, func->NumRegS, func->NumRegA, - func->MaxParam, func->ExtraSpace); - VMFrame *frame = Alloc(size); frame->Func = func; + VMFrame *frame = Alloc(func->StackSize); frame->NumRegD = func->NumRegD; frame->NumRegF = func->NumRegF; frame->NumRegS = func->NumRegS;