Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid nil-ing VMArray twice and improve strEqual and VMString inlining #55

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/primitives/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>

#include "../misc/defs.h"
Expand Down Expand Up @@ -74,7 +75,7 @@ static vm_oop_t strLength(vm_oop_t rcvr) {
}

static vm_oop_t strEqual(vm_oop_t leftObj, vm_oop_t op1) {
VMString* op2 = static_cast<VMString*>(leftObj);
VMString* left = static_cast<VMString*>(leftObj);

if (IS_TAGGED(op1)) {
return load_ptr(falseObject);
Expand All @@ -85,14 +86,18 @@ static vm_oop_t strEqual(vm_oop_t leftObj, vm_oop_t op1) {
}

VMClass* otherClass = CLASS_OF(op1);
if (otherClass == load_ptr(stringClass) ||
otherClass == load_ptr(symbolClass)) {
StdString s1 = static_cast<VMString*>(op1)->GetStdString();
StdString s2 = op2->GetStdString();
if (otherClass != load_ptr(stringClass) &&
otherClass != load_ptr(symbolClass)) {
return load_ptr(falseObject);
}

if (s1 == s2) {
return load_ptr(trueObject);
}
VMString* right = static_cast<VMString*>(op1);
if (left->length != right->length) {
return load_ptr(falseObject);
}

if (strncmp(left->chars, right->chars, left->length) == 0) {
return load_ptr(trueObject);
}
return load_ptr(falseObject);
}
Expand Down
8 changes: 0 additions & 8 deletions src/vmobjects/VMArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@

const size_t VMArray::VMArrayNumberOfFields = 0;

VMArray::VMArray(size_t arraySize, size_t additionalBytes)
: VMObject(
arraySize + 0 /* VMArray is not allowed to have any fields itself */,
additionalBytes + sizeof(VMArray)) {
assert(VMArrayNumberOfFields == 0);
nilInitializeFields();
}

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

Expand Down
7 changes: 6 additions & 1 deletion src/vmobjects/VMArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ class VMArray : public VMObject {
public:
typedef GCArray Stored;

explicit VMArray(size_t arraySize, size_t additionalBytes);
explicit VMArray(size_t arraySize, size_t additionalBytes)
: VMObject(arraySize +
0 /* VMArray is not allowed to have any fields itself */,
additionalBytes + sizeof(VMArray)) {
assert(VMArrayNumberOfFields == 0);
}

// VMArray doesn't need to customize `void WalkObjects(walk_heap_fn)`,
// because it doesn't need anything special.
Expand Down
19 changes: 0 additions & 19 deletions src/vmobjects/VMString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,6 @@ VMString* VMString::CloneForMovingGC() const {
VMString(length, chars);
}

void VMString::MarkObjectAsInvalid() {
for (size_t i = 0; i < length; i++) {
chars[i] = 'z';
}
chars = (char*)INVALID_GC_POINTER;
}

bool VMString::IsMarkedInvalid() const {
return chars == (char*)INVALID_GC_POINTER;
}

void VMString::WalkObjects(walk_heap_fn) {
// nothing to do
}

size_t VMString::GetObjectSize() const {
size_t size = sizeof(VMString) + PADDED_SIZE(length);
return size;
Expand All @@ -67,10 +52,6 @@ VMClass* VMString::GetClass() const {
return load_ptr(stringClass);
}

size_t VMString::GetStringLength() const {
return length;
}

std::string VMString::GetStdString() const {
if (chars == 0) {
return std::string("");
Expand Down
27 changes: 18 additions & 9 deletions src/vmobjects/VMString.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,30 @@ class VMString : public AbstractVMObject {
return (int64_t)hash;
}

inline char* GetRawChars() const;
inline char* GetRawChars() const { return chars; }

StdString GetStdString() const;
size_t GetStringLength() const;

inline size_t GetStringLength() const { return length; }

VMString* CloneForMovingGC() const override;
VMClass* GetClass() const override;
size_t GetObjectSize() const override;
void WalkObjects(walk_heap_fn) override;

void MarkObjectAsInvalid() override;
bool IsMarkedInvalid() const override;
inline void WalkObjects(walk_heap_fn) override {
// nothing to do
}

void MarkObjectAsInvalid() override {
for (size_t i = 0; i < length; i++) {
chars[i] = 'z';
}
chars = (char*)INVALID_GC_POINTER;
}

bool IsMarkedInvalid() const override {
return chars == (char*)INVALID_GC_POINTER;
}

StdString AsDebugString() const override;

Expand All @@ -80,7 +93,3 @@ class VMString : public AbstractVMObject {
length(length),
chars(adaptedCharsPointer) {}; // constructor to use by VMSymbol
};

char* VMString::GetRawChars() const {
return chars;
}