Skip to content

Commit

Permalink
Further localize the deserializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmacnak committed Mar 17, 2024
1 parent 1eb8170 commit e8b4e68
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 89 deletions.
11 changes: 4 additions & 7 deletions vm/isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void Isolate::PrintStack() {
interpreter_->PrintStack();
}

Isolate::Isolate(void* snapshot, size_t snapshot_length, uint64_t seed)
Isolate::Isolate(const void* snapshot, size_t snapshot_length, uint64_t seed)
: heap_(nullptr),
interpreter_(nullptr),
loop_(nullptr),
Expand All @@ -95,10 +95,7 @@ Isolate::Isolate(void* snapshot, size_t snapshot_length, uint64_t seed)
heap_ = new Heap();
interpreter_ = new Interpreter(heap_, this);
loop_ = new PlatformMessageLoop(this);
{
Deserializer deserializer(heap_, snapshot, snapshot_length);
deserializer.Deserialize();
}
Deserialize(heap_, snapshot, snapshot_length);

AddIsolateToList(this);

Expand Down Expand Up @@ -199,7 +196,7 @@ void Isolate::Interpret() {

class SpawnIsolateTask : public ThreadPool::Task {
public:
SpawnIsolateTask(void* snapshot,
SpawnIsolateTask(const void* snapshot,
size_t snapshot_length,
IsolateMessage* initial_message)
: snapshot_(snapshot),
Expand All @@ -219,7 +216,7 @@ class SpawnIsolateTask : public ThreadPool::Task {
}

private:
void* snapshot_;
const void* snapshot_;
size_t snapshot_length_;
IsolateMessage* initial_message_;

Expand Down
4 changes: 2 additions & 2 deletions vm/isolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ThreadPool;

class Isolate {
public:
Isolate(void* snapshot, size_t snapshot_length, uint64_t seed);
Isolate(const void* snapshot, size_t snapshot_length, uint64_t seed);
~Isolate();

Heap* heap() const { return heap_; }
Expand Down Expand Up @@ -54,7 +54,7 @@ class Isolate {
Heap* heap_;
Interpreter* interpreter_;
MessageLoop* loop_;
void* snapshot_;
const void* snapshot_;
size_t snapshot_length_;
uintptr_t salt_;
Random random_;
Expand Down
2 changes: 1 addition & 1 deletion vm/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int main(int argc, const char** argv) {
void (*defaultSIGINT)(int) = signal(SIGINT, SIGINT_handler);

intptr_t exit_code =
PrimordialSoup_RunIsolate(reinterpret_cast<void*>(snapshot.base()),
PrimordialSoup_RunIsolate(reinterpret_cast<const void*>(snapshot.base()),
snapshot.size(), argc - 2, &argv[2]);

signal(SIGINT, defaultSIGINT);
Expand Down
2 changes: 1 addition & 1 deletion vm/main_emscripten.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ EM_JS(void, _JS_initializeAliens, (), {
});

static psoup::Isolate* isolate;
extern "C" void load_snapshot(void* snapshot, size_t snapshot_length) {
extern "C" void load_snapshot(const void* snapshot, size_t snapshot_length) {
PrimordialSoup_Startup();
_JS_initializeAliens();

Expand Down
2 changes: 1 addition & 1 deletion vm/primordial_soup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ PSOUP_EXTERN_C void PrimordialSoup_Shutdown() {
psoup::OS::Shutdown();
}

PSOUP_EXTERN_C intptr_t PrimordialSoup_RunIsolate(void* snapshot,
PSOUP_EXTERN_C intptr_t PrimordialSoup_RunIsolate(const void* snapshot,
size_t snapshot_length,
int argc,
const char** argv) {
Expand Down
2 changes: 1 addition & 1 deletion vm/primordial_soup.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

PSOUP_EXTERN_C void PrimordialSoup_Startup();
PSOUP_EXTERN_C void PrimordialSoup_Shutdown();
PSOUP_EXTERN_C intptr_t PrimordialSoup_RunIsolate(void* snapshot,
PSOUP_EXTERN_C intptr_t PrimordialSoup_RunIsolate(const void* snapshot,
size_t snapshot_length,
int argc, const char** argv);
PSOUP_EXTERN_C void PrimordialSoup_InterruptAll();
Expand Down
102 changes: 79 additions & 23 deletions vm/snapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

#include <type_traits>

#include "vm/allocation.h"
#include "vm/heap.h"
#include "vm/interpreter.h"
#include "vm/object.h"
#include "vm/os.h"

namespace psoup {

class Deserializer;

class Cluster {
public:
Cluster() : ref_start_(0), ref_stop_(0) {}
Expand All @@ -27,6 +30,54 @@ class Cluster {
intptr_t ref_stop_;
};

class Deserializer : public ValueObject {
public:
Deserializer(const void* snapshot, size_t snapshot_length);
~Deserializer();

intptr_t position() { return cursor_ - snapshot_; }
template <typename T>
T Read() {
T result;
memcpy(&result, cursor_, sizeof(T));
cursor_ += sizeof(T);
return result;
}
template <typename T = uintptr_t>
T ReadLEB128();
template <typename T = intptr_t>
T ReadSLEB128();

void Deserialize(Heap* heap);

Cluster* ReadCluster();

intptr_t next_ref() const { return next_ref_; }

void RegisterRef(Object object) {
refs_[next_ref_++] = object;
}
Object ReadRef() {
return Ref(ReadLEB128());
}
Object Ref(intptr_t i) {
ASSERT(i > 0);
ASSERT(i < next_ref_);
return refs_[i];
}

private:
const uint8_t* const snapshot_;
const size_t snapshot_length_;
const uint8_t* cursor_;

intptr_t num_clusters_;
Cluster** clusters_;

Object* refs_;
intptr_t next_ref_;
};

class RegularObjectCluster : public Cluster {
public:
explicit RegularObjectCluster(intptr_t format, intptr_t cid = kIllegalCid)
Expand Down Expand Up @@ -345,11 +396,11 @@ class FloatCluster : public Cluster {
void ReadEdges(Deserializer* d, Heap* h) {}
};

Deserializer::Deserializer(Heap* heap, void* snapshot, size_t snapshot_length)
Deserializer::Deserializer(const void* snapshot, size_t snapshot_length)
: snapshot_(reinterpret_cast<const uint8_t*>(snapshot)),
snapshot_length_(snapshot_length),
cursor_(snapshot_),
heap_(heap),
num_clusters_(0),
clusters_(nullptr),
refs_(nullptr),
next_ref_(0) {}
Expand All @@ -363,7 +414,7 @@ Deserializer::~Deserializer() {
delete[] refs_;
}

void Deserializer::Deserialize() {
void Deserializer::Deserialize(Heap* heap) {
int64_t start = OS::CurrentMonotonicNanos();

// Skip interpreter directive, if any.
Expand Down Expand Up @@ -392,29 +443,29 @@ void Deserializer::Deserialize() {
for (intptr_t i = 0; i < num_clusters_; i++) {
Cluster* c = ReadCluster();
clusters_[i] = c;
c->ReadNodes(this, heap_);
c->ReadNodes(this, heap);
}
ASSERT((next_ref_ - 1) == num_nodes);
for (intptr_t i = 0; i < num_clusters_; i++) {
clusters_[i]->ReadEdges(this, heap_);
clusters_[i]->ReadEdges(this, heap);
}

ObjectStore os = static_cast<ObjectStore>(ReadRef());

heap_->RegisterClass(kSmallIntegerCid, os->SmallInteger());
heap_->RegisterClass(kMediumIntegerCid, os->MediumInteger());
heap_->RegisterClass(kLargeIntegerCid, os->LargeInteger());
heap_->RegisterClass(kFloatCid, os->Float());
heap_->RegisterClass(kByteArrayCid, os->ByteArray());
heap_->RegisterClass(kStringCid, os->String());
heap_->RegisterClass(kArrayCid, os->Array());
heap_->RegisterClass(kWeakArrayCid, os->WeakArray());
heap_->RegisterClass(kEphemeronCid, os->Ephemeron());
heap_->RegisterClass(kActivationCid, os->Activation());
heap_->RegisterClass(kClosureCid, os->Closure());

heap_->interpreter()->InitializeRoot(os);
heap_->InitializeAfterSnapshot();
heap->RegisterClass(kSmallIntegerCid, os->SmallInteger());
heap->RegisterClass(kMediumIntegerCid, os->MediumInteger());
heap->RegisterClass(kLargeIntegerCid, os->LargeInteger());
heap->RegisterClass(kFloatCid, os->Float());
heap->RegisterClass(kByteArrayCid, os->ByteArray());
heap->RegisterClass(kStringCid, os->String());
heap->RegisterClass(kArrayCid, os->Array());
heap->RegisterClass(kWeakArrayCid, os->WeakArray());
heap->RegisterClass(kEphemeronCid, os->Ephemeron());
heap->RegisterClass(kActivationCid, os->Activation());
heap->RegisterClass(kClosureCid, os->Closure());

heap->interpreter()->InitializeRoot(os);
heap->InitializeAfterSnapshot();

int64_t stop = OS::CurrentMonotonicNanos();
int64_t time = stop - start;
Expand All @@ -424,15 +475,15 @@ void Deserializer::Deserialize() {
"with %" Pd " objects "
"in %" Pd64 " us\n",
snapshot_length_ / KB,
heap_->Size() / KB,
heap->Size() / KB,
next_ref_ - 1,
time / kNanosecondsPerMicrosecond);
}

#if defined(DEBUG)
size_t before = heap_->Size();
heap_->CollectAll(Heap::kSnapshotTest);
size_t after = heap_->Size();
size_t before = heap->Size();
heap->CollectAll(Heap::kSnapshotTest);
size_t after = heap->Size();
ASSERT(before == after); // Snapshots should not contain garbage.
#endif
}
Expand Down Expand Up @@ -509,4 +560,9 @@ Cluster* Deserializer::ReadCluster() {
}
}

void Deserialize(Heap* heap, const void* snapshot, size_t snapshot_length) {
Deserializer d(snapshot, snapshot_length);
d.Deserialize(heap);
}

} // namespace psoup
54 changes: 1 addition & 53 deletions vm/snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,14 @@
#ifndef VM_SNAPSHOT_H_
#define VM_SNAPSHOT_H_

#include "vm/allocation.h"
#include "vm/globals.h"
#include "vm/object.h"

namespace psoup {

class Cluster;
class Heap;
class Object;

// Reads a variant of VictoryFuel.
class Deserializer : public ValueObject {
public:
Deserializer(Heap* heap, void* snapshot, size_t snapshot_length);
~Deserializer();

intptr_t position() { return cursor_ - snapshot_; }
template <typename T>
T Read() {
T result;
memcpy(&result, cursor_, sizeof(T));
cursor_ += sizeof(T);
return result;
}
template <typename T = uintptr_t>
T ReadLEB128();
template <typename T = intptr_t>
T ReadSLEB128();

void Deserialize();

Cluster* ReadCluster();

intptr_t next_ref() const { return next_ref_; }

void RegisterRef(Object object) {
refs_[next_ref_++] = object;
}
Object ReadRef() {
return Ref(ReadLEB128());
}
Object Ref(intptr_t i) {
ASSERT(i > 0);
ASSERT(i < next_ref_);
return refs_[i];
}

private:
const uint8_t* const snapshot_;
const intptr_t snapshot_length_;
const uint8_t* cursor_;

Heap* const heap_;

intptr_t num_clusters_;
Cluster** clusters_;

Object* refs_;
intptr_t next_ref_;
};
void Deserialize(Heap* heap, const void* snapshot, size_t snapshot_length);

} // namespace psoup

Expand Down

0 comments on commit e8b4e68

Please sign in to comment.