Skip to content

Commit

Permalink
Optimize VMFrame Creation (#53)
Browse files Browse the repository at this point in the history
 - move most bits to the constructor
- do not initialize arguments with nil since they will be set during the
call
  • Loading branch information
smarr authored Aug 11, 2024
2 parents 11d1094 + 4ce1f21 commit 52e8c0e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/unitTests/WalkObjectsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ void WalkObjectsTest::testWalkFrame() {
Universe::NewMethod(methodSymbol, 0, 0, 0, 0,
new LexicalScope(nullptr, {}, {}), inlinedLoops);

VMFrame* frame = Universe::NewFrame(nullptr, method);
frame->SetPreviousFrame(frame->CloneForMovingGC());
VMFrame* prev = Universe::NewFrame(nullptr, method);
VMFrame* frame = Universe::NewFrame(prev, method);
frame->SetContext(frame->CloneForMovingGC());
VMInteger* dummyArg = Universe::NewInteger(1111);
frame->SetArgument(0, 0, dummyArg);
Expand Down
3 changes: 0 additions & 3 deletions src/unitTests/WriteBarrierTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ void WriteBarrierTest::testWriteFrame() {
VMFrame* frame = Interpreter::GetFrame()->CloneForMovingGC();
frame->SetContext(frame->CloneForMovingGC());

frame->SetPreviousFrame(Interpreter::GetFrame());
TEST_WB_CALLED("VMFrame failed to call writeBarrier on SetPreviousFrame",
frame, Interpreter::GetFrame());
frame->SetContext(frame->GetContext()->CloneForMovingGC());
TEST_WB_CALLED("VMFrame failed to call writeBarrier on SetContext", frame,
frame->GetContext());
Expand Down
6 changes: 3 additions & 3 deletions src/vm/IsValidObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ void obtain_vtables_of_known_classes(VMSymbol* someValidSymbol) {
new (GetHeap<HEAP_CLS>(), 0) VMEvaluationPrimitive(1);
vt_eval_primitive = get_vtable(ev);

VMFrame* frm = new (GetHeap<HEAP_CLS>(), 0) VMFrame(0, 0);
vt_frame = get_vtable(frm);

VMInteger* i = new (GetHeap<HEAP_CLS>(), 0) VMInteger(0);
vt_integer = get_vtable(i);

Expand All @@ -193,6 +190,9 @@ void obtain_vtables_of_known_classes(VMSymbol* someValidSymbol) {
vt_method = get_vtable(mth);
vt_object = get_vtable(load_ptr(nilObject));

VMFrame* frm = new (GetHeap<HEAP_CLS>(), 0) VMFrame(0, mth, nullptr);
vt_frame = get_vtable(frm);

VMPrimitive* prm =
new (GetHeap<HEAP_CLS>(), 0) VMPrimitive(someValidSymbol, FramePrim());
vt_primitive = get_vtable(prm);
Expand Down
10 changes: 6 additions & 4 deletions src/vm/Universe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ vm_oop_t Universe::interpretMethod(VMObject* receiver, VMInvokable* initialize,
VMMethod* bootstrapMethod = createBootstrapMethod(load_ptr(systemClass), 2);

VMFrame* bootstrapFrame = Interpreter::PushNewFrame(bootstrapMethod);
for (size_t argIdx = 0; argIdx < bootstrapMethod->GetNumberOfArguments();
argIdx += 1) {
bootstrapFrame->SetArgument((long)argIdx, (long)0, load_ptr(nilObject));
}

bootstrapFrame->Push(receiver);

if (argumentsArray != nullptr) {
Expand Down Expand Up @@ -721,10 +726,7 @@ VMFrame* Universe::NewFrame(VMFrame* previousFrame, VMMethod* method) {

size_t additionalBytes = length * sizeof(VMObject*);
result = new (GetHeap<HEAP_CLS>(), additionalBytes)
VMFrame(length, additionalBytes);
result->method = store_root(method);
result->previousFrame = store_root(previousFrame);
result->ResetStackPointer();
VMFrame(additionalBytes, method, previousFrame);

LOG_ALLOCATION("VMFrame", result->GetObjectSize());
return result;
Expand Down
25 changes: 1 addition & 24 deletions src/vmobjects/VMFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@ VMFrame* VMFrame::EmergencyFrameFrom(VMFrame* from, long extraLength) {

size_t additionalBytes = length * sizeof(VMObject*);
VMFrame* result = new (GetHeap<HEAP_CLS>(), additionalBytes)
VMFrame(length, additionalBytes);
VMFrame(additionalBytes, method, from->GetPreviousFrame());

result->clazz = nullptr; // result->SetClass(from->GetClass());

// set Frame members
result->SetPreviousFrame(from->GetPreviousFrame());
result->SetMethod(method);
result->SetContext(from->GetContext());
result->stack_ptr =
(gc_oop_t*)SHIFTED_PTR(result, (size_t)from->stack_ptr - (size_t)from);
Expand Down Expand Up @@ -124,27 +122,6 @@ VMFrame* VMFrame::CloneForMovingGC() const {

const long VMFrame::VMFrameNumberOfFields = 0;

VMFrame::VMFrame(size_t, size_t additionalBytes)
: VMObject(0, additionalBytes + sizeof(VMFrame)), bytecodeIndex(0),
previousFrame(nullptr), context(nullptr), method(nullptr) {
arguments = (gc_oop_t*)&(stack_ptr) + 1;
locals = arguments;
stack_ptr = locals;

// initilize all other fields
// --> until end of Frame
gc_oop_t* end = (gc_oop_t*)SHIFTED_PTR(this, totalObjectSize);
size_t i = 0;
while (arguments + i < end) {
arguments[i] = nilObject;
i++;
}
}

void VMFrame::SetMethod(VMMethod* method) {
store_ptr(this->method, method);
}

VMFrame* VMFrame::GetContextLevel(long lvl) {
VMFrame* current = this;
while (lvl > 0) {
Expand Down
32 changes: 17 additions & 15 deletions src/vmobjects/VMFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,25 @@ class VMFrame : public VMObject {

static VMFrame* EmergencyFrameFrom(VMFrame* from, long extraLength);

explicit VMFrame(size_t size, size_t additionalBytes);
explicit VMFrame(size_t additionalBytes, VMMethod* method,
VMFrame* previousFrame)
: VMObject(0, additionalBytes + sizeof(VMFrame)), bytecodeIndex(0),
previousFrame(store_root(previousFrame)), context(nullptr),
method(store_root(method)), arguments((gc_oop_t*)&(stack_ptr) + 1),
locals(arguments + method->GetNumberOfArguments()),
stack_ptr(locals + method->GetNumberOfLocals() - 1) {
// initilize all other fields. Don't need to initalize arguments,
// because they iwll be copied in still
// --> until end of Frame
gc_oop_t* end = (gc_oop_t*)SHIFTED_PTR(this, totalObjectSize);
size_t i = 0;
while (locals + i < end) {
locals[i] = nilObject;
i++;
}
}

inline VMFrame* GetPreviousFrame() const;
inline void SetPreviousFrame(VMFrame*);
inline void ClearPreviousFrame();
inline bool HasPreviousFrame() const;
inline bool IsBootstrapFrame() const;
Expand All @@ -55,7 +70,6 @@ class VMFrame : public VMObject {
VMFrame* GetContextLevel(long);
VMFrame* GetOuterContext();
inline VMMethod* GetMethod() const;
void SetMethod(VMMethod*);

inline vm_oop_t Pop() {
vm_oop_t result = load_ptr(*stack_ptr);
Expand Down Expand Up @@ -83,14 +97,6 @@ class VMFrame : public VMObject {
store_ptr(*stack_ptr, obj);
}

void ResetStackPointer() {
// arguments are stored in front of local variables
VMMethod* meth = GetMethod();
locals = arguments + meth->GetNumberOfArguments();
// Set the stack pointer to its initial value thereby clearing the stack
stack_ptr = locals + meth->GetNumberOfLocals() - 1;
}

inline long GetBytecodeIndex() const;

inline vm_oop_t GetStackElement(long index) const {
Expand Down Expand Up @@ -192,10 +198,6 @@ VMFrame* VMFrame::GetPreviousFrame() const {
return load_ptr(previousFrame);
}

void VMFrame::SetPreviousFrame(VMFrame* frm) {
store_ptr(previousFrame, frm);
}

void VMFrame::ClearPreviousFrame() {
previousFrame = nullptr;
}
Expand Down

0 comments on commit 52e8c0e

Please sign in to comment.