Skip to content

Commit

Permalink
Improve inlining of array access
Browse files Browse the repository at this point in the history
Move the error handling behind a method boundary and make the rest inlinable.

Signed-off-by: Stefan Marr <git@stefan-marr.de>
  • Loading branch information
smarr committed Aug 11, 2024
1 parent 3a08eea commit 3ef5fb9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
26 changes: 6 additions & 20 deletions src/vmobjects/VMArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,6 @@ VMArray::VMArray(size_t arraySize, size_t additionalBytes)
nilInitializeFields();
}

vm_oop_t VMArray::GetIndexableField(size_t idx) const {
if (unlikely(idx > GetNumberOfIndexableFields())) {
ErrorExit(("Array index out of bounds: Accessing " + to_string(idx) +
", but array size is only " +
to_string(GetNumberOfIndexableFields()) + "\n")
.c_str());
}
return GetField(idx);
}

void VMArray::SetIndexableField(size_t idx, vm_oop_t value) {
if (unlikely(idx > GetNumberOfIndexableFields())) {
ErrorExit(("Array index out of bounds: Accessing " + to_string(idx) +
", but array size is only " +
to_string(GetNumberOfIndexableFields()) + "\n")
.c_str());
}
SetField(idx, value);
}

VMArray* VMArray::Copy() const {
VMArray* copy = Universe::NewArray(GetNumberOfIndexableFields());

Expand Down Expand Up @@ -97,6 +77,12 @@ VMArray* VMArray::CloneForMovingGC() const {
return clone;
}

void VMArray::IndexOutOfBounds(size_t idx) const {
ErrorExit(("Array index out of bounds: Accessing " + to_string(idx) +
", but array size is only " + to_string(numberOfFields) + "\n")
.c_str());
}

void VMArray::CopyIndexableFieldsTo(VMArray* to) const {
const size_t numIndexableFields = GetNumberOfIndexableFields();
for (size_t i = 0; i < numIndexableFields; ++i) {
Expand Down
20 changes: 18 additions & 2 deletions src/vmobjects/VMArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,24 @@ class VMArray : public VMObject {
VMArray* Copy() const;

VMArray* CopyAndExtendWith(vm_oop_t) const;
vm_oop_t GetIndexableField(size_t idx) const;
void SetIndexableField(size_t idx, vm_oop_t value);

inline vm_oop_t GetIndexableField(size_t idx) const {
if (unlikely(idx > numberOfFields)) {
IndexOutOfBounds(idx);
}
return GetField(idx);
}

inline void SetIndexableField(size_t idx, vm_oop_t value) {
if (unlikely(idx > GetNumberOfIndexableFields())) {
IndexOutOfBounds(idx);
}
SetField(idx, value);
}

__attribute__((noreturn)) __attribute__((noinline)) void IndexOutOfBounds(
size_t idx) const;

void CopyIndexableFieldsTo(VMArray*) const;
VMArray* CloneForMovingGC() const override;

Expand Down

0 comments on commit 3ef5fb9

Please sign in to comment.